Embedded System Project 4: ESP32 External Sensor

Fernaldi Fauzie
6 min readFeb 18, 2021

--

Alright! Again, we will play with sensor, but this time, not internal sensor from ESP32 itself but external sensor instead! I feel like this project will bring much insights to me because if I can make us of many external sensors well, I think it will bring me to many possibilities which make me pretty excited! Isn’t that pretty interesting?

First, I would like to explain about my sensor that will be used in this project. Although I bought it from online shop in the name of BME280, when I checked the pin, its pins are not VCC, GND, SDI, SCK, CSB, SDO which should be it if my sensor is BME-280 (based on reference that my lecture gave). Also, it isn’t BMP180 as mine has 6 pins instead of 4 pins. So, I tried to discuss it with my System Embedded Assistant, which lead me to conclusion that my sensor is BMP280, as its pins are VCC, GND, SCL, SDA, CSB and SDO.

So, in this project I would like to make use of BMP280 sensor to read temperature, pressure and estimate altitude using Arduino IDE software. This sensor connects directly to a microcontroller via I2C or SPI.

What are we waiting for? Let’s get started!

For this project, I am using this circuitschools.com as reference! Of course, we will make it with our own version!

First, we need these components :

  • ESP32 Development Board
  • BMP280 sensor
  • Some jumper wires (I’m using male to male)
  • Micro USB cable
  • Breadboard
Schematic Diagram

So I tried to make my own schematic diagram. Unfortunately, I couldn’t find the exact sensor I used which is BMP280 that has 6 pins (as there are only 4 pins in schematic diagram). But actually there is no serious matter with it, because in general they are actually same as I only use 4 from 6 pins which all of them are available in that sensor in schematic diagram.

Schematic diagram from https://www.circuitschools.com/interfacing-bmp280-with-esp-32-on-i2c-with-errors-and-solutions/

Next, let’s start with the code that we may get from File > Examples > BMP 280 library. If you don’t find it in your Arduino IDE, you need to download and install, Adafruit BMP280 library, which can be achieved by

  • Sketch > Include Library > Manage Libraries…
  • Search for “Adafruit BMP280 Library”
  • Click install
  • If you haven’t install Adafruit Unified Sensor Library, install it by pressing “Install all”!

Finally, we have the code that we can access from File > Examples > Adafruit BMP280 Library > bmp280test

But we are not done yet! Next, we need to look for the I2C address of BMP280 so that we can add it to the code to make it works! We have to change if (!bmp.begin()) to if (!bmp.begin(<I2C address>)). To get the I2C address, I am using this code from randomnerdtutorials.

/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
#include <Wire.h>

void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("\nI2C Scanner");
}

void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; 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.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow 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);
}
Result

With this, we know that I2C device found at address 0x76. Finally, we can finish our code. As I get 0x76 as the result, our last code would be seen like this :

/***************************************************************************
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);
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);
}
My ESP32 connected with BMP280

Finally, we can start the the experiment!

Result

It seems that my BMP280 is working, because its temperature increase as I am touching it with my hand. (26.78 C to 28.23 C).

So, after that I try to do another testing, but this time I make it in gif! Seems like this time my body temperature is not really hot so the difference not really big this time (only from around 27.07 C to 27.31 C)

Result in live version

For your information, if you find the error “Could not find a valid BMP280 sensor, check wiring!”, that means you forgot to add the I2C address of BMP280 to the code. Mine is 0x76 so I made the if (!bmp.begin()) to if (!bmp.begin(0x76)). This is important!

In conclusion, we can use BMP280 as external sensor to read temperature, pressure and estimate altitude using Arduino IDE software. I2C address of BMP280 is required to make BMP280 works as intended. We can see the result from serial monitor in Arduino IDE.

I think, that will end my story about BMP280, the external sensor which is used to read temperature, pressure, and estimate altitude. I hope that this experience can give you guys inspiration, knowledge, and insight! There are still many opportunities and chances from ESP32 that can be explored. Make sure to keep curious and see you in the next story!

--

--

No responses yet