Intro to Embedded System with ESP32 [External Sensor]

Reinhart Lim
4 min readFeb 25, 2023

--

Let’s continue the topics from the last blog about sensors but this time it’s about the External Sensor of ESP32. Several sensors are ESP32 compatible. The sensors typically operate on 3.3V, but if you wish to use them at 5V, a logic-level converter is required to change the voltage. These are some sensors that can be used with ESP32:

Sensors

In this project, I’ll use the BMP280 sensors. It’s a sensor that can detect temperature, pressure, and altitude.

Let’s get started !!

— Component Required

  • ESP32
  • Jumper wires
  • BMP280

When you buy the sensors, they have to be soldered. You can ask the seller to help you or do it yourself.

Here’s the schematic result to create this project:

Because the sensor uses I2C communication protocol, these are the pins that can connect the sensor and ESP32:

https://randomnerdtutorials.com/esp32-bme280-arduino-ide-pressure-temperature-humidity/

— Code

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

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

readTemperature(), readPressure(), and readAltitude() functions will detect the environment variable and convert it to a readable value.

These are the code I’ll use to run this project. But before running the code make sure you already download the Adafruit BMP280 Library from the library manager in the Arduino IDE.

BMP280 Library

After running the code, we’ll see the result at the Serial Monitor in Arduino IDE. Here’s the result I got when the project was created:

Serial Monitor Result

— Error Encounter

  • Don’t forget to adjust the baud value in the serial monitor according to your code.
  • In the bmp.begin() function don't forget to define the address 0x76, this error wastes my time so much 😭

By following the step above you’ve already finished this simple project about ESP32 External Sensor. Congrats 🎉. This is simply a simple experiment with External Sensor; you may try more complex circuits and programs.

I hope this blog can be useful for those who are learning and getting started using ESP32. That’s all for this project, stay tuned for the next experiment with ESP32 !!

— Bonus

This is a sneak peek of the next 2 projects by combining the LCD display and external sensor, I’ll cover the topic and code in the next topic. Stay tuned !!!

--

--