Is it too hot or just humid?: External BMP280 sensor experiment on ESP32

Hi everyone, i once had a dream of becoming a weather announcer, but i think i don’t have the guts. As the old saying goes, all roads lead to Rome, i have another chance dealing with the weather. No, i’m not becoming a meteorologist, instead I’ll show you how to “read” the weather using a microcontroller and an external sensor. This will be very exciting, so make sure you read this story through!

As you might know from my last story in Medium, that ESP32 has various sensor inside it’s own body, such as the magnet sensor and the touch sensor. But what if we need another sensor, one that the ESP32 doesn’t already have? Don’t you worry, because we actually can extend the ability of ESP32 by attaching external sensors. Here I will try to attach an external sensor, a weather sensor to detect the weather around me.

Let me introduce to y’all to weather sensor BMP280. The BMP280 is an external sensor that can read altitude, temperature and pressure. now we will read the temperature, pressure, and altitude around us using the BMP280 which is connected to the ESP32 microcontroller. This sensor could help us to keep track of the weather around us and might states phenomenons occuring around us, like the extreme winter in North America right now.

storm

Without any further do let’s take a look at the components we will need here in this experiment:

  1. ESP32 and breadboard
  2. MicroUSB Cable
  3. PC with Arduino IDE installed
  4. BMP280 sensor
left to right: BMP 280, Arduino IDE, MicroUSB Cable, ESP32 and breadboard

After you got what you need for this experiment, you can arrange the circuit. I arranged my circuit like this, shown in diagram below:

connect the ESP32 to the BMP280 through dupont wires, as usual, connect the ESP32’s ground to BMP280’s ground, connect the voltage to the voltage, and you can pick random GPIO for SDA and SCL. Here i picked GPIO 21 and GPIO 22. Pretty simple innit? Well actually the real challenge was adjusting the code, which we will discuss now

Before we got to the code, there are some configurations we need to prepare. First we need to get the library for the BMP280 sensor. Don’t worry, the library for the sensor is available at the Arduino IDE. Go to Tools > Manage libraries then type “adafruit bmp280” only one option should appear, click install and you’re good to go

The next step you gotta do is finding the example code to read temperature, pressure, and altitude. Thanks to the library we installed, there are ready-to-use example source code we could use. Here’s how to find it : File > Examples > Adafruit BMP280 Library > bmp280test

The source code would be shown like below:

#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);
Serial.println(F("BMP280 test"));
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}
/* 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);
}

Some problems actually occur when i tried using this raw source code for my BMP280, the first problem is that the connecting time was too long sometimes, resulting in time out. I managed to solve that by pressing the boot button. The second problem is that the Arduino IDE can’t detect the BMP280 and telling me to check the wires, I tried changing all the wires to the new one but nothing changed. The error message looks like this in Serial Monitor.

But thanks to a friend, I realized that i need to figure out the address of the BMP280 using this source code our lecturer referred to us https://raw.githubusercontent.com/RuiSantosdotme/Random-Nerd-Tutorials/master/Projects/LCD_I2C/I2C_Scanner.ino I tried to compile the source code in my Arduino IDE and tada!

The device found at address 0x76, so we need to adjust the source code we got from the example library. We should make sure that the BMP begin at the right adress. Fill the bmp begin part with 0x76

#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);
Serial.println(F("BMP280 test"));
if (!bmp.begin(0x76)) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}
/* 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);
}

I’ll tell you how the code works, first we need to insert the libraries we’re going to use and you need to define the SPI (Serial Peripheral Interface) pins, shown below:

#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)

The next step is initializing a variable of I2C communication protocol

Adafruit_BMP280 bmp

In the setup procedure, we need to initialize the data transfer speed in serial communication and initialize the sensor, remember to type the address

void setup() {
Serial.begin(9600);
Serial.println(F("BMP280 test"));
if (!bmp.begin(0x76)) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}

Next, we need to make the looping procedure, here we will read values from BMP280 and show it in the serial monitor. Here the code will show temperature first, then pressure, then using the pressure will calculate our altitude. IMPORTANT you need to adjust the sea level pressure to your local forecast. The sea level pressure would be different in each regions, here near the equator the sea level pressure is lower than subtropical area, The forecast near me informed that the sea level pressure here is around 1007–1008, I checked it here https://meteologix.com/id/model-charts/standard/indonesia/sea-level-pressure/20210221-0800z.html

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

The serial monitor will show something like this when you run the program. I live really close to the beach so the approximated altitude is somewhat very low and the temperature is really high, don’t worry it feels around 25 degrees here thanks to the wind.

Okay without any further do i’ll demonstrate how the code works and the result in a video:

Analysis

What i’ve learn from this module? Well there are plenty:

This module requires well-maintained equipments. Actually we need to keep our equipments well-maintained in every module, But this is the first time I actually got a problem because of a bad-maintained equipments. Make sure none of your dupont wires is bent, or else they might not perform well.

The next thing, we have to make sure the address of the external input we embed to the microcontroller. It takes me some time to realize what happened after I changed the dupont wires but the serial monitor said that no BMP280 detected, apparently I gave the wrong address in the source code to the microcontroller and it can’t find the external sensor i embedded. Just like when people are given false address toward someone’s house, they won’t be able to meet each other.

Last but not least, we need to re-check the source code. At first written in serial monitor that my altitudes is around 500 meters above the sea level, this makes me curious as I’m staying near the beach right now and the altitude should be below 10m. Then I checked the source code and written there that I need to modify the sea level pressure according to my region’s forecast, after the code is modified, the serial monitor gave the right output around 2 meters above the sea level.

That’s all for this weeks guys! See u next week, stay healthy and stay sane! (I Wayan Ananta W M Suandira / 18219038)

--

--