EmbDev.net

Forum: µC & Digital Electronics reading BH1750 lightsensor with Atmega328P using i2c /TWI


von Sebastian (Guest)


Attached files:

Rate this post
useful
not useful
Hi,
currently i am trying to connect a BH1750 lightsensor with i2C/TWI to my 
ATmega328P (Arduino uno but i dont use the arduino interface). After 3 
days of failure i decided to get some help.

I dont know if my code is wrong or my hardware wiring. i attached the 
main.c which contains all the code i use to test it. I also attached a 
fritzing picture of the wiring.

currently i have the following working:
- i set the start condition for i2c
- i send the slave address (TWDR = SLA_W which is 0x5C)
- i specify the register i want to read (TWDR = 0x10 based on BH1750 
Datasheet in which operation code for recommended mode is 0001_0000)
- i switch master to read by sending another start signal and setting
TWDR = SLA_R (0x5D)
- Now i want to read the message from BH1750 so i set TWCR as usual but 
this time with ACK (acknowledge)
- And now i am stuck i wait for completion as i did after each operation 
using: while(!(TWCR & (1<<TWINT))); but the while never ends.

i would expect to get the first data byte now which hopefully is the 
first part of some lumen value. In Total i expect 2 bytes of date 
because the sensor resolution is 1-65535 lx.

I know my code is not flawless but for now i just want the. connection 
to work (I miss stuff like status checks and first part of communication 
should also use ACK,..)

If anyone has an idea what the problem could be please tell me because 
at the moment i am clueless.

greetings
Sebastian

-----
BH1750 Datasheet: 
https://components101.com/sites/default/files/component_datasheet/BH1750.pdf

Atmega328P Datasheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf 
(i2C starts at page 174)

von Willi (Guest)


Rate this post
useful
not useful
Sebastian wrote:
> Automotive

UNO Automotive µC ?
No! (?)

von leo (Guest)


Rate this post
useful
not useful
Sebastian wrote:

> #define I2C_WRITE 0;

All these semicolons in defines are plain wrong.

leo

von Sebastian (Guest)


Rate this post
useful
not useful
so you are saying the datasheet is wrong? because thats the one our 
prof. was using.

von Sebastian (Guest)


Rate this post
useful
not useful
leo wrote:
> Sebastian wrote:
>
>> #define I2C_WRITE 0;
>
> All these semicolons in defines are plain wrong.
>
> leo

removed them but i doubt it made a difference couldnt see any

von leo (Guest)


Rate this post
useful
not useful
Sebastian wrote:
> removed them but i doubt it made a difference couldnt see any

Yes, it did work by chance. Though I didn't look further as you have 
likely more C-related errors in the code. And yes ...

>   TWBR = ((16000000 / 100000) -16) / 2;

That may overflow. Compare to the F_CPU define.

leo

von 123abc456 (Guest)


Rate this post
useful
not useful
Don´t know this Sensor, but it seems to be the wrong Address.

Low - 0x23
High - 0x5C

Simply try to change the Code from
1
uint8_t SLA_W = 0x5C; // Address from slave + write (which is +0) // 0x5c because i found it in the Internet not in data sheet.

to
1
uint8_t SLA_W = 0x23; // Address from slave + write (which is +0) // 0x5c because i found it in the Internet not in data sheet.

von Sebastian (Guest)


Rate this post
useful
not useful
I tryd both with 0x5C i get to the following point:
start condition
send address
specify register
switch master to read
read first message
waiting for completion of read...

with 0x23 i get the following:
start condition
send address
specify register

but you are right it should be 0x23 because i connected ADDR pin to 
ground at least thats something i read. Maybe the register 0x10

von Sebastian (Guest)


Attached files:

Rate this post
useful
not useful
[SOLVED]
Ok i got it working now. The major issue was that the address i used was 
wrong. The solution was basically at Page 7 of the BH1750 datasheet. 
There i found an example with address bites that worked.

I also added ACK accordingly and some status checking for debugging.
And i completely removed the ADDR wiring so the ADDR Pin of the BH1750 
has now no connection at all.

I attached the working code as LightSensorRead.c .

thanks for the comments.

von Tommy Z. (Company: no) (tommynewman)


Rate this post
useful
not useful
Hello Sebastian,

Have you got the BH1750 working properly ? I also want to get this 
device work in non arduino interface. I am writing it in Atmel studio . 
But i cannot get it works .

Can you or any one share experiacne or code for this ?

thank you !
Tommy

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.