Project 4 : Experiment with External Sensor Module

Gresya Leman
12 min readFeb 25, 2022

Hello everyone!
Welcome to the fourth chapter of me learning on how to use ESP32. In this part, I’m going to show you how I experimented with external sensor modules on ESP32.

I decided to use BMP280 sensor module (temperature, pressure, and altitude sensor), KY-037 sound detection sensor module, and LDR light sensor module.

Necessary components

For the tutorials there are few similar necessary components such as written below.

ESP32+Breadboard
In this project I used ESP32 DevKit V1, but you can use any kind of ESP32. It’s better to have your ESP32 secured on a breadboard to make the arrangement easier to do.

ESP32 on a breadboard

Micro USB Cable
This cable will be used as a connector between our device and the ESP32.

Micro USB Cable

Laptop/PC
Use the device of your choice, in this project I used my laptop which is Asus TUF Gaming A15. Make sure the device’s already set up. If it’s not yet been set up, click here for a tutorial on setting up your device.

Laptop

Male-to-Male Jumper cable

In this project I used the male-to-male jumper cable because I secured the ESP32 on the breadboard. But you can use female-to-female jumper cable to connect the sensor directly to the ESP32. If so, you won’t need the breadboard.

Male-to-Male Jumper Cable

PART 1 : BMP280 Sensor Module

From 3 options that my lecturer gave, I decided to try the BMP280 sensor module since it’s way more affordable than the BME280 sensor module but looks almost identical and has pretty much similar functions. The only difference is that BMP280 sensor module can’t detect humidity.

Additional components required

For this part, I only needed 4 jumper cables and the sensor apart from the ESP32 and the breadboard.

BMP280 Sensor

My lecturer said I could choose any of the sensors, but I already bought the BMP280 so I just used it. You can choose any other sensors such as BME280 or BMP180. The only difference would be the module required to install on the Arduino IDE.

BMP280 sensor

Arrangement on the breadboard

Make sure the ESP32 is secured on the breadboard. Then check carefully the names of the leg on the BMP280 sensor. Connect it to the compatible ports on the ESP32. Here, I connected it as the table and picture below.

Where to connect from BMP280 sensor to ESP32
Schematic (www.circuitschools.com)
My final arrangement on the breadboard

Code the program

After the arrangements in the circuit is completed, open the Arduino IDE.

Install the BMP280 module on the Arduino IDE by going to Sketch > Include Library > Manage Libraries…

A new window will pop up, then search Adafruit BMP280 then install the module.

Then search Adafruit Unified Sensor, scroll to the bottom, then install the module. Then click close on the bottom of the window.

After the setup is done, go to File > Examples > Adafruit BMP280 Library > bmp280test

A new window containing the code will pop up, then go to the void setup() part, on the part where the code says ‘status = bmp.begin()’, type in 0x76 in the middle of the parentheses as seen in the picture below. This indicates the I2C address of the sensor.

Compile the code and wait until it says ‘Done compiling’.

Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.

Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.

After it’s done uploading, go to Tools > Serial Monitor

A new window will pop up, then set the serial to 9600 baud. Wait for any writing to pop up, if it’s not showing, then press the EN button on the ESP32. The writings that shows up in that window will be the values read by the sensor. The readings should seem like the picture below.

Here’s a video of me trying it on my laptop.

Struggles & how I resolved them

Some struggles I went through when trying this first part is that when I first tried it, I didn’t know that there should be I2C address attached to the bmp.begin() so I just compiled and uploaded it to my ESP32. But then when I opened the serial monitor, I was left with empty white screen.

I tried pressing the EN button on my ESP32 but this showed up instead.

I tried looking it up online, then I found this website : https://www.xtronical.com/bmp280andesp32/

I tried running the code to figure out where the I2C address is. Then I got this.

After that, I tried putting the 0x76 inside the bmp.begin() as the website says so that it turned into bmp.begin(0x76) and it worked.

PART 2 : KY-037 Sound Detection Sensor Module

My lecturer said that I could try different kinds of external sensors, so I tried buying the KY-037 sound detection sensor module and decided to experiment with it a little bit. Here, I don’t want to just show the sensor readings, but I wanted to utilize it on a simple program with LED lights as output.

Additional components required

For this part, I needed 11 jumper cables for 3 LED lights in the whole circuit.

KY-037 Sound Detection Sensor Module

You can use any other sound detection sensor instead, like KY-038 sensor module or LM393 sensor module.

KY-032 Sound Detection Sensor

330 Ohm Resistor (3 pcs)

This resistor will be used for the LED lights. Use it accordingly to how many LED lights you’ll be using.

330 Ohm Resistor

5mm LED lights (3pcs)

Here I just used colorful LED lights to make a little variation.

5mm LED Lights

Arrangements on the breadboard

Make sure the ESP32 is secured on the breadboard. Then attach the LED lights with some space between them to put the resistors and jumper cables.

Attach the resistors to a spot next to the positive leg of the LED lights and connect it to a spot on an empty row to connect to the jumper cable later on.

Connect a jumper cable from a spot near the other end of the resistor to a spot next to the pin on the ESP32 that are used. Here I used GPIO 21, 22, and 23 for the LED lights.

Then connect another jumper cable from a spot next to the negative leg of the LED lights and connect it to the negative side on the board.

Attach a jumper cable from the spot next to GND pin and attach it to the negative side of the board, connecting to other jumper cables on the negative side of the board.

