ESP32 I2C timeouts in PIO but works ok in Arduino

I’m using Adafruit’s HUZZAH32 Feather (ESP32 WROOM) and three of their MPR121 capacitive touch sensor breakouts with I2C communication. The MPR121 breakouts have distinct I2C addresses, I2C level shifting and a regulator (it’s a 3V chip: to use 3V or 5V sources) and include the required SDA/SCL pullups. The HUZZAH does not have SDA/SCL pullups.

On PIO I’m using Core v5.2.3 with Espressif32 v3.4.0. On Arduino I’m using IDE v.1.8.15 with Espressif v2.0.1 (all latest stable releases for each platform and IDE). On both I’m using Adafruit BusIO v1.9.3 and Adafruit MPR121 v1.1.1

The following simple sketch reads the FilteredData of each sensor and prints it out.

The HUZZAH’s V-Out pin serves 3.3V & 500mA from its regulator, but the ESP32 draws about half of this. I could not find how much power the MPR121’s draw but it would seem that the 250mA remaining from the HUZZAH32 may not be enough for all three: when powering them from the V-Out, every now and then a timeout in the I2C occur (represented as negative reading from the cap sensor) when compiled with Arduino. But, when compiling the same sketch with PIO, the faults appear with nearly every attempt to read the sensors.

If powering the MPR121’s directly from battery (4.1V) or USB (5V), the timeouts disappear if compiled with Arduino but remain there when compiled with PIO.

The erratic behavior when compiled with PIO includes creating the sensor’s instance with cap.begin(), which sometimes sends this error to the serial monitor: [E][esp32-hal-i2c.c:1434] i2cCheckLineState(): Bus Invalid State, TwoWire() Can’t init sda=1, scl=1. Creation of the sensors’ instances never fails when compiled with Arduino

Is there something I could / should change in PIO’s config to fix this?

Thanks!

platformio.ini:

[env:featheresp32]
platform = espressif32
board = featheresp32
framework = arduino
monitor_speed = 115200
lib_deps = adafruit/Adafruit MPR121@^1.1.1

main.cpp:

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

Adafruit_MPR121 cap1 = Adafruit_MPR121(); 
Adafruit_MPR121 cap2 = Adafruit_MPR121(); 
Adafruit_MPR121 cap3 = Adafruit_MPR121();
Adafruit_MPR121 cap4 = Adafruit_MPR121();

boolean c5A, c5B, c5C, c5D = false;

void setup() {
  Serial.begin(115200);
  Wire.begin(23,22);  
  Serial.println("delaying..."); delay(500);
// Default address is 0x5A, if tied to 3.3V its 0x5B, If tied to SDA its 0x5C and if SCL then 0x5D
  if (!cap1.begin(0x5A)) {Serial.println("5A Not Found"); } else {Serial.println("5A Good"); c5A = true;}
  if (!cap2.begin(0x5B)) {Serial.println("5B Not Found"); } else {Serial.println("5B Good"); c5B = true;}
  if (!cap3.begin(0x5C)) {Serial.println("5C Not Found"); } else {Serial.println("5C Good"); c5C = true;}
  if (!cap4.begin(0x5D)) {Serial.println("5D Not Found"); } else {Serial.println("5D Good"); c5D = true;}  
  delay(2000);
}

void loop() {
  if (c5A) {
    Serial.print("5A: ");
    for (int i = 0; i < 12; i++)
      {Serial.print(cap1.filteredData(i)); Serial.print(",");}
    Serial.println();  }
  if (c5B) {
    Serial.print("5B: ");
    for (int i = 0; i < 12; i++)
      {Serial.print(cap2.filteredData(i)); Serial.print(",");}
    Serial.println();  }
  if (c5C) {
    Serial.print("5C: ");
    for (int i = 0; i < 12; i++)
      {Serial.print(cap3.filteredData(i)); Serial.print(",");}
    Serial.println();  }
  if (c5D) {
    Serial.print("5D: ");
    for (int i = 0; i < 12; i++)
      {Serial.print(cap4.filteredData(i)); Serial.print(",");}
    Serial.println();  }
  Serial.println();
  delay(500);
}

PlatformIO is using Arduino-ESP32 v.1.0.6, not v2.0.1. This is an open issue. The difference in the used core version might explain your I2C problem in one IDE but not in the other.

You can get a technical preview of Arduino-ESP32 v2.0.0 in PlatformIO with the following: Change your platformio.ini to

[env:featheresp32]
platform  = https://github.com/platformio/platform-espressif32.git#feature/arduino-idf-master
platform_packages =
   framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.0
board = featheresp32
framework = arduino
monitor_speed = 115200
lib_deps = adafruit/Adafruit MPR121@^1.1.1

does it magically work?

Hi @maxgerhardt . Thanks for your response. I was not aware that PIO was using v.1.0.6 of Arduino-ESP32. Indeed the errors from I2C exist when using that version even in Arduino IDE (I opened that issue back in Sep : MPR121 with HUZZAH32 : noise from cap sensor readings via I2C · Issue #5635 https://github.com/espressif/arduino-esp32/issues/5635) and was fixed with the refactor of I2C in release 2.0.1.

I copied your suggested platform.ini, installed GIT for windows https://git-scm.com/ and got this compilation error:

Linking .pio\build\featheresp32\firmware.elf
xtensa-esp32-elf-g++: error: Tests.map: No such file or directory
**** [.pio\build\featheresp32\firmware.elf] Error 1*
============================================ [FAILED]

What am I missing?

…that should not happen – did you add anything to the platformio.ini besides the content above? I can compile your example code above with my platformio.ini with no errors.

@maxgerhardt I think it´s the GIT manager. It has a lot of installations options (of which I understood about half…first time using it) . I found here GIT library download - Libraries - PlatformIO Community a conversation on GIT Libraries download issues with these recommendations: check PATH and reinstall PIO.

Are you using Git (git-scm.com) or Git for Windows ? Any pointers on which install options to change from their defaults (i.e. before I reinstall both PIO & the Git manager).

did you add anything to the platformio.ini besides the content above?

Nope. Cut & Pasted it.

Thanks

The first one.

Are you sure you’ve selected the right project and environment (featheresp32) using the project environment switcher?