Guest
#6288830
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)