Then attach the KY-037 sensor on the breadboard. Attach the 4 jumper cables according to the table below.

Where to connect from KY-037 Sensor to ESP32
My arrangement according to the table

Finally connect the micro USB cable that connects the ESP32 to the laptop and the whole circuit should look like this.

Code the program

Open the Arduino IDE and make a new file. Save the file where ever you like it.

Type in the code as below.

//initialize the pins used
const int led1 = 21;
const int led2 = 22;
const int led3 = 23;
const int s_digital = 2;
const int s_analog = 4;
//initialize variables to keep the value of analog and digital reading
int val_digital;
int val_analog;
void setup() {
//setup input and outputs
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(s_digital, INPUT);
}
void loop() {
//set the values to analog and digital reading
val_digital = digitalRead(s_digital);
val_analog = analogRead(s_analog);
//print the values
Serial.print(val_analog);
Serial.print("\t");
Serial.println(val_digital);
if(val_digital == HIGH){
//turn on all LED lights
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
delay(500);
}else{
//turn off LED lights
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}
}

Compile the code and wait until it says ‘Done compiling’.

Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.

Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.

Now all three of the LED lights will turn on simultaneously when the sensor detects any sound. The result will show up as in the video attached below.

Struggles & how I resolved them

On my first attempt after uploading the code, the LED lights all turned on immediately even though my room was quiet. So I tried looking it up in the internet and found out from this website : https://diyi0t.com/sound-sensor-arduino-esp8266-esp32/#Functionality_of_the_KY-038_and_KY-037_Sound_Sensor_Module that I have to adjust the potentiometer first which hold the threshold for the digital output pin.

The potentiometer on the KY-027 sensor module

To do this adjustment, the sensor must be in a connected state and the code must be uploaded first. The adjustment also has to be done in a quiet room. Since my sensor was too sensitive and LED2 on the sensor was turned on all the time, I had to turn the potentiometer counter clockwise to reach the point where the LED2 would turn off. Adjust it to the point where the LED2 would turn on if the potentiometer is turned clockwise at the slightest bit. Now just test the settings and the LED2 should turn on when any sound is made around it.

Part 3 : LDR Light Sensor Module

I felt a little unsatisfied after trying the sound sensor module, so I decided to experiment with another sensor module. Here I used a light sensor module.

Additional components required

For this part, I only needed 6 jumper cables and I used a buzzer as an output instead of LED lights.

LDR Light Sensor Module

I don’t really know what it’s called but that was the one I found at the store I was buying from.

LDR Light Sensor Module

Piezo buzzer

Make sure that the buzzer can work at 3,3V voltage.

Piezo Buzzer

Arrangements on the breadboard

Make sure the ESP32 is secured on the breadboard. Then attach the buzzer on the breadboard with the positive leg on the right.

Connect the buzzer’s positive leg to GPIO pin 22 using a jumper cable.

Using another cable, connect the buzzer’s negative leg to the breadboard’s negative side.

After that, connect the GND pin on the ESP32 to the negative side of the breadboard.

After the buzzer’s done, attach the light sensor module on the breadbaord.

Then connect the pins on the sensor module according to the table and picture below.

Where to connect from LDR light sensor to ESP32
My final arrangement on the breadboard

Code the program

Open the Arduino IDE and make a new file. Save the file where ever you like it.

Type in the code as below.

//initialize the pins used
const int buzzer = 22;
const int buzzer_ch = 0;
const int s_analog = 4;
//set the threshold for each light condition
const int bright = 1000;
const int normal = 2200;
const int dim = 3000;
//variable for analog reading
int val_analog;
void setup() {
// setup input and outputs
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
pinMode(s_analog, INPUT);
}
void loop() {
//set the values of analog reading
val_analog = analogRead(s_analog);
//print the analog reading values
Serial.print(val_analog);
//attach buzzer pin to PWM channel 0
ledcAttachPin(buzzer, buzzer_ch);
//if it's bright, buzzer plays note C on 5th octave
if(val_analog <= bright){
Serial.println("-> bright");
ledcWriteNote(buzzer_ch, NOTE_C, 5);
delay(500);
}
//if it's normal, buzzer plays note G on 4th octave
else if(val_analog > bright && val_analog <= normal){
Serial.println("-> normal");
ledcWriteNote(buzzer_ch, NOTE_G, 4);
delay(500);
}
// if it's dim, buzzer plays note C on 4th octave
else if(val_analog > normal && val_analog <= dim){
Serial.println("-> dim");
ledcWriteNote(buzzer_ch, NOTE_C, 4);
delay(500);
}
//if it's dark, the buzzer turns off
else if(val_analog > dim){
Serial.println("-> dark");
digitalWrite(buzzer, LOW);
delay(500);
}
delay(100);
}

Compile the code and wait until it says ‘Done compiling’.

Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.

Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.

Now the buzzer will make a C on 5th octave when it’s bright, G on 4th octave when ‘normal’, and C on 4th octave when it’s dim, and it’ll turn off when it’s dark. The result will show up as in the video attached below.

Struggles & how I resolved them

Some struggles I went through when trying this light sensor modification is to find a substitute for the tone() function on Arduino for ESP32. I searched and stumbled upon this. So I tried it and it worked. I also struggled finding the right thresholds on the sensor but I eventually made it through few experiments and found the right thresholds so that the result is exactly how I wanted it to be.

This is the end of the tutorial and I hope this helps!

See you in my next projects!

--

--