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.