Pages

Monday, February 18, 2013

Light intensity (LUX) measure using BH1750 sensor with an AVR Atmega library

updated to version 02

This is a BH1750 light intensity sensor module with built-in a 16 bit AD converter generating digital signal. With the BH1750 Light Sensor intensity can be directly measured by the luxmeter, without needing to make calculations.
This library provides function to measure lux through I2C.
This library was developed on Eclipse, built with avr-gcc on Atmega8 @ 1MHz.

User has to setup only resolution mode.
Setup parameters in bh1750.h


Changelog
  • 02: fixed a bug in lux conversion formula.
  • 01: first release.

Code

Notes
  • read risk disclaimer
  • excuse my bad english

15 comments:

  1. where should i give pin numbers in source code..?

    ReplyDelete
    Replies
    1. Hello. This a I2C sensor, you have to connect it to the I2C pin of your micro. As example, for the ATmega8 SCL is PC5, SDA is PC4.

      Delete
    2. sir ,how to display the lux reading is this code is sufficient or we have to made another code for lcd display?? and what is the range output(voltage/current) we are getting at SDA pin of sensor..?

      Delete
    3. To display values on LCD, you need an LCD library. Then instead of using the uart_puts, you should use your LCD library printout function. About the SDA voltage, it's typically it's 5+. For more information about the I2C protocol, you can take a look to the I2C wikipedia page, or on the ATmega datasheet.

      Delete
    4. Hello David, do you have an example code to use the LCD with the sensor? Im working on a project with a BH1750 sensor and a HD44780 LCD. Im working with a ATMEGA328p but cant get it working.

      Delete
    5. Hello, unluckly I do not have an LCD sample for this, but you can use the P.Fleury LCD library, it's a pretty simple library to work with.

      Delete
    6. Hello, I already have an working lcd code, and i have got the BH1750 Lightsensor code from you. The problem is: I have no idee how to get the data from the lightsensor on the LCD. I can't even test your BH1750 code becuase i cant see it on the lcd. So Besides the fact that the code has no errors, i dont even know if it works. Im using eclipse btw.

      Sorry, but im still learning so its hard to understand sometimes

      Delete
    7. Hello, let's suppose your lcd output function for a char array is lcd_puts. Then you could do something like this in your main loop: char printbuff[100]; int lux = bh1750_getlux(); itoa(lux, printbuff, 10); lcd_puts("lux: "); lcd_puts(printbuff); lcd_puts("\r\n");. But I will test the BH1750 using uart, before using an LCD, mixing two test together isn't always the best option.

      Delete
  2. oke thanks man i will try this. i cant use that library btw. It errors every time. im using http://winavr.scienceprog.com/example-avr-projects/avr-gcc-4-bit-and-8-bit-lcd-library.html right now.

    ReplyDelete
    Replies
    1. Hello, you can try the LCDsendChar function. Something like this, instead of the lcd_puts should work LCDstring(printbuff, strlen(printbuff));

      Delete
  3. Hey David! I'm still struggling to get the light sensor working along with the LCD screen. This is what I am using: hd44780 + BH1750 sensor amega 328p.

    Ive got the LCD working on it's own. But can i maybe send the code to an email or something? So you can maybe see the problem with it.

    ReplyDelete
    Replies
    1. Hello, I'm sorry but i can not review code, I hope you understand, if I do it for you, I must do it for all. So, can you print out an int value on the LCD?

      Delete
  4. Hi David! I use Code Vision AVR to code an ATMega. So, is your code still relevant with Code Vision AVR?

    ReplyDelete
    Replies
    1. Hello, I've never compiled it using the code vision compiler, this project is built using avrgcc. I think you have to port it to the code vision avr compiler.

      Delete