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

Smart Dustbin (Bluetooth Serial Terminal Version)

🎯 Objective of This Lesson

In this lesson students will:

  • Use ESP32 Bluetooth

  • Monitor dustbin status on mobile

  • Open lid automatically

  • Check whether dustbin is FULL or NOT

  • Understand wireless serial communication


📱 Required Mobile App

Install:

  • Serial Bluetooth Terminal (Android)

  • Or any Bluetooth terminal app


🔌 Pin Configuration (Same Hardware)

Ultrasonic 1 – Hand Detection

  • TRIG → GPIO 5

  • ECHO → GPIO 18

Ultrasonic 2 – Garbage Level

  • TRIG → GPIO 17

  • ECHO → GPIO 16

Servo

  • Signal → GPIO 13


🧾 Complete ESP32 Bluetooth Code (Copy–Paste Ready)

#include <ESP32Servo.h>
#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

// -------- 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 --------
void setup() {

Serial.begin(115200);
SerialBT.begin("Smart_Dustbin"); // Bluetooth device name

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

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

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

Serial.println("Bluetooth Smart Dustbin Started");
SerialBT.println("Connected to Smart Dustbin");
}

// -------- 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);

if (distanceHand > 0 && distanceHand < 20) {

Serial.println("Hand Detected - Opening Lid");
SerialBT.println("Hand Detected - Opening Lid");

myServo.write(90);
delay(3000);
myServo.write(0);
}

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

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

if (distanceGarbage > 0 && distanceGarbage < 8) {

SerialBT.println("Status: Dustbin is FULL");

} else {

SerialBT.println("Status: Dustbin is NOT Full");

}

SerialBT.println("------------------------");

delay(1000);
}

🧠 Code Explanation (Step-by-Step)


1️⃣ Bluetooth Library

 
#include “BluetoothSerial.h”
 

This library enables Bluetooth communication in ESP32.


2️⃣ Bluetooth Object

 
BluetoothSerial SerialBT;
 

Creates a Bluetooth communication object.


3️⃣ Starting Bluetooth

 
SerialBT.begin(“Smart_Dustbin”);
 

This sets the device name that appears on your mobile Bluetooth list.

When you scan devices, you will see:

➡ Smart_Dustbin


4️⃣ Sending Data to Mobile

Instead of:

 
Serial.println();
 

We use:

 
SerialBT.println();
 

This sends data directly to the Bluetooth terminal app.


5️⃣ Hand Detection Logic

If hand distance < 20 cm:

  • Servo rotates to 90°

  • Lid opens

  • Waits 3 seconds

  • Closes

Message is also sent to mobile:

 
Hand Detected – Opening Lid
 

6️⃣ Garbage Level Monitoring

If garbage distance < 8 cm:

Mobile shows:

 
Status: Dustbin is FULL
 

Otherwise:

 
Status: Dustbin is NOT Full
 

📱 How To Connect Mobile

  1. Upload code to ESP32

  2. Turn ON Bluetooth on phone

  3. Open Serial Bluetooth Terminal app

  4. Search for device → Smart_Dustbin

  5. Pair and connect

  6. Start monitoring


📊 Example Bluetooth Output

 
Garbage Distance: 24 cm
Status: Dustbin is NOT Full
————————
Garbage Distance: 6 cm
Status: Dustbin is FULL
————————
 

🎓 Learning Outcomes

We learn:

  • Wireless communication

  • Bluetooth Serial

  • Real-time monitoring

  • IoT upgrade concept

  • Difference between Serial and SerialBT

Scroll to Top