External Sensors

Andreana Hartadi Suliman
6 min readFeb 27, 2022

--

Welcome back to my Embedded System tutorial. This time I will gonna test out the external sensor, which is BMP280 and SRF-05 ultrasonic.

Before we begin this tutorial, don’t forget to set up the Arduino IDE first.

BMP 280

These are the component required for the tutorial.

  1. ESP-32

2. BMP-280 sensor

3. Male to Male Cable

First, attach the BMP280 sensor to the breadboard. Then using the male-to-male cable, connect VCC from the sensor to 3V3 in ESP-32, GND from the sensor to GND in ESP-32, SCL from the sensor to GPIO 22 in ESP-32, and SDA from then sensor to GPIO21 in ESP-32.

Install BMP280 library by clicking sketch > include library > Manage Library.

Search Adafruit Unified Sensor and install all Adafruit Unified Sensor. Search again BMP280 and install Adafruit BMP280 Library.

Open the Arduino IDE and paste 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(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);
}

then compile and upload it.

open the serial monitor, and the result will look like this.

Variations of Ultrasonic Sensor HC-SR04

Let’s move on to the second tutorial, which is a variation on the use of the HC-SR04 ultrasonic sensor. The ultrasonic sensor type HCSR04 is a device used to measure the distance from an object. The range of the measurable distance is about 2–450 cm. These are the component required for the tutorial.

  1. Ultrasonic sensor HC-SR04

2. LED

3. Resistor 330-Ohm

4. Buzzer

5. Male-to-Female Jumper

Look at the pins on the ultrasonic sensor HC-SR04, we only use 4 pins, namely Vcc, Trig, Echo, and GND. Install the ultrasonic sensor HC-SR04 on the breadboard, Vcc is on the far left of the ultrasonic sensor HC-SR04. Install the male to female jumper cable on the Vcc then connect it to the VIN leg on the ESP-32. Then, attach the male to female jumper cable to the Trig and then connect it to the D26 leg on the ESP-32. After that, attach the male to female jumper cable to the Echo and then connect it to the D27 pin on the ESP-32. Finally, attach the male to male jumper cable to GND and then connect it to the negative pole of the breadboard.

Install the male to female jumper cable on the GND ESP-32 that is parallel to the VIN and then connect it to the negative pole of the breadboard.

Install the buzzer on the breadboard. Attach the male to male jumper cable vertically with the negative leg of the buzzer and then connect it to the negative pole of the breadboard. Then, attach the male to female jumper cable vertically with the positive leg of the buzzer and then connect it to the D25 leg on the ESP-32.

Install the male to female jumper cable on the GND ESP-32 which is parallel to 3V3 then connect it to the negative pole of the breadboard opposite the previous one because we will install the LED on the other side of the breadboard.

Install the LED on the breadboard, the negative leg of the LED is connected to a 330 ohm resistor parallel to it vertically and the other leg of the resistor is mounted on the negative pole of the breadboard. The positive leg of the LED is connected with a male to female jumper cable vertically aligned and mounted on the D23 leg on the ESP-32.

Open the ARDUINO IDE and paste this code.

int led = 23; // set LED pinconst int trigPin = 26;const int echoPin = 27;const int buzz = 25; // set buzzer//define sound speed in cm/uS#define SOUND_SPEED 0.034long duration;float dist;void setup() {Serial.begin(115200); // Starts the serial communicationpinMode(trigPin, OUTPUT); // Sets the trigPin as an OutputpinMode(echoPin, INPUT); // Sets the echoPin as an InputpinMode(buzz, OUTPUT);pinMode(led, OUTPUT);}void loop() {// Clears the trigPindigitalWrite(trigPin, LOW);delayMicroseconds(2);// Sets the trigPin on HIGH state for 10 micro secondsdigitalWrite(trigPin, HIGH);delayMicroseconds(10);digitalWrite(trigPin, LOW);// Reads the echoPin, returns the sound wave travel time in microsecondsduration = pulseIn(echoPin, HIGH);// Calculate the distancedist = duration * SOUND_SPEED/2;Serial.print("Distance (cm): ");Serial.println(dist);// Prints the distance in the Serial Monitor use the output to indicate condition per distanceif (dist > 30){Serial.print("Distance (cm): ");Serial.println(dist);Serial.println("Nothing :(");digitalWrite(buzz, LOW);digitalWrite(led, LOW);}else if (dist <= 30 && dist > 20){Serial.print("Distance (cm): ");Serial.println(dist);Serial.println("Ada objek yang terdeteksi");digitalWrite(buzz, LOW);digitalWrite(led, HIGH);delay(1000);digitalWrite(led, LOW);delay(1000);}else if (dist <= 20 && dist > 10){Serial.print("Distance (cm): ");Serial.println(dist);Serial.println("Sudah dekat!");digitalWrite(buzz, LOW);digitalWrite(led, HIGH);delay(100);digitalWrite(led, LOW);delay(100);}else {Serial.print("Distance (cm): ");Serial.println(dist);Serial.println("terlalu dekat :(");digitalWrite(buzz, HIGH);digitalWrite(led, HIGH);}delay(500);}

First make sure the board and ports are correct. Verify the sketch to check whether there are still parts that have errors or not. Then, upload sketch. Open serial monitor to check if the code works or not. If it success, then it should be like this.

And the speaker will buzz when an object is near.

So here is the end of my tutorial, hope you enjoy!

--

--

Andreana Hartadi Suliman

If we are afraid of failure, it means we have limited our ability. — Hi, nice to see you here. Have a good day!