Automation with ESP32
Analog and Digital Sensors
Interfacing with ESP32
• Introduction to Digital and Analog Sensors,
• Simple sensor system with ESP32
Learn Engineering by Activity with Products – LEAP
http://leap.respark.iitm.ac.in/
Apr 2025 Copyright © 2021 - 2025 | LEAP | All rights reserved 1
Board Selection and Installing Libraries
Arduino IDE
(If necessary) Install required
Include Board Select Board -> Library
Manger (Tools-> Board) (Sketch -> Include Library ->
(File -> Preferences -> Manage Libraries )
Additional Board Manager URL)
Apr 2025 Copyright © 2021 - 2025 | LEAP | All rights reserved 2
Connecting sensors to ESP32
• Always make the schematic first
• With the ESP32 board disconnected from the computer, make the connections as per the
schematic
⎯ Ensure that pin numbers in your schematic and circuit match
• Write the required code, ensuring that the pin numbers in your code match that in the circuit
• Connect the ESP32 board to PC
• Compile the code in Arduino IDE
• Upload and run!
Apr 2025 Copyright © 2021 - 2025 | LEAP | All rights reserved 3
In-class Activity 1: Temperature and Humidity Monitoring
Code: if (result == 0) {
#include <DHT11.h> Serial.print("Temperature: ");
DHT11 dht11(5); Serial.print(temperature);
void setup() { Serial.print(" °C\tHumidity: ");
// Initialize serial communication to allow Serial.print(humidity);
debugging and data readout. Serial.println(" %");
// Using a baud rate of 9600 bps. } else {
Serial.begin(9600); // Print error message based on the error
} code
void loop() { Serial.println(DHT11::getErrorString(res
int temperature = 0; ult));
int humidity = 0; }
// Attempt to read the temperature and }
humidity values from the DHT11 sensor.
int result =
dht11.readTemperatureHumidity(temp • Go to Tools -> Manage libraries
erature, humidity); • Search DHT-11 and Install Library of Dhruba Saha
// If there are errors, print the appropriate (Version 2.1.0)
error messages. • Open the code File -> Examples -> Example from
Custom libraries -> DHT11 -> ReadTempAndHumidity
Apr 2025 Copyright © 2021 - 2025 | LEAP | All rights reserved 4
In-class Activity 2 : Ultrasonic Distance Sensor
Code:
#include <HCSR04.h>
UltraSonicDistanceSensor distanceSensor(4, 5);
// Initialize sensor that uses digital pins 4 and 5
void setup () {
Serial.begin(9600);
// We initialize serial connection so that we could print values from sensor.
}
void loop () {
// Every 500 milliseconds, do a measurement using the sensor and print the
//distance in centimetres
Serial.println(distanceSensor.measureDistanceCm());
delay(500);
} • Go to Tools -> Manage libraries
• Search HCSR04 and Install Library of Martin Sosic (Version 2.0.0)
• Open the code File -> Examples -> Example from Custom libraries
-> HCSR04 -> simple
Apr 2025 Copyright © 2021 - 2025 | LEAP | All rights reserved 5
In-class Activity 3 : LDR Detects Light
Code:
const int LDR_Pin = 13; // DO pin connected to digital pin 13
void setup() {
// Initialize serial communication at 9600 baud
Serial.begin(9600);
// Set LDR pin as input
pinMode(LDR_Pin, INPUT);
}
void loop() {
// Read the digital value from the LDR sensor
int LDR_value = digitalRead(LDR_Pin);
if (LDR_value == LOW) {
Serial.println("Bright light detected");
} else {
Serial.println("Low light detected");
}
// Wait 500 milliseconds before reading again
delay(500);
}
Apr 2025 Copyright © 2021 - 2025 | LEAP | All rights reserved 6
In-class Activity 4 : Detects Touch
Code:
const int touchPin = 18; // Pin connected to the touch sensor
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(touchPin, INPUT); // Set touch pin as input
}
void loop() {
int touchValue = digitalRead(touchPin); // Read the state
of the touch sensor
if (touchValue == HIGH) { // Touch is detected
Serial.println("Touch is detected");
} else { // Touch is not detected
Serial.println("Touch is not detected");
}
delay(500); // Wait for half a second before the next reading
}
Apr 2025 Copyright © 2021 - 2025 | LEAP | All rights reserved 7
In-class Activity 5: Motion Detection by PIR
Code:
const int pirPin = 13; // Pin connected to the PIR sensor's output
int pirValue = 0; // Variable to store the PIR sensor's value
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(pirPin, INPUT); // Set PIR pin as input
Serial.println("Waiting For Power On Warm Up");
delay(20000); // Power On Warm Up Delay
Serial.println("Ready!");}
void loop() {
pirValue = digitalRead(pirPin); // Read the value from the PIR sensor
if (pirValue == HIGH) { // Check if motion is detected
Serial.println("Motion detected!"); // Print message if motion is
detected
} else {
Serial.println("No motion detected."); // Print message if no
motion is detected
} delay(1000); // Wait for 1 second before the next reading
}
Apr 2025 Copyright © 2021 - 2025 | LEAP | All rights reserved 8
In-class Activity 6: Using IR sensor
Code:
int IRSensor = 23; // connect IR sensor module to ESP32 pin 23
void setup() {
Serial.begin(9600); // Init Serial at 9600 Baud Rate.
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
}
void loop() {
int status = digitalRead(IRSensor); // Set the GPIO as Input
Serial.println(status);
if (status == 0) // Check if the pin high or not
{
Serial.println("Motion Detected!"); // print Motion detected! on the
serial monitor window
}
else {
Serial.println("Motion not Detected"); // print Motion not detected! on
the serial monitor window
}
}
Apr 2025 Copyright © 2021 - 2025 | LEAP | All rights reserved 9
In-class Activity 7: Using Sound sensor
Code: // Read analog data from the sound sensor
const int soundSensorPin = 15; // Digital int analogData =
output of KY-038 sound sensor module
analogRead(analogSensorPin);
const int analogSensorPin = 34; // Analog
output of the sound sensor module
Serial.print("Analog Data: ");
Serial.println(analogData);
void setup() {
// Add a brief delay to prevent the serial
pinMode(soundSensorPin, INPUT); // Set monitor from scrolling too quickly
the pin to input for reading digital data
delay(500);
Serial.begin(9600);
}
}
void loop() {
// Read digital data from the sound sensor
int digitalData =
digitalRead(soundSensorPin);
Serial.print("Digital Data: ");
Serial.println(digitalData);
Apr 2025 Copyright © 2021 - 2025 | LEAP | All rights reserved 10
Exercise – Design and build a useful Arduino System
• Design your own Arduino system using the available sensors and actuators
• Use at least 2 sensors and 2 output devices
• Catchy name & description
• Draw a block diagram
• Draw a schematic circuit diagram
• Design the C code
• Connect and test
• A few simple systems combining sensors and actuators that may give you
ideas:
• Monitoring weather – use DHT-11 sensors with LEDs
• Burglar alarm – using ultrasonic sensor with LEDs and buzzer
• Automatic streetlights – combine photoresistor and LEDs
Apr 2025 Copyright © 2021 - 2025 | LEAP | All rights reserved 11