Project IV : ESP32 External Sensor

Rayhan Naufal Luthfi
4 min readFeb 27, 2022

Welcome to my fourthproject of ESP32. This project is a form of implementation of the knowledge that i got from Embedded System course. In this next project, we’re going to learn to use an External Sensor, which is BMP280 Sensor that capture Pressure, Temperature, and Altitude.

#Required Components/Hardware

First things first we need to know what components are needed for this project. These components are just the examples I used for this project, you can use different types of components that you can find easily on the internet.

  1. ESP32 Development Kit
  2. Male to Male Wire Cable
  3. Micro USB Cable
  4. Breadboard
  5. LED Lamp
  6. Resistor 330 ohm
  7. BMP280 Sensor

#Required Software

In this project we use an external sensor, so we need to install another library for BMP280. You can go to Tools -> Manage Libraries, then search Adafruit BMP280. Click Install -> Install All.

#Assemble The Circuit

We can use this schematic for the circuit

Assemble the components on the breadboard and the circuit should be like this

And for the code you can copy and paste this code below. Dont forget to verify and upload the code.

#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(115200);
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(0x76);
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);
}

After upload the code, open your serial monitor and change the baud rate to 115200. This is how it will look like

#BMP280 Sensor + LED

Now we can combine this sensor with LED. BMP280 can measure temperature. So, we can make this feature to turn on the LED. For this experiment, i made the code to turn on the LED when the temperature exceed 30 degrees Celsius. This is the circuit that i used.

And this is the code

#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);
const int LED = 5;
void setup() {
Serial.begin(115200);
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. */
pinMode(LED, OUTPUT);
}
void loop() {
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(" *C");

if (bmp.readTemperature() >= 30){
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}
delay(1000);
}

To adjust the temperature we can put our hand on the sensor. You can see that the temperature will increase slowly. And when it reach 30 degrees, the LED will turn on. This is the result.

And so that’s my experience in working on the fourth project. Thank you for reading this article and I hope this article can be helpful for people who want to start trying to learn ESP32.

Rayhan Naufal Luthfi
Information System Technology ITB 2020
18220048

--

--