Can't read BMP280 sensor data using ESP32

I'm using BMP280 with ESP32 ,library provided code. The problem is that the serial monitor says can't find the BMP280, check wiring.
code :

#include <Adafruit_BMP280.h>

/***************************************************************************
  This is a library for the BMP280 humidity, temperature & pressure sensor

  Designed specifically to work with the Adafruit BMP280 Breakout
  ----> http://www.adafruit.com/products/2651

  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

void setup() {
  Serial.begin(9600);
  while ( !Serial ) delay(100);   // wait for native usb
  Serial.println(F("BMP280 test"));
  unsigned status;
  //status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
  status = bmp.begin();
  if (!status) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
                      "try a different address!"));
    Serial.print("SensorID was: 0x"); Serial.println(bmp.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);
  }

  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}

void loop() {
    Serial.print(F("Temperature = "));
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");

    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");


    Serial.print(F("Approx altitude = "));
    Serial.print(bmp.readAltitude(1013.25)); 
    Serial.println(" m");

    Serial.println();
    delay(2000);
}

Then I tried to find the I2C address of the sensor using below code:

#include <Wire.h> //include Wire.h library
 
void setup()
{
  Wire.begin(); // Wire communication begin
  Serial.begin(9600); // The baudrate of Serial monitor is set in 9600
}
 
void loop()
{
  byte error, address; //variable for error and I2C address
  int devicecount;
 
  Serial.println("Scanning...");
 
  devicecount = 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("  !");
      devicecount++;
    }
    else if (error == 4)
    {
      Serial.print("Unknown error at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.println(address, HEX);
    }
  }
  if (devicecount == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
 
  delay(5000); // wait 5 seconds for the next I2C scan
}

But the serial monitor shows "NO I2C DEVICE FOUND"

1 Like

Which pins of the ESP32 do you use for the I2C bus ?

D21 is SDA, the green wire.
D22 is SCL, the yellow wire.

The scanner sketch that you showed works in a Wokwi simulation and detects a I2C device (Wokwi does not have a BMP280): I2CScanner_ESP32.ino - Wokwi Arduino and ESP32 Simulator

Do you think that you have a genuine BMP280 sensor ? It could be counterfeit and it could be broken.

It worked for the first time but now it doesn't. I'll try another one

Breadboards have bad contacts and jumper wires can be broken. When you look at your project, assume that everything is wrong, can check it step by step.

I2C scanner

What did the I2C scanner report?

Is the BMP a 5V device or a 3V3 device?

Hi @farhan74

Just make sure that the BMP280 breakout board is powered with 3.3V.

The BMP280's I2C address will be either 0x76 or 0x77, depending on the logic level of the board's SDO pin: LOW -> 0x76, HIGH -> 0x77.

1. Check that the 3.3V-I2C-BMP280 Breakout Board is connected as follows (Fig-1) with ESP32 using I2C Bus.


Figure-1:

2. Upload the following sketch to check the presence of the sensor at slaveAddress 0x76.

#include <Wire.h>
byte busStatus;

void setup()
{
  Serial.begin(115200);
  Wire.begin(21, 22);
  do
  {
    Wire.beginTransmission(0b1110110);//i2c);
    busStatus = Wire.endTransmission();
  }
  while (busStatus != 0);

  if (busStatus == 0)
  {
    Serial.println("FOUND!");
  }
  else
  {
    Serial.println("not found");
    while (1);
  }
}

void loop()
{
    Serial.println("Forum");
    delay(1000);
}

Output:

FOUND!
Forum
Forum
Forum
Forum
Forum
Forum

3 Likes

have tried both

I have run this on a ESP32

/* Interfacing Arduino with BMP280 temperature and pressure sensor.
 * Temperature and pressure values are displayed on 16x2 LCD.
 * This is a free software with NO WARRANTY.
 * https://simple-circuit.com/
 */

 // ESP32 SDA to D21 SCL to D22
 // ESP8266 SDA to D2 SCL to D1
 
#include <Wire.h>             // include Wire library, required for I2C devices
#include <Adafruit_Sensor.h>  // include Adafruit sensor library
#include <Adafruit_BMP280.h>  // include adafruit library for BMP280 sensor
 
// define device I2C address: 0x76 or 0x77 (0x77 is library default address)
#define BMP280_I2C_ADDRESS  0x76
 
Adafruit_BMP280 bmp280;

 
void setup() {
  Serial.begin(115200);
 
  Serial.println(F("Arduino + BMP280"));
  
  if (!bmp280.begin(BMP280_I2C_ADDRESS))
  {  
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1);
  }
  Serial.println("Found BMP280 sensor!");
}

void loop() {
  // get temperature, pressure and altitude from library
  float temperature = bmp280.readTemperature();  // get temperature
  float pressure    = bmp280.readPressure();     // get pressure
  float altitude_   = bmp280.readAltitude(1013.25); // get altitude (this should be adjusted to your local forecast)
  // print data on the serial monitor software
  // 1: print temperature
  Serial.print("Temperature = ");
  Serial.print(temperature);
  Serial.println(" °C");
  // 2: print pressure
  Serial.print("Pressure    = ");
  Serial.print(pressure/100);
  Serial.println(" hPa");
  // 3: print altitude
  Serial.print("Approx Altitude = ");
  Serial.print(altitude_);
  Serial.println(" m");
    
  Serial.println();  // start a new line
  delay(2000);       // wait 2 seconds
  
}

a run om a ESP-WROOM-32 gives

