I2C problem with BH1750

edo76mal
Posts: 16
Joined: Wed May 11, 2022 10:17 am

I2C problem with BH1750

Postby edo76mal » Wed Jun 18, 2025 9:58 am

Hi.

I'm developing, with wroom32, a simple weather station.
I'm using esp-idf 5.4.2

I find that using i2c.c [with attached i2cdev_OK] everything works, while using i2c_master.c [with attached i2cdev_NO_OK] trasmission fails. In particular I find this:

Found BH1750 device at 0x23
[0;31mE (649) i2c.master: I2C transaction unexpected nack detected[0m
[0;31mE (649) i2c.master: s_i2c_synchronous_transaction(918): I2C transaction failed[0m
[0;31mE (652) i2c.master: i2c_master_multi_buffer_transmit(1180): I2C transaction failed[0m

It seems a problem of i2c_master.

Can you help me?
Attachments
i2cdev_OK.zip
(4.12 KiB) Downloaded 14 times
i2cdev_NO_OK.zip
(7.93 KiB) Downloaded 14 times

MicroController
Posts: 2704
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: I2C problem with BH1750

Postby MicroController » Wed Jun 18, 2025 12:27 pm

The BH1750 seems to expect a NACK from the master at the end of a read transaction. The master not sending the NACK at the end may 'confuse' the slave and cause it to respond with a NACK to the next write transaction.

Try using i2c_master_transmit_receive() instead of i2c_master_transmit() + i2c_master_receive() in i2c_dev_read_reg().

edo76mal
Posts: 16
Joined: Wed May 11, 2022 10:17 am

Re: I2C problem with BH1750

Postby edo76mal » Wed Jun 18, 2025 1:31 pm

@MicroController

It doesn't work.

edo76mal
Posts: 16
Joined: Wed May 11, 2022 10:17 am

Re: I2C problem with BH1750

Postby edo76mal » Wed Jun 18, 2025 4:05 pm

Atteched sample projecdt with problem
Attachments
BH1750_Prj.zip
(43.4 KiB) Downloaded 16 times

eriksl
Posts: 232
Joined: Thu Dec 14, 2023 3:23 pm
Location: Netherlands

Re: I2C problem with BH1750

Postby eriksl » Mon Jul 21, 2025 11:19 am

@microcontroller: either method (read or write+repeatedstart+read) will work, both will stop data reception with a NAK. So that's not the problem.

I have it working, so it should be possible.

The bh1750 is a very simple I2C device. It accepts either a write with a "register pointer" or a read (no register pointer). So a write-repeatedstart-read cycle doesn't make sense. IIRC the register pointer can be set independent of the value (i.e. in 2 transactions).

Who is online

Users browsing this forum: PetalBot, Qwantbot and 0 guests