I2C, NACK on ESP32 boards (SHT21 sensor)

Posted on
  • Hi!

    Edit: tested some more boards:

    1. Espruino Wifi - ok! (hw I2C)
    2. Pixl.Js - ok! (hw I2C1)
    3. ESP32 - not OK, my original issue, details below
    4. ESP8266 - OK! There was some issue previously with maybe my wiring?
      Tried to read an SHT21 sensor with an ESP32, but the SHT2x module did not work. Failed with CRC error, and got some console warnings about NACK by the IIC slave.
      Wiring is good, there are other I2C sensors on the same board, and those works It does work. Tried the code on an Espruino wifi, and works without any problems.

    After digging into the datasheet, and the "hold master" measurement failed, tried the "no hold master" way of things...
    You start the conversion, and poll the sensor: the SHT21 responds with NACK while the measurement is not ready. Later managed to use the CRC check and retry reading until the sensor actually finished conversion, so I have working code on the ESP32 as well. (give me some time, and will post it)

    My guess is that the STM32 and ESP32 I2C implementation differs: on STM32 it tries to read until it gets proper response with ACK from the IIC slave, and discards NACKs. On the other hand the ESP32 just returns data even when the slave said NACK.
    Does anyone know whether this is true (without digging into the C source code)?
    If it's true, is there some "official" guidance how to handle this in modules? The code for the ESP32 is of course bigger, so I guess it wouldn't benefit everyone to overwrite the current module.

    ESP8266 - used software I2C, temperature reading fails. Hardware I2C does work. Just had some unknow problem...
    Temperature reading fails with an I2C Timeout error.
    Humidity measurement works. I guess the Espruino interpreter + soft I2C is slow enough that readHumidity does not time out before the humidity measurement is complete (22-29ms accoring to the datasheet) readTemperature consistently times out (takes 66-85ms).
    But this is not the same as the ESP32, where the I2C read succeeded & ignored the NACK.
    Funnily enough my ESP32 code does work, but I guess simply because the setTimeout + speed of the ESP8266 + software I2C is slow enough.

    Used the hardware I2C1 where I could:
    ESP32: I2C1.setup({sda: D21, scl: D22}); I got the pins right, right? :)
    Espruino Wifi: I2C1.setup({sda: B9, scl: B8});
    Pixl.JS: I2C1.setup({sda: A4, scl: A5});
    ESP8266: i2c.setup({sda: NodeMCU.D2, scl: NodeMCU.D1});

  • A quick google shows


    I think this is related to clock steching by the slave which might not be implemented in the hardware version used on the esp32.

    Can you try the software implementation?

  • Software I2c fails too on the ESP32.

    Result with soft I2c:

    Uncaught Error: I2C Error: Timeout (rd)
     at line 1 col 68
    in function "readTemperature" called from line 41 col 53
    console.log('SHT Temperature:', sht.readTemperature());

    That this.i2c.readFrom(this.addr,3)... fragment is from the minified module.

    Result with hardware I2C:

    ERROR: jshI2CRead:, Operation timeout because the bus is busy
    Uncaught CRC Error

    The Uncaught CRC Error comes from this line of the module.

  • Software I2C & SHT21 fails the same way on Espruino Wifi board as well.

    Other sensors on the same board (BMP085 and ISL29023) work with software&hardware I2C on Espruino Wifi and ESP32 without any problem with the same code.

  • ESP8266: i2c = new I2C(); i2c.setup({sda: D4, scl: D5});

    This is the Espruino software implementation of i2c, try I2C1 which is a c coded software implementation based on espressif sample and extended by clock stretching.

  • @MaBe good find! It works!
    Had some issue when tried to get the HW I2C working on the ESP8266. Probably it was just wiring, or dunno, that's why I had software I2C there. But just tried now, and it does work!

    And one more bit of info: since the "hold master" measurement failed, tried the "no hold master" way of things with my code on the ESP32.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

I2C, NACK on ESP32 boards (SHT21 sensor)

Posted by Avatar for AkosLukacs @AkosLukacs