18:36:50.779 -> Arduino + BMP280
18:36:50.914 -> Found BMP280 sensor!
18:36:50.914 -> Temperature = 27.56 °C
18:36:50.914 -> Pressure    = 1020.95 hPa
18:36:50.914 -> Approx Altitude = -63.93 m
18:36:50.914 -> 
18:36:52.917 -> Temperature = 27.59 °C
18:36:52.917 -> Pressure    = 1021.01 hPa
18:36:52.917 -> Approx Altitude = -64.39 m
18:36:52.917 -> 
18:36:54.919 -> Temperature = 27.28 °C
18:36:54.919 -> Pressure    = 1021.02 hPa
18:36:54.919 -> Approx Altitude = -64.50 m

Use the attached Library with which my ESP32 and 3.3VI2CBMP280 works well.
ErriezBMX280-master.zip (3.0 MB)

Upload the sketch that is taken from the IDE's Example:

#include <Wire.h>
#include <ErriezBMX280.h>

#define SEA_LEVEL_PRESSURE_HPA      1026.25
ErriezBMX280 bmx280 = ErriezBMX280(0x76);

void setup()
{
    delay(500);
    Serial.begin(115200);
    while (!Serial) {
        ;
    }
    Serial.println(F("\nErriez BMP280/BMX280 example"));
    Wire.begin();
    Wire.setClock(400000);

    // Initialize sensor
    while (!bmx280.begin()) {
        Serial.println(F("Error: Could not detect sensor"));
        delay(3000);
    }
    
    Serial.print(F("\nSensor type: "));
    switch (bmx280.getChipID()) {
        case CHIP_ID_BMP280:
            Serial.println(F("BMP280\n"));
            break;
        case CHIP_ID_BME280:
            Serial.println(F("BME280\n"));
            break;
        default:
            Serial.println(F("Unknown\n"));
            break;
    }
    bmx280.setSampling(BMX280_MODE_NORMAL,    // SLEEP, FORCED, NORMAL
                       BMX280_SAMPLING_X16,   // Temp:  NONE, X1, X2, X4, X8, X16
                       BMX280_SAMPLING_X16,   // Press: NONE, X1, X2, X4, X8, X16
                       BMX280_SAMPLING_X16,   // Hum:   NONE, X1, X2, X4, X8, X16 (BME280)
                       BMX280_FILTER_X16,     // OFF, X2, X4, X8, X16
                       BMX280_STANDBY_MS_500);// 0_5, 10, 20, 62_5, 125, 250, 500, 1000
}

void loop()
{
    Serial.print(F("Temperature: "));
    Serial.print(bmx280.readTemperature());
    Serial.println(" C");

    if (bmx280.getChipID() == CHIP_ID_BME280) {
        Serial.print(F("Humidity:    "));
        Serial.print(bmx280.readHumidity());
        Serial.println(" %");
    }

    Serial.print(F("Pressure:    "));
    Serial.print(bmx280.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print(F("Altitude:    "));
    Serial.print(bmx280.readAltitude(SEA_LEVEL_PRESSURE_HPA));
    Serial.println(" m");

    Serial.println();

    delay(1000);
}

Output:

Temperature: 28.72 C
Pressure: 1008.07 hPa
Altitude: 150.51 m

a run of the library/sketch recommended by @GolamMostafa gives

18:41:47.630 -> Erriez BMP280/BMX280 example
18:41:47.731 -> 
18:41:47.731 -> Sensor type: BMP280
18:41:47.731 -> 
18:41:47.731 -> Temperature: 22.31 C
18:41:47.731 -> Pressure:    1021.17 hPa
18:41:47.764 -> Altitude:    41.81 m
18:41:47.764 -> 
18:41:48.739 -> Temperature: 22.30 C
18:41:48.739 -> Pressure:    1021.07 hPa
18:41:48.739 -> Altitude:    42.68 m

also works for a BME280

18:47:06.421 -> Erriez BMP280/BMX280 example
18:47:06.559 -> 
18:47:06.559 -> Sensor type: BME280
18:47:06.559 -> 
18:47:06.559 -> Temperature: 22.18 C
18:47:06.559 -> Humidity:    46.69 %
18:47:06.559 -> Pressure:    1021.06 hPa
18:47:06.559 -> Altitude:    42.76 m
18:47:06.559 -> 
18:47:07.550 -> Temperature: 22.23 C
18:47:07.550 -> Humidity:    46.65 %
18:47:07.550 -> Pressure:    1021.05 hPa
18:47:07.550 -> Altitude:    42.84 m
18:47:07.550 -> 

1 Like

This Library looks a very good one. Adafruit's Library works well with UNO and does not work with ESP32.

works fine with my new sensor. Why have you set sea level pressure at 1026.25 instead of 1013.25?

I run the example from the Library. The value could be for the Geographical area of the Library Author.

ok, thanks. I'm basically building a spirometer using this sensor. for that I need to remove the atmospheric pressure and just take the pressure applied by the blow. Can u help?

1 Like

This is an innovative idea by which I am inspired.

not a solution but have you solved this problem(it's your post):

is it an actual, factual BMP280, or is it a BME280?

is it marked BME/BMP280, like all of mine?

have you tried the BME280 library?

I tried that yesterday, and just like that my "BMP280" works. sort of. the temperature seems right. the pressure is way off ( 25.92 inches of mercury ), and it shows my "altitude" ( which is properly elevation when you are on the ground ) as 45,000 meters, but it is no longer inert

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