Course Content
Hands-On ESP32 Robotics: Build Smart Robots Step by Step

💻 Programming Code

🎯 Lesson Objective

In this lesson, students will learn:

• How to program the Smart Dustbin using ESP32
• How the ultrasonic sensor measures distance
• How the ESP32 processes the sensor data
• How the servo motor opens and closes the lid
• How the complete automation logic works

By the end of this lesson, students will be able to upload the program and run the Smart Dustbin system.


1️⃣ Complete Copy-Paste Code

Students can copy and paste the following code directly into Arduino IDE.

#include <Servo.h>

int trigPin = 5;
int echoPin = 18;

long duration;
int distance;

Servo dustbinServo;

void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

dustbinServo.attach(4);

Serial.begin(9600);
}

void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration * 0.034 / 2;

Serial.print("Distance: ");
Serial.println(distance);

if (distance < 20)
{
dustbinServo.write(90);
delay(3000);
dustbinServo.write(0);
}

delay(200);
}

2️⃣ Program Logic

The program follows a simple automation logic:

1️⃣ The ultrasonic sensor measures the distance.
2️⃣ ESP32 reads the sensor value.
3️⃣ If a hand is detected within 20 cm, the servo motor opens the lid.
4️⃣ The lid stays open for 3 seconds.
5️⃣ The servo motor closes the lid.
6️⃣ The program repeats continuously.


3️⃣ Library Explanation

 
#include <Servo.h>
 

This line imports the Servo library.

The Servo library allows the ESP32 to control a servo motor easily.

Without this library, controlling servo motors would require complex PWM programming.


4️⃣ Pin Declaration

 
int trigPin = 5;
int echoPin = 18;
 

These variables define the pins connected to the ultrasonic sensor.

Trig pin sends ultrasonic signals.
Echo pin receives the reflected signal.


 
long duration;
int distance;
 

These variables store the sensor data.

duration stores the time taken for the ultrasonic signal to return.
distance stores the calculated distance in centimeters.


 
Servo dustbinServo;
 
This creates a servo motor object.

This object allows the program to control the servo motor.


5️⃣ Setup Function Explanation

void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

dustbinServo.attach(4);

Serial.begin(9600);
}

The setup() function runs once when the ESP32 starts.

pinMode()

 
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
 

These commands configure the ultrasonic sensor pins.

• Trig pin → OUTPUT
• Echo pin → INPUT


Servo Attach

dustbinServo.attach(4);
 
This connects the servo motor to GPIO 4.

The ESP32 will send control signals through this pin.


Serial Communication

Serial.begin(9600);
 
This starts serial communication with the computer.

It allows the program to display distance values in the Serial Monitor.


6️⃣ Loop Function Explanation

The loop() function runs continuously.

This allows the dustbin to constantly check for a hand.


Step 1 — Trigger the Ultrasonic Sensor

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);

This sends a 10 microsecond ultrasonic pulse.

The sensor emits a sound wave that travels through the air.


Step 2 — Measure Echo Time

duration = pulseIn(echoPin, HIGH);
 
The pulseIn() function measures how long the echo pin stays HIGH.

This time represents the travel time of the ultrasonic wave.


Step 3 — Calculate Distance

distance = duration * 0.034 / 2;
 
This formula converts time into distance.

Explanation:

Speed of sound = 0.034 cm per microsecond

The signal travels to the object and back, so we divide by 2.


Step 4 — Print Distance

 
Serial.print("Distance: ");
Serial.println(distance);
 

This prints the distance in the Serial Monitor.

This helps during testing and debugging.


Step 5 — Check Hand Detection

if (distance < 20)
 
This condition checks if the object is closer than 20 cm.

If true, the dustbin lid opens.


Step 6 — Open the Lid

 
dustbinServo.write(90);
 

The servo rotates to 90 degrees.

This opens the dustbin lid.


Step 7 — Keep Lid Open

delay(3000);
 
The lid remains open for 3 seconds.

Step 8 — Close the Lid

dustbinServo.write(0);
 
The servo rotates back to 0 degrees.

This closes the lid.


7️⃣ Final Working Process

The Smart Dustbin works like this:

1️⃣ Ultrasonic sensor detects a hand.
2️⃣ ESP32 calculates the distance.
3️⃣ If distance < 20 cm → lid opens.
4️⃣ After 3 seconds → lid closes.
5️⃣ System continues monitoring.

This creates a fully automatic dustbin system.


8️⃣ Common Mistakes

Students may face the following issues:

⚠ Servo not moving → Check servo signal pin
⚠ Sensor always shows 0 → Check echo pin connection
⚠ Lid opens randomly → Adjust distance threshold
⚠ Servo shaking → Ensure proper power supply

Always verify wiring before debugging the code.


🚀 What Happens Next

Now that the program is uploaded and working, the final step is to test the system and explore improvements.

Scroll to Top