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)
Sebastian wrote: > Automotive UNO Automotive µC ? No! (?)
Sebastian wrote: > #define I2C_WRITE 0; All these semicolons in defines are plain wrong. leo
so you are saying the datasheet is wrong? because thats the one our prof. was using.
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
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
Don´t know this Sensor, but it seems to be the wrong Address. Low - 0x23 High - 0x5C Simply try to change the Code from
uint8_t SLA_W = 0x5C; // Address from slave + write (which is +0) // 0x5c because i found it in the Internet not in data sheet.
uint8_t SLA_W = 0x23; // Address from slave + write (which is +0) // 0x5c because i found it in the Internet not in data sheet.
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
[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.