top of page

ESP32 BMP280 Sensor

In this article, I will tell you about my experience using ESP32 external sensor, BMP280. This project is my fourth ESP32 project and it was given by my lecturers.


Prerequisites

I used Arduino IDE v1.8.19 in Windows 11 64-bit to write the code and upload the code to ESP32, you can download it in here. Just click on download, make sure it the right version and OS, and it will download a zip file. After the download is finished, extract the zip file and open the arduino.exe. It will open the IDE. You might use other IDE, but I recommend you to use Arduino IDE.


I also used some component that I bought at Jaya Plaza, electronic market located in Bandung. But you can get this component on online marketplace.

  1. an ESP32 microcontroller

  2. a Micro-USB type B to USB type A (Male-to-Male)

  3. a BMP280 Sensor

  4. Four Jumper Wires (Male-to-Male)

  5. Breadboard

Step 1: Place ESP32 and BMP280 on Breadboard

Don't remind me that I soldered the BMP280 backwards. Can't see the pin name lol.

ree

Step 2: Connect ESP32 and BMP280 with Jumper Wires

My BMP280 sensor has a name for each pin: VCC, GND, SCL, SDA, CSB, SDO. I connected the GND to GND in ESP32, VCC to 3V3, SCL to GPIO22, and SDA to GPIO21 using a jumper wires male-to-male. You can use a female-to-female if you don't use a breadboard.

ree

Step 3: Install Adafruit BMP280 Library

There are many BMP280 library, but I think Adafruit library is the most popular ones. You can find many forum that talk about it, so I used it. To Install this library, all you need to do is click Sketch > Include Library > Manage Library > Search BMP280 > Install the Adafruit BMP280 Library. I used version 2.6.1, I think it's the latest one. You need to install other Adafruit library to use this library, it will popup what you need to install.

ree

Step 4: Write and Upload the Code

The good news is Adafruit BMP280 library provides an example code to test the sensor, so we don't have to write the code from scratch. All you need to do is click Files > Examples > Adafruit BMP280 Library > bmp280test. BOOM! The code is here, but it's not ready yet. This is the code that you'll get:

#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)); /* Adjusted to local forecast! */
    Serial.println(" m");

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

So the highlighted code:

status = bmp.begin();

It should contain an I2C address. Adafruit says that the address of BMP280 is within 0x56-0x58 as you can see under the highlighted code. Unfortunately in my case, it's not. So, I googled the case. I visited every forum, every website, and every videos. Then I know exactly what to do. All I need to do is search for my I2C address and I found the how to. There is this code where you can find your I2C address after you upload it to your ESP32 (I used it while the BMP280 circuit is still connected to ESP32). This is the code:

// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
 
#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...");
 
  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);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
 
  delay(5000);           // wait 5 seconds for next scan
}

So the code will print the I2C address to serial monitor. Well, at least in my case. Apparently, I found others who used this method and still can't find the address. Maybe the sensor was broken, or maybe it was not soldered well, or the circuit, or it just didn't compatible with the code. Back to the topic again, so the output says my I2C address is 0x76. Then, I reupload the BMP280 test code with the address.


ree

status = bmp.begin(0x76);

and...BOOM!!

Step 5: The Output
ree

 
 
 

Comments


bottom of page