Course Content
IoT Engineering Course using ESP32 with 12 Real-World Projects

🎯 Objective of This Lesson

In this lesson, students will:

  • Upload complete code to ESP32

  • Understand how ultrasonic sensors measure distance

  • Control a servo motor

  • Display dustbin status on Serial Monitor

  • Detect whether the dustbin is FULL or NOT


🔌 Pin Configuration (Same as Lesson 9.1)

Ultrasonic Sensor 1 (Hand Detection)

  • TRIG → GPIO 5

  • ECHO → GPIO 18

Ultrasonic Sensor 2 (Garbage Level)

  • TRIG → GPIO 17

  • ECHO → GPIO 16

Servo Motor

  • Signal → GPIO 13


🧾 Complete ESP32 Code

#include <ESP32Servo.h>

// ----------- Pin Definitions -----------
#define TRIG1 5
#define ECHO1 18

#define TRIG2 17
#define ECHO2 16

#define SERVO_PIN 13

Servo myServo;

long duration;
float distanceHand;
float distanceGarbage;

// ----------- Setup Function -----------
void setup() {

Serial.begin(115200);

pinMode(TRIG1, OUTPUT);
pinMode(ECHO1, INPUT);

pinMode(TRIG2, OUTPUT);
pinMode(ECHO2, INPUT);

myServo.attach(SERVO_PIN);
myServo.write(0); // Lid initially closed

Serial.println("Smart Dustbin System Started...");
}

// ----------- Distance Function -----------
float getDistance(int trigPin, int echoPin) {

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

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

duration = pulseIn(echoPin, HIGH);

float distance = duration * 0.034 / 2;

return distance;
}

// ----------- Main Loop -----------
void loop() {

// -------- Hand Detection --------
distanceHand = getDistance(TRIG1, ECHO1);

Serial.print("Hand Distance: ");
Serial.print(distanceHand);
Serial.println(" cm");

if (distanceHand > 0 && distanceHand < 20) {
Serial.println("Hand Detected - Opening Lid");
myServo.write(90); // Open lid
delay(3000);
myServo.write(0); // Close lid
}

// -------- Garbage Level Detection --------
distanceGarbage = getDistance(TRIG2, ECHO2);

Serial.print("Garbage Distance: ");
Serial.print(distanceGarbage);
Serial.println(" cm");

if (distanceGarbage > 0 && distanceGarbage < 8) {
Serial.println("Status: Dustbin is FULL");
} else {
Serial.println("Status: Dustbin is NOT Full");
}

Serial.println("-----------------------------");

delay(1000);
}

🧠 Code Explanation (Step-by-Step)


1️⃣ Including Library

 
#include <ESP32Servo.h>
 

This library allows ESP32 to control servo motors using PWM signals.


2️⃣ Pin Definitions

 
#define TRIG1 5
#define ECHO1 18
 

We define pins for both ultrasonic sensors and servo so code becomes readable and organized.


3️⃣ Servo Object Creation

 
Servo myServo;
 

This creates a servo object to control the motor.


4️⃣ Setup Function

 
Serial.begin(115200);
 

Starts Serial communication so we can see data in Serial Monitor.

 
myServo.attach(SERVO_PIN);
myServo.write(0);
 
  • Attach servo to GPIO 13

  • Set initial position to 0° (Lid Closed)


5️⃣ Distance Calculation Function

 
float getDistance(int trigPin, int echoPin)
 

This is a reusable function to calculate distance.

Working Inside Function:

  1. Send ultrasonic pulse (10 microseconds)

  2. Measure return time using pulseIn()

  3. Apply formula:

 
Distance = (Time × Speed of Sound) / 2
 

Where:

  • Speed of sound ≈ 0.034 cm per microsecond

  • Divided by 2 because sound travels forward and back


6️⃣ Hand Detection Logic

 
if (distanceHand > 0 && distanceHand < 20)
 

If hand is within 20 cm:

  • Servo rotates to 90° → Lid opens

  • Wait 3 seconds

  • Servo returns to 0° → Lid closes


7️⃣ Garbage Level Detection

 
if (distanceGarbage < 8)
 

If garbage distance is less than 8 cm:

  • Dustbin is FULL

Otherwise:

  • Dustbin is NOT Full

You can adjust 8 based on your bin height.


📊 Serial Monitor Output Example

 
Hand Distance: 15 cm
Hand Detected – Opening Lid
Garbage Distance: 25 cm
Status: Dustbin is NOT Full
—————————–
 

🎓 Learning Outcomes

After this lesson, we will understand:

  • Function creation in Arduino

  • Ultrasonic sensor timing principle

  • Servo motor angle control

  • Conditional statements (if-else)

  • Serial debugging



Scroll to Top