Help using Adafruit ESP32-S2 with platformio: BME280 i2c sensor communication issue

I’m trying to program the Adafruit ESP32-S2 with built in BME280 platform io. It works fine except for this one BME280 case of accessing the built in BME280 example. The sensor returns 0, for pretty much any query. The curious thing is that the exact same code works fine on the arudino ide.

I’ve verified the upload phase by pushing the two .bin files to address 0x10000 of the device using esptool seperately keeping all other settings equal. The .bin file created by platformio vs the .bin file created by the arduino ide: the platformio bin has the issue while the arduino bin file works fine. I did not find a way to compare the build step because arduino ide verbose build spits out miles of text, and the platformio build step is hidden even with verbose build.

In any case the issue is weird because things like blinking the LED, flashing the neopixel or serial communication gets programed correctly but the BME280 (i2c) communication is the bit that does not work.

What version of the Arduino-ESP32 core are you using in the Arduino IDE? (Tools → Boards → Board manager)

esp32 by Espressif Systems version 2.0.3

Please open a CLI to execute pio platform update espressif32 to possibly update your local Espressif32 installation (and with it, the use Arduino core). Does it work after uploading the project again?

If not, please state the full platformio.ini and src/main.cpp.

Updated, but no luck, also I believe I’m using the arduino core (that is the framework parameter in the platfromio.ini?)

platfromio.ini:

[env:featheresp32-s2]
platform = espressif32
board = featheresp32-s2
upload_protocol = esptool
framework = arduino
board_build.mcu = esp32s2
board_build.f_cpu = 240000000L
board_build.f_flash = 80000000L
lib_deps = 
	adafruit/Adafruit BME280 Library@^2.2.2
upload_speed = 921600
board_upload.flash_size = 4MB
board_build.flash_mode = dio

src/main.cpp:


#include "Arduino.h"
#include "Adafruit_BME280.h"

Adafruit_BME280 bme; // I2C

unsigned long delayTime;

void setup() {
    Serial.begin(9600);
    while(!Serial);    // time to get serial running
    Serial.println(F("BME280 test"));

    unsigned status;
    
    // default settings
    status = bme.begin();  
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
        Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
        Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
        Serial.print("        ID of 0x56-0x58 represents a BMP 280,\n");
        Serial.print("        ID of 0x60 represents a BME 280.\n");
        Serial.print("        ID of 0x61 represents a BME 680.\n");
        while (1) delay(10);
    }
    
    Serial.println("-- Default Test --");
    delayTime = 1000;

    Serial.println();
}


void loop() { 

    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" °C");

    Serial.println();

    delay(delayTime);
}

Can you check in the Arduino IDE’s library manager which exact version of the Adafruit BME280 version you’re using?

Is the sensor using I²C as its communication interface?

Adafruit BME280 version is 2.2.2, Which is identical to the arduino ide. The sensor is i2c at address 0x77. The exact same program runs fine when programed with the arduino ide, :thinking:

What exact board have you selected in the Arduino IDE → Tools setting? Could you make a screenshot of the configuration?

When you use PlatformIO with the unmodifidied I2C Scanner sketch, does it detect a device on the bus?

I tried the i2c scanner sketch. Same problem, If arduino ide is used to program, two i2c devices are found (one at 0x0B - the battery monitor, the other at 0x77 - the BME280). If platfromio is used to program, none are found.

Technically to equalize settings with the Arduino IDE you should qio here.

Can you add

build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2
platform_packages =
   framework-arduinoespressif32@https://github.com/espressif/arduino-esp32/archive/refs/heads/master.zip

to the platformio.ini and rebuild? (Once you save the platformio.ini the new core is downloaded, this may take a while until index rebuild is finished).

And to the I2C scanner sketch, please add

Serial.println("I2C pins: " + String(SDA) + " " + String(SCL));

and see if it outputs

yes the SDA SCL pins are printed as 3 and 4. Changed flashmode to qio. added that platform_package to platformio.ini. Still no luck.

That is extremely strange then. Can you do me one last favor, in the Arduino IDE File → Settings, tick the checkboxes for “Verbose Build” and “Verbose Upload”, then upload your sketch again. (You might need to remove the temp folder hinted at in the compilation log to force a full rebuild). Save the log to a textfile.

Do the same in PlatformIO by opening a CLI and executing the commands

pio run -t clean
pio run -t upload | tee compile.log

This should produce compile.log.

Upload both Arduino IDE + PlatformIO logs to e.g. pastebin.com.

Through that, some differences in compiler settings might get apparent.

Oh actually before that, when you add the print SDA and SCL code into the Arduino IDE sketch and reupload, does it print the same pins it did with PlatformIO?

Yes, The SDA and SCL pins match on arduino and platformio. The logs:

platformio log : platfromio log - Pastebin.com
arduino log : arduino log - Pastebin.com

The arduino log turned out to be greater than 500kB allowed on pastebin hence the substitution at the top. I’m not exactly sure what to look for

1 Like

Before blaming software, are pull up resistors fitted for both SDA and SCL?
Maybe Arduino enables the internal pull ups and if none are fitted external…
Internal pullups should not be relied upon for I2C bus speeds though, far too high.

As I explained in my original post, the exact same code works when compiled with the arduino ide but not platfromio. I think that rules out a hardware issue.

1 Like

Sorry, I meant pio run -t upload -v | tee compile.log here to get verbose logs :frowning: Could you regenerate?

The Arduino log looks good.

Still not working, Here is the produced log with pio run -t upload -v | tee compile.log.

Another peculiar observation, I’m using the I2cScanner sketch with some print statements inside the loop:

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


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  Serial.println("I2C pins: " + String(SDA) + " " + String(SCL));

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of the Write.endTransmisstion to see if a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknown error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }
    Serial.print("testing address ");
    Serial.println(address,HEX);
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

When programed through the Arduino IDE, and using the arduino IDEs serial monitor, the programs prints the testing address xx lines all in one quick burst, (finds the two connected devices), then holds 5 seconds before doing another loop iteration. (ie. works as expected)

However when programmed using platformio and using platformios serial monitors the testing address xx lines print one by one in slowly and does not find any connected devices.

I’ve also tested all combinations of serial monitor and programmer (platformio and arduino ide) and results are the same: it does not work when programmed using platfromio, but works fine when programmed using arduino ide.

Thanks

Official Arduino core 2.0.3 support was released 2 days ago (here).

Before going in any further detail on this, can you open a CLI and execute pio platform update espressif32 to trigger this update and then upload the I2C scanner sketch via PlatformIO again? Does it behave differently?

Specificially it could be cause the sensor is using I2C clock streching and that was only fixed in Call i2c_set_timeout in i2cSetClock by PaulZC · Pull Request #6537 · espressif/arduino-esp32 · GitHub

Still no luck. I tried a fresh project: platformio.ini

[env:featheresp32-s2]
platform = espressif32
board = featheresp32-s2
framework = arduino