0
\$\begingroup\$

I am trying to read temperature from a SHT21 sensor using an ESP32-WROOM board. The I2C driver is from ESP-IDF SDK. This is my reading sequence:

cmd = i2c_cmd_link_create();
i2c_master_start(cmd);                              // Send i2c start on bus
i2c_master_write_byte(cmd, (I2C_ADD<<1) | I2C_MASTER_WRITE, 1);
i2c_master_write_byte(cmd, command, 1);

i2c_master_start(cmd);
i2c_master_write_byte(cmd, (I2C_ADD<<1) | I2C_MASTER_READ, 1);
i2c_master_read_byte(cmd, data+0, 1); // data MSB
i2c_master_read_byte(cmd, data+1, 1); // data LSB
i2c_master_read_byte(cmd, &checksum, 1); // CRC
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);

Logical analyser recording

Based on the recording from my logical analyser I can tell that all write commands are executed properly. However there is no clock for reading the response from sensor. The CLK line remains low for another 41 ms.

So where is the clock for reading the response? Am I using the I2C driver incorrectly? Is the 41 ms delay produced by the sensor because of clock stretching? But shouldn't then the ESP32 continue with the reading clock afterwards? Am I missing anything else?

Thanks for all responses!

\$\endgroup\$
1
  • \$\begingroup\$ I2C uses pullups; if a slave is holding the clock line low there's nothing the master can do to actuate the clock. Incorrect access attempts can sometimes do this. You might want to cross-check what you are doing, for example use a simpler operation on this target, or try a simpler target, or use a different MCU or different software on this MCU, and then try to figure out what the functional difference between the working and non-working cases is. \$\endgroup\$ Jul 15, 2018 at 16:45

1 Answer 1

1
\$\begingroup\$

However there is no clock for reading the response from sensor. The CLK line remains low for another 41 ms.

RTFM (or the SHT21 datasheet in this case). The E3 command is "hold master" mode - the datasheet explicitly says:

the SCL line is blocked (controlled by sensor) during measurement process

So eveything normal in your logic analyer shot above.

But shouldn't then the ESP32 continue with the reading clock afterwards?

Depends on how I²C is implemented - and whether clock stretching is actually supported. Some TWI implementations may not support slave clock stretching.

Note that the 41ms dead time could also trigger the watchdog.

I recommend trying no-hold master mode with properly timed read access, that is 85ms for 14-bit T and 29ms for 12-bit RH according to the datasheet.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.