Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Help with MAX44009 sensor needed please

Home Forums MikroC Pro for PIC Project Discussions Help with MAX44009 sensor needed please

This topic contains 7 replies, has 2 voices, and was last updated by  Bitahwa Bindu 1 month, 3 weeks ago.

  • Author
    Posts
  • #5274

    SheldonNyce
    Participant

    Hey all Programming Gurus!

    I’m hoping that someone here may have seen or worked with this sensor in the past.

    I need to interface a MAX44009 to a PIC24FJ64GB002 microcontroller and display the lux reading, continuously (at a refresh rate of 100ms), in real-time, on an LCD. I am coding the system using the dsPIC MikroC IDE.

    I suspect that there is an issue with the C code.

    I do not want to enable the interrupt setting of the sensor IC, I just want to read the lux value on the LCD every 100ms.

    The code compiles without error or warning, I am just concerned that I have configured it incorrectly. The sensor package is the GY-49, and I suspect that the address pin is high (A0) thus you will note different slave address to the typical MAX44009 code already available on Github. I have tried the slave address with A0 low, and still no response. If you are familiar with the GY-49package, you will know that only the VCC, GND, SDA, SCL pins are labelled, and the INT and A0 pins are not. So I have no idea which pin is A0. So it’s a bit of a guess, but both slave addresses don’t work.

    I’m worried that I have coded register 0x02 and register 0x03 incorrectly. I think this is a code issue, not a library issue, and so anyone who’s worked with this sensor might be able to spot the error quickly.

    Please could you take a look at the code attached. I couldn’t upload the datasheet, as the file was too big. But if you Google “MAX44009 datasheet” it’s the first datasheet that shows up, by MAXIM INTEGRATED.

    I measured the SCL and SDA lines on the oscilloscope, and both pins are just high all the time (3.3V) and yes, there are pull-up resistor attached. But it seems like there is no communication, even when zoomed in to 20us/div and the trigger is set at the right level.

    I would really appreciate any and all assistance.

    Thanks very much

    Attachments:
    You must be logged in to view attached files.
  • #5286

    Bitahwa Bindu
    Keymaster

    Hi,

    The first thing to check with I2C communication before anything else is to make sure the master (Microcontroller) can talk to the slave if the start command can be established.

    Based on the supplied information, it seems like it may be a hardware issue. The master can’t can’t acknowledgment form slave.

    Are you use you are using the correct address for the slave? Can you try the address: 0x94 (write) and 0x95 (read) and check if the A0 pin is grounded? From the datasheet (page 5) I see that chip has 6 pins and the A0 pin is pin 3. Try to check continuity on the PCB if it’s not marked if this pin is grounded by default or connected to VCC or it’s floating.

    The other thing is the initialization of the chip, these lines:

    I2C_Write(0x02); //Address specific register 02 (interupt register) for MAX44009
    I2C_Write(0xC3); //Continuous mode, 100ms integration time

    How did you get the value of 0xC3? Why can’t you use the default integration time of 100ms which is 0x3 (page 9)?

    I would do it like this:

    I2C_Start(); //Wake-up I2C components
    I2C_Write(0x94); //Address MAX44009 lux sensor specifically, and prepare for write command
    I2C_Write(0x02); //Address specific register 02 (interupt register) for MAX44009
    I2C_Write(0x03); //Continuous mode, 100ms integration time
    I2C_Stop(); //issue I2C stop signal
    Delay_ms(300); //Because Arduino said so….

     

    Try:

    void MAX44009_getvalue(){

    unsigned char value-0x00;

    I2C1_Start();
    I2C_Write(0x94);
    I2C_Write(0x03);
    I2C_Restart(); //Not actually writing to the register, so we don’t need a stop signal – we actually intend to read
    I2C_Write(0x95); //Address MAX44009 specifically, we want to read.

    value = I2C_Read(0);
    I2C1_Stop();
    }

     

    or you can try this if it doesn’t work:

    void MAX44009_getvalue(){

    unsigned char value-0x00;

    I2C1_Start();
    I2C_Write(0x94);
    I2C_Write(0x03);
    I2C_Restart(); //Not actually writing to the register, so we don’t need a stop signal – we actually intend to read
    I2C_Write(0x95); //Address MAX44009 specifically, we want to read.

    value[0] = I2C_Read(0);

    I2C_Restart();

    I2C_Write(0x94);
    I2C_Write(0x04);
    I2C_Restart(); //Not actually writing to the register, so we don’t need a stop signal – we actually intend to read
    I2C_Write(0x95); //Address MAX44009 specifically, we want to read.

    value[1] = I2C_Read(0);

     

    I2C1_Stop();
    }

  • #5341

    SheldonNyce
    Participant

    Hi Guys,

    I recently tried to program a MAX44009 sensor to interface with PIC24FJ64GB002 microcontroller. The code compiles without failure, but it’s not working. When I view the SDA and SCL lines using an oscilloscope, they are always held high. But if I convert: “float luminence into: “int luminence” and “FloatToStr (luminence, lux)” into “IntToStr (luminence, lux)” then the code is loaded onto the embedded device, and the oscilloscope shows that the I2C communication is taking place, but the value the LCD displays does not give any valuable data. The lux display is -17204 and it never changes.

    I tried to display “exponent” on the LCD and it displayed: -17204
    I tried to display “mantissa” on the LCD and it displayed: 255

    When I tried to debug the code, when luminence is declared as a float, I get an error: “Simulation Error: PC out of scope [0x0011F0]”
    When I change from float to int; I can debug the code, but the value output on the LCD is useless.

    I am using the free download version of dsPIC MikroC PRO compiler, and programming the hardware using MikroProg Suite and MikroProg Programmer.

    Any ideas? Is it because I am using the freeware? I am a student and cannot afford the licensed version.

    Please see attached file, I have included the MAX44009 user manual, pictures of Oscilloscope output, and associated code.

    All input will be welcomed. TIA

    Attachments:
    You must be logged in to view attached files.
  • #5346

    Bitahwa Bindu
    Keymaster

    Hi, can you post the additional information like oscilloscope output images, your circuit diagram etc. You only posted the code.

    Try to zip your files and please note for server’s performance, we have limited the maximum size for each upload to 512KB. If your file is bigger than this, it won’t be uploaded.

    You can upload multiples times if you have more than 1 big files.

    When you use the Float variables, does your code compiles without any errors? Because the free version of Mikroelekronika compilers (Free demo versions) support the projects with a size less than 2K of program words. And if you compile big projects, it will give you the Demo limit compile error, if you don’t get this error, that it can’t be because you are using the Demo version. Floating point variables are the number one culprits when it comes to consuming memory, so by changing from float to int, you might decrease your program size but it might affect the reading.

    In the IDE, in memory view, when your code compiles, what are the memory statistics? Are your program memory size and RAM still within the maximum of your PIC24FJ64GB002?

    When you change from float to int, does the output value changes if you change the luminosity of the sensor?

  • #5357

    SheldonNyce
    Participant

    Hi Bitahwa, Thanks for getting back to me.

    I got the sensor working… sort of. I set A0 = GND, and worked out that the sensor did not want to send an acknowledge bit after a reading. So I changed that, and now the sensor is producing meaningful data that can be read off the LCD. The poor master MCU was waiting for an acknowledge bit, which the sensor wasn’t sending and so it was getting stuck :cry:

    But I have another problem.

    If I set the variable “luminance” as
    <div>
    <div class=”codetitle”><b>Code:</b> Select all</div>
    <div class=”codecontent”>int luminance;</div>
    </div>
    and
    <div>
    <div class=”codetitle”><b>Code:</b> Select all</div>
    <div class=”codecontent”>unsigned char lux[15];</div>
    </div>
    and within the main function, perform the conversion:
    <div>
    <div class=”codetitle”><b>Code:</b> Select all</div>
    <div class=”codecontent”>IntToStr(luminance, lux);</div>
    </div>
    Everything works fine. I get a meaningful lux reading for the sensor. But, it is not a floating point number, ergo, no decimal point accuracy.

    If I set the variable “luminance” as
    <div>
    <div class=”codetitle”><b>Code:</b> Select all</div>
    <div class=”codecontent”>float luminance;</div>
    </div>
    and
    <b></b>

    <b>Code:</b> Select all
    <div>
    <div class=”codecontent”>unsigned char lux[15];</div>
    </div>
    and within the main function, perform the conversion:
    <div>
    <div class=”codetitle”><b>Code:</b> Select all</div>
    <div class=”codecontent”>FloatToStr(luminance, lux);</div>
    </div>
    Everything does not work. The whole program gets stuck and implodes. For some reason; it absolutely hates floating point numbers?

    Any ideas as to why this might be the case? Am I setting the conversion up incorrectly? Is it perhaps that the LCD doesn’t like the data being sent to it?

    Given the scope of the project I am working on, it is not absolutely imperative that I establish lux readings with accuracy to decimal point values – whole numbers are fine. But the next part of the project will be to set up a neural network, which will ABSOLUTELY require the use of floating point numbers… So it is important that I get a grasp of the concept, and this current project will provide a valuable opportunity to do so.

    I have included the semi-working code below.

    Any and all assistance will be greatly appreciated; TIA

    Attachments:
    You must be logged in to view attached files.
  • #5359

    Bitahwa Bindu
    Keymaster

    Hi. Try to format your code properly or remove the format all together when posting, it makes it easier to read.

    As I posted in previous replies, before we check your floating conversion, can you post a screenshot of your PIC memory usage? When you use floating variables, what’s the percentage of memory are used?

    When you say it explodes what do you mean exacly? Does it still compiles with no errors? If you debug,  at which line does it get stuck? What values are you reading?

    You didn’t the previous message.

  • #5411

    SheldonNyce
    Participant

    Good Morning guys! I fixed all the problems – Please see attached for working code for MAX44009 lux sensor. Thank you for all the help!

    Attachments:
    You must be logged in to view attached files.
  • #5416

    Bitahwa Bindu
    Keymaster

    Hi, thanks a lot for your feedback, this will benefit other people in the forum.

    Much appreciated.

     

You must be logged in to reply to this topic.

Select your currency
EUR Euro
USD United States (US) dollar