Creating a smart trash bin that opens and closes automatically and plays an audio clip of Optimus Prime is a fun and practical project. This tutorial will guide you through the steps to build your own smart trash bin using an Arduino, a servo motor, an ultrasonic sensor, and a DFPlayer Mini MP3 player.
Materials Needed:
- Arduino Uno
- Servo motor (SG90 or similar)
- Ultrasonic sensor (HC-SR04)
- DFPlayer Mini MP3 player
- Speaker
- Jumper wires
- Breadboard
- External power supply (if necessary)
- Trash bin (to mount the components)
Circuit Diagram:
- Ultrasonic Sensor:
- Trig pin to Arduino pin 8
- Echo pin to Arduino pin 9
- VCC to 5V
- GND to GND
- Servo Motor:
- Signal pin to Arduino pin 3
- VCC to 5V
- GND to GND
- DFPlayer Mini:
- RX to Arduino pin 11
- TX to Arduino pin 10
- VCC to 5V
- GND to GND
- Speaker connected to the speaker pins on the DFPlayer Mini
Step-by-Step Instructions:
- Setup Arduino IDE:
- Install the necessary libraries: Servo and DFRobotDFPlayerMini.
bash Sketch -> Include Library -> Manage Libraries...
- Search for and install the
DFRobotDFPlayerMini
andServo
libraries.
- Upload the Code:
#include <Servo.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
// Pin definisi
const int trigPin = 8;
const int echoPin = 9;
const int servoPin = 3;
// Servo instance
Servo myservo;
// SoftwareSerial untuk DFPlayer Mini
SoftwareSerial mySerial(11, 10); // RX, TX
// DFPlayer Mini instance
DFRobotDFPlayerMini myDFPlayer;
// Variabel lainnya
long duration;
int distance;
int servoOpenAngle = 180;
int servoCloseAngle = 0;
void setup() {
// Inisialisasi pin
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Inisialisasi servo
myservo.attach(servoPin);
myservo.write(servoCloseAngle); // Servo pada posisi tertutup
// Inisialisasi komunikasi serial
Serial.begin(9600);
mySerial.begin(9600);
// Inisialisasi DFPlayer Mini
if (!myDFPlayer.begin(mySerial)) {
Serial.println(F("DFPlayer Mini tidak terdeteksi!"));
while(true);
}
Serial.println(F("DFPlayer Mini siap."));
myDFPlayer.volume(30); // Set volume, bisa disesuaikan 0-30
}
void loop() {
// Mengukur jarak
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Menghitung jarak
// Debugging, print jarak ke Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Jika jarak kurang atau sama dengan 50cm, buka tutup tempat sampah
if (distance <= 50) {
myservo.write(servoOpenAngle);
Serial.println("Tutup terbuka");
delay(5000); // Tahan posisi terbuka selama 5 detik
// Putar audio
myDFPlayer.play(1); // Putar audio, file pertama
Serial.println("Memutar audio...");
delay(3000); // Menunggu audio selesai diputar (3 detik, sesuaikan dengan durasi audio)
// Tutup tutup tempat sampah
myservo.write(servoCloseAngle);
Serial.println("Tutup tertutup");
}
delay(500); // Penundaan sebelum pengukuran berikutnya
}
- Prepare the Audio File:
- Download or create an Optimus Prime audio clip.
- Save it as
0001.mp3
on a microSD card. - Insert the microSD card into the DFPlayer Mini.
- Assembly:
- Mount the components on the breadboard.
- Connect the servo motor to the lid of the trash bin.
- Ensure the ultrasonic sensor is positioned to detect approaching objects.
- Power Up and Test:
- Connect the Arduino to a power source.
- Upload the code.
- Test the setup by approaching the ultrasonic sensor and watching the servo open the lid and play the audio.
Conclusion:
Congratulations! You have successfully built a smart trash bin that opens automatically and plays an Optimus Prime audio clip. This project not only enhances the functionality of a regular trash bin but also adds a touch of fun with the audio feature. Enjoy your new smart trash bin!