ESP32-Based Smart Trash Bin Monitoring System with Wi-Fi Connectivity
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed for a smart trash bin monitoring system. It utilizes an ESP32 microcontroller to interface with various sensors and a servo motor. The system includes an ultrasonic sensor for measuring trash levels, an IR sensor for detecting obstacles, an MQ-135 gas sensor for detecting harmful gases, and an LED indicator. The ESP32 connects to the Blynk platform for remote monitoring and control.
Component List
ESP32 Devkit V1
- Description: A powerful microcontroller with WiFi and Bluetooth capabilities.
- Pins: 3V3, GND, D15, D2, D4, RX2, TX2, D5, D18, D19, D21, RX0, TX0, D22, D23, EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, VIN
HC-SR04 Ultrasonic Sensor
- Description: A sensor used to measure distance by using ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND
Servo
- Description: A motor used for precise control of angular position.
- Pins: GND, VCC, PWM
IR Sensor
- Description: An infrared sensor used to detect obstacles.
- Pins: out, gnd, vcc
MQ-135 Gas Sensor
- Description: A sensor used to detect harmful gases.
- Pins: D OUT, A OUT, GND, VCC
LED: Two Pin (red)
- Description: A simple red LED used as an indicator.
- Pins: cathode, anode
Wiring Details
ESP32 Devkit V1
- 3V3: Connected to VCC of HC-SR04 Ultrasonic Sensor, Servo, IR Sensor, and MQ-135 Gas Sensor.
- GND: Connected to GND of HC-SR04 Ultrasonic Sensor, Servo, IR Sensor, MQ-135 Gas Sensor, and cathode of LED.
- D15: Connected to PWM of Servo.
- D5: Connected to ECHO of HC-SR04 Ultrasonic Sensor.
- D18: Connected to TRIG of HC-SR04 Ultrasonic Sensor.
- D19: Connected to anode of LED.
- D23: Connected to out of IR Sensor.
- D34: Connected to A OUT of MQ-135 Gas Sensor.
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 3V3 of ESP32 Devkit V1.
- TRIG: Connected to D18 of ESP32 Devkit V1.
- ECHO: Connected to D5 of ESP32 Devkit V1.
- GND: Connected to GND of ESP32 Devkit V1.
Servo
- GND: Connected to GND of ESP32 Devkit V1.
- VCC: Connected to 3V3 of ESP32 Devkit V1.
- PWM: Connected to D15 of ESP32 Devkit V1.
IR Sensor
- out: Connected to D23 of ESP32 Devkit V1.
- gnd: Connected to GND of ESP32 Devkit V1.
- vcc: Connected to 3V3 of ESP32 Devkit V1.
MQ-135 Gas Sensor
- D OUT: Not connected.
- A OUT: Connected to D34 of ESP32 Devkit V1.
- GND: Connected to GND of ESP32 Devkit V1.
- VCC: Connected to 3V3 of ESP32 Devkit V1.
LED: Two Pin (red)
- cathode: Connected to GND of ESP32 Devkit V1.
- anode: Connected to D19 of ESP32 Devkit V1.
Documented Code
// Blynk credentials
#define BLYNK_TEMPLATE_ID "TMPL6njxn9Afv"
#define BLYNK_TEMPLATE_NAME "Smart Trash Bin Monitoring"
#define BLYNK_AUTH_TOKEN "tLRf6lKv0tmOddiNEDzB6Usg4nReIkjv"
#include <ESP32Servo.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// Pin Definitions
#define IR_SENSOR_PIN 23 // Pin untuk sensor IR
#define SERVO_PIN 15 // Pin untuk servo
#define MQ135_PIN 34 // Pin untuk sensor MQ-135 (analog)
#define TRIG_PIN 18 // Pin untuk Trigger sensor ultrasonik
#define ECHO_PIN 5 // Pin untuk Echo sensor ultrasonik
#define LED_PIN 19 // Pin untuk LED Merah
// WiFi credentials
char ssid[] = "M4"; // Ganti dengan SSID WiFi Anda
char pass[] = "12345678"; // Ganti dengan password WiFi Anda
// Thresholds
int gasThreshold = 1000; // Ambang batas untuk mendeteksi gas (sesuaikan dengan kalibrasi sensor)
bool gasDetected = false;
Servo servo;
void setup() {
// Setup Pins
pinMode(IR_SENSOR_PIN, INPUT); // Set pin sensor IR sebagai input
servo.attach(SERVO_PIN); // Menghubungkan servo ke pin
servo.write(0); // Posisi awal servo (tertutup)
pinMode(TRIG_PIN, OUTPUT); // Set pin Trigger sebagai output
pinMode(ECHO_PIN, INPUT); // Set pin Echo sebagai input
pinMode(LED_PIN, OUTPUT); // Set pin LED sebagai output
// Connect to WiFi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
// Connect to Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); // Menghubungkan ke Blynk
}
void loop() {
Blynk.run(); // Menjalankan Blynk
// Sensor IR
int sensorValue = digitalRead(IR_SENSOR_PIN); // Membaca nilai dari sensor IR
if (sensorValue == LOW) { // Jika halangan terdeteksi (sensor IR LOW)
servo.write(90); // Menggerakkan servo (buka)
delay(2000); // Tunggu 2 detik
servo.write(0); // Kembali ke posisi awal (tutup)
}
// Sensor MQ-135
int gasValue = analogRead(MQ135_PIN);
Blynk.virtualWrite(V0, gasValue); // Kirim nilai gas ke widget di Virtual Pin V0
if (gasValue > gasThreshold) {
gasDetected = true;
Blynk.logEvent("gas_detected", "Bau gas terdeteksi!"); // Log event ke Blynk
} else {
gasDetected = false;
}
// Sensor Ultrasonik
long duration, distance;
// Mengirimkan sinyal trigger
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Membaca sinyal echo
duration = pulseIn(ECHO_PIN, HIGH);
// Menghitung jarak (dalam cm)
distance = duration * 0.034 / 2;
// Menghitung kapasitas sampah (maksimum 15 cm)
int trashCapacity = map(distance, 0, 15, 100, 0); // Menghitung kapasitas dalam persen
if (trashCapacity < 0) trashCapacity = 0; // Pastikan tidak negatif
// Mengupdate status di Blynk
Blynk.virtualWrite(V2, trashCapacity); // Menampilkan kapasitas sampah di widget V2
// Menghidupkan LED jika jarak lebih dari 15 cm
if (distance < 15) {
digitalWrite(LED_PIN, HIGH); // Hidupkan LED Merah
} else {
digitalWrite(LED_PIN, LOW); // Matikan LED
}
delay(500); // Delay sebelum pembacaan berikutnya
}
This documentation provides a comprehensive overview of the circuit, including a summary, component list, wiring details, and the embedded code for the ESP32 microcontroller.