Arduino-Based Smart Trash Can with Ultrasonic Sensor and Servo Mechanism
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control a smart trash bin using an Arduino UNO. The system includes an ultrasonic sensor to detect the presence of trash, servos to open and close the lid and throw mechanism, a DFPlayer Mini to play audio messages, and an LCD to display information. The circuit also includes a loudspeaker for audio output.
Component List
Arduino UNO
- Description: Microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
HC-SR04 Ultrasonic Sensor
- Description: Ultrasonic distance sensor.
- Pins: VCC, TRIG, ECHO, GND
Servo (Lid)
- Description: Servo motor for controlling the lid.
- Pins: GND, VCC, PWM
Servo (Throw Mechanism)
- Description: Servo motor for controlling the throw mechanism.
- Pins: GND, VCC, PWM
DFPlayer MINI
- Description: MP3 player module.
- Pins: VCC, BUSY, RX, USB -, TX, USB +, DAC_R, K2/ADC_KEY, DAC_L, K1/ADC_KEY, SPK1, IO 2, GND, SPK2, IO 1
Loudspeaker
- Description: Speaker for audio output.
- Pins: pin1, pin2
Resistor
- Description: 200 Ohms resistor.
- Pins: pin1, pin2
16x2 I2C LCD
- Description: LCD display with I2C interface.
- Pins: GND, VCC, SDA, SCL
Wiring Details
Arduino UNO
- 5V: Connected to VCC of HC-SR04 Ultrasonic Sensor, Servo (Lid), Servo (Throw Mechanism), and 16x2 I2C LCD.
- GND: Connected to GND of HC-SR04 Ultrasonic Sensor, Servo (Lid), Servo (Throw Mechanism), DFPlayer MINI, and 16x2 I2C LCD.
- D6: Connected to TRIG of HC-SR04 Ultrasonic Sensor.
- D7: Connected to ECHO of HC-SR04 Ultrasonic Sensor.
- D0: Connected to pin2 of Resistor.
- D1: Connected to TX of DFPlayer MINI.
- D9: Connected to PWM of Servo (Throw Mechanism).
- D10: Connected to PWM of Servo (Lid).
- A4: Connected to SDA of 16x2 I2C LCD.
- A5: Connected to SCL of 16x2 I2C LCD.
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 5V of Arduino UNO.
- TRIG: Connected to D6 of Arduino UNO.
- ECHO: Connected to D7 of Arduino UNO.
- GND: Connected to GND of Arduino UNO.
Servo (Lid)
- VCC: Connected to 5V of Arduino UNO.
- GND: Connected to GND of Arduino UNO.
- PWM: Connected to D10 of Arduino UNO.
Servo (Throw Mechanism)
- VCC: Connected to 5V of Arduino UNO.
- GND: Connected to GND of Arduino UNO.
- PWM: Connected to D9 of Arduino UNO.
DFPlayer MINI
- VCC: Connected to 5V of Arduino UNO.
- GND: Connected to GND of Arduino UNO.
- RX: Connected to pin1 of Resistor.
- TX: Connected to D1 of Arduino UNO.
- SPK1: Connected to pin2 of Loudspeaker.
- SPK2: Connected to pin1 of Loudspeaker.
Loudspeaker
- pin1: Connected to SPK2 of DFPlayer MINI.
- pin2: Connected to SPK1 of DFPlayer MINI.
Resistor
- pin1: Connected to RX of DFPlayer MINI.
- pin2: Connected to D0 of Arduino UNO.
16x2 I2C LCD
- VCC: Connected to 5V of Arduino UNO.
- GND: Connected to GND of Arduino UNO.
- SDA: Connected to A4 of Arduino UNO.
- SCL: Connected to A5 of Arduino UNO.
Documented Code
#include <Servo.h>
#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>
// Pin Definitions
const int TRIG_PIN = 6; // Ultrasonic Sensor TRIG pin
const int ECHO_PIN = 7; // Ultrasonic Sensor ECHO pin
const int SERVO_LID_PIN = 9; // Servo untuk tutup tempat sampah
const int SERVO_THROW_PIN = 10; // Servo untuk mekanisme membuang sampah
const int RX_PIN = 2; // DFPlayer RX pin
const int TX_PIN = 3; // DFPlayer TX pin
// Constants
const int DISTANCE_THRESHOLD = 50; // Jarak dalam centimeter untuk membuka tutup
const int TRASH_DETECT_THRESHOLD = 20; // Jarak untuk mendeteksi sampah dibuang
const int SERVO_OPEN_ANGLE = 90; // Sudut buka servo
const int SERVO_CLOSE_ANGLE = 0; // Sudut tutup servo
// Object Declarations
Servo servoLid;
Servo servoThrow;
SoftwareSerial mySerial(RX_PIN, TX_PIN);
DFPlayerMini_Fast myMP3;
// Variables
float duration_us, distance_cm;
unsigned long lastTrashDetectTime = 0;
const int TRASH_DETECT_DELAY = 3000; // Jeda 3 detik antara ucapan terima kasih
void setup() {
Serial.begin(9600);
// Ultrasonic Sensor Setup
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Servo Setup
servoLid.attach(SERVO_LID_PIN);
servoLid.write(SERVO_CLOSE_ANGLE);
servoThrow.attach(SERVO_THROW_PIN);
servoThrow.write(SERVO_CLOSE_ANGLE);
// DFPlayer Setup
mySerial.begin(9600);
if (!myMP3.begin(mySerial)) {
Serial.println(F("Inisialisasi DFPlayer gagal!"));
while(true); // Hentikan eksekusi lebih lanjut
}
myMP3.volume(20); // Atur volume (0-30)
}
void loop() {
// Ukur jarak
distance_cm = measureDistance();
// Kontrol servo berdasarkan jarak
if (distance_cm < DISTANCE_THRESHOLD) {
// Buka kedua servo 90 derajat secara bersamaan
servoLid.write(SERVO_OPEN_ANGLE);
servoThrow.write(SERVO_OPEN_ANGLE);
} else {
// Tutup kedua servo
servoLid.write(SERVO_CLOSE_ANGLE);
servoThrow.write(SERVO_CLOSE_ANGLE);
}
// Deteksi sampah dibuang
if (distance_cm < TRASH_DETECT_THRESHOLD) {
if (millis() - lastTrashDetectTime > TRASH_DETECT_DELAY) {
playThankYouMessage();
lastTrashDetectTime = millis();
}
}
delay(500);
}
float measureDistance() {
// Hasilkan pulsa 10-mikrodetik ke pin TRIG
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Ukur durasi pulsa dari pin ECHO
duration_us = pulseIn(ECHO_PIN, HIGH);
// Hitung jarak
return 0.017 * duration_us;
}
void playThankYouMessage() {
// Putar file MP3 pertama dari kartu SD
// Pastikan Anda telah mengupload file MP3 ucapan terima kasih ke kartu SD
myMP3.play(1);
}
This code controls the smart trash bin by measuring the distance using the ultrasonic sensor, controlling the servos to open and close the lid and throw mechanism, and playing a thank you message using the DFPlayer Mini when trash is detected.