Wire.read() not working in PlatformIO but Arduino IDE on ESP32-S3

Hello Community,

This is my first post, so please bear with me.

I have a simple piece of code where I read a register from an I2C device.

Here’s the embedded code:

#include <Arduino.h>
#include <Wire.h>
#include <esp_sleep.h>

#define GYRO_ADDRESS 0x36

uint8_t readRegister8_SH5001(uint8_t reg) {
  Wire.beginTransmission(GYRO_ADDRESS);
  Wire.write(reg);
  Wire.endTransmission(true);
  Wire.requestFrom(GYRO_ADDRESS, 1);
  if (Wire.available()) {
    uint8_t data = Wire.read();
    return data;
  }
  return 0xFF;
}  

void setup() {
  Serial.begin(115200);

  Wire.setPins(40, 39);
  Wire.begin();
  Wire.setClock(100000);

  Serial.println("PlatformIO ReadOut Register test");

  uint8_t registers[15] = {0x2B, 0x00, 0xD1, 0xD1, 0x30, 0x42, 0x43, 0x40, 0x4D, 0x54, 0x56, 0x21, 0x23, 0x25, 0x24};
  for (int i = 0; i < sizeof(registers) / sizeof(registers[0]); i++) {
    Serial.print("Read register 0x");
    Serial.print(registers[i], HEX);
    Serial.print(" with 0x");
    Serial.println(readRegister8_SH5001(registers[i]), HEX);
  }
  esp_deep_sleep_start();
}

void loop() {
}

This code runs perfectly when uploaded using the Arduino IDE. However, it returns incorrect values when compiled and uploaded using PlatformIO.

I’ve noticed that there are differences in the packages used by the Arduino IDE and PlatformIO. Although I find it odd that these differences could affect I2C functionality, I believe this might be the cause of the issue. I’m struggling to get the packages aligned between both environments, so if anyone has advice on how to achieve that, I would really appreciate it.

I’ve also tried searching online but haven’t found something that really helps me. Many attempts where I failed … And that is why I ask now for help here.

I’m using the ESP32-S3 8MB module. The version of the Wire.h library is 2.0.0 in both environments.

Here is my PlatformIO .ini file:

[env:esp32s3]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino

The packages from PlatformIO are:

  • framework-arduinoespressif32 @ 3.20006.221224 (2.0.6)
  • tool-esptoolpy @ 1.40400.0 (4.4.0)
  • tool-mkfatfs @ 2.0.1
  • tool-mklittlefs @ 1.203.210628 (2.3)
  • tool-mkspiffs @ 2.230.0 (2.30)
  • toolchain-riscv32-esp @ 8.4.0+2021r2-patch5
  • toolchain-xtensa-esp32s3 @ 8.4.0+2021r2-patch5

Here’s the configuration from the Arduino IDE:

  • FQBN: esp32:esp32:esp32s3
  • framework-arduinoespressif32: 3.20014.0 (2.0.14)
  • tool-esptoolpy: 1.45100.0 (4.5.1)
  • toolchain-xtensa-esp32s3: esp-2021r2-patch5-8.4.0
  • xtensa-esp32s3-elf-gcc: esp-2021r2-patch5-8.4.0
  • esptool: v4.5.1
  • ctags: 5.8-arduino11

Thanks so much in advance for any help!

Woooha, that’s an ancient version!

The latest Arduino 2.x version is 2.0.17.
You can get it with

platform = espressif32 @ 6.8.1

Try this and check whether the error still occurs.

1 Like

Thank you very much for your prompt response. You were absolutely right—everything is working perfectly now.