Tempat Sampah Otomatis
Alat/Bahan
Arduino IDE
Buzzer
1 Buah
Library Servo
1 Buah
Sensor HC-SR04
1 Buah
Motor Servo
1 Buah
Project board/PCB Dot matrik
1 Buah / Secukupnya
Kabel Jumper
Secukupnya
Skema Rangkaian
Vin – VCC HC-SR04, VCC Servo
GND – GND HC-SR04, GND Servo
D8 – Trig HC-SR04
D7 – Ecdho HC-SR04
D9 – Pin Data Servo
D6 – Buzzer
Langkah Kerja
Sketch Program
#include <Servo.h>
#define Trigpin 8
#define Echopin 7
#define buzzer 6
long duration,distance;
Servo servo;
void setup()
{
pinMode(Trigpin, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(Echopin, INPUT);
servo.attach(9);
Serial.begin(9600);
servo.write(0);
}
void loop()
{
digitalWrite(Trigpin,LOW);
delayMicroseconds(2);
digitalWrite(Trigpin,HIGH);
delayMicroseconds(10);
digitalWrite(Trigpin,LOW);
duration=pulseIn(Echopin,HIGH);
distance=duration/58.2;
if(distance <30 && distance >0)
{
Serial.println(distance);
servo.write(120);
digitalWrite(buzzer, HIGH);
delay(1000);
}
else
{
digitalWrite(buzzer, LOW);
servo.write(0);
}
}
0 comments:
Post a Comment