External Sensor ESP32

Pavita
5 min readFeb 26, 2022

Hello everyone, today I will show you my fourth experience with ESP32. In this project, I will use two external sensor, which is atmospheric pressure, temperature and relative humidity sensor and distance sensor. Before we get into the tutorial, I will show you necessary items to build the system.

  1. ESP32

2. Micro USB Cable

3. Laptop/PC with Arduino IDE app installed

4. LED Light

5. Male to male jumper cable

6. Resistor 330 Ohm

7. Buzzer

8. BMP280

9. HY-SRF05

After you have all of this, you can start the project.

Atmospheric pressure, temperature and relative humidity sensor

For this project, I use BMP280 sensor. But you can replace it with BME280. To start this project, you need to check the address of BMP280 and set the Arduino IDE.

  1. Go to Tools -> Manage Libraries

2. Search BMP280 and install Adafruit BMP280 Library

3. Arrange your breadboard until it look like this

4. Upload this code

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

After you upload the code, your serial monitor will tell the address of BMP280, and your serial monitor will look like this

This means, your BMP280 found at address 0x76

After you found the address you can start the project, but because I don’t use any output device so the arrangement of the breadboard will the same as above. What you need to do is upload this 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(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);
}

The address will be used in this part of the code:

if (!bmp.begin(0x76)) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));

You will see the result in your serial monitor and it will look like this:

Distance sensor

In this project, I use ultrasonic sensor HY-SRF05. And I also use green LED light, red LED light, and buzzer. So, in this project, if the distance between the object and the sensor is below 10cm, the red LED light and the buzzer will turn on, but if it’s above 10cm the green LED light will turn on.

This is the arrangement of my breadboard:

it’s pretty complicated right but the main point is :

The sensor will be connected to ESP32 like that. And I put red LED light on GPIO33, green LED light on GPIO32, and the buzzer on GPIO23

After setting up the breadboard, you need to upload this code:

const int trigPin = 5;
const int echoPin = 18;
const int ledPinRed = 33;
const int ledPinGreen = 32;
const int buzzer = 23;
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
long duration;
float distanceCm;
float distanceInch;
void setup() {
Serial.begin(115200); // Starts the serial communication
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinGreen, OUTPUT);
pinMode (buzzer, OUTPUT);
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculate the distance
distanceCm = duration * SOUND_SPEED/2;

// Convert to inches
distanceInch = distanceCm * CM_TO_INCH;

// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
Serial.print("Distance (inch): ");
Serial.println(distanceInch);
if (distanceCm < 10) {
digitalWrite(ledPinGreen, LOW);
digitalWrite(ledPinRed, HIGH);
digitalWrite(buzzer, HIGH);
}
else {
digitalWrite(ledPinGreen, HIGH);
digitalWrite(ledPinRed, LOW);
digitalWrite(buzzer, LOW);
}
delay(500);
}

After you upload the code, your project result will be look like this:

Also, you can see detail of how much is the distance in serial monitor.

So, this is the end of my experience with ESP32 external sensor. Thank you for your attention. In approx. one week, I will update my next experience with ESP32 :)

Pavita Andrea (18220014)

--

--