I2C Protocol with M5CoreInk using Wire Library

Hi guys,

I would like to use my M5CoreInk (ESP32) to communicate with my Adafruit STH40 Temperature and Humidity sensor using I2C. However, instead of using the Adafruit STH4x library, I want to interact with the sensor through the Wire library. I have gone through a couple of basic examples using the functions of the Wire library, and here is my snippet of the code:

#include <Arduino.h>
#include <M5CoreInk.h>
#include <Wire.h>
#include "Adafruit_SHT4x.h"

#define M5CoreInk_SCL 22
#define M5CoreInk_SDA 21

#define STH40_Add 0x44

 
void setup() {
  Wire.begin(21,22);
  Serial.begin(115200);
  Serial.println("Hello");
}
 
void loop() {
    uint8_t readbuffer[6];
    Wire.beginTransmission(0x44);
    Serial.println("T1");
    Wire.write(0xFD);
    Wire.endTransmission();
    Serial.println("T2");
    Wire.requestFrom(0x44, 6);
    Serial.println("T3");

    if (Wire.available()){
        while (Wire.available()) {
            for (int i = 0; i < 6; i++) {
                readbuffer[i] = Wire.read();
            }
        }
    }

    float t_ticks = (uint16_t)readbuffer[0] * 256 + (uint16_t)readbuffer[1];
    float rh_ticks = (uint16_t)readbuffer[3] * 256 + (uint16_t)readbuffer[4];

    float temp = -45 + 175 * t_ticks / 65535;
    float humidity = -6 + 125 * rh_ticks / 65535;

    Serial.printf("Temp: %f", temp);
    Serial.println();
    Serial.printf("Humidity: %f", humidity);
    Serial.println();
    
    delay(2000);
}

Unfortunately, I keep running into the same problem of "requestfrom(): i2cread returned error 263", and the "No data" condition was triggered. I have double-checked my wiring, even ran the I2C_Scan code, and been able to detect the sensor at the address "0x44". So I would like to ask for some guidance on using the Wire library for I2C protocol interaction.

Here is the datasheet I found for the STH40 sensor (the one used in the Adafruit breakout board). It has already had the pull-up resistors for the SDA, and SCL. Also, this is the link to the online document for the M5CoreInk

Sensirion_Datasheet_SHT4x-2936319.pdf (801.5 KB)

Please let me know if you require any additional information.

Try adding pull up resistors, I could not find the schematic for the sensor. I just had an experience where the scanner worked without any problems but the board did not work. Added the pull up resistors and it is running without a problem.

Hi,

The datasheet I attached to the post is only the sensor alone, to show the Command Overview part which I am trying to do (Section 4.5: Command Overview). I am currently interacting with the Adafruit STH40 Breakout board, the link is attached below:

According to their schematic, I think they already have the pull-ups. I am attaching it here as well.

Furthermore, I have tried to interact with the sensor using the Adafruit STH4x library. I was able to read the temperature and humidity from it just fine.

Thanks,

Ok, I have found the issue. Apparently, I just need to add a delay of 15 ms before the requestFrom() function. The problem is solved, but any explanation for it would be appreciated. Thanks guys!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.