Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat menggunakan Arduino untuk buka tutup pagar otomatis menggunakan RFID, alat ini dilengkapi dengan sensor jarak HC-SRF04 yang difungsikan jika ada orang yang masuk ketika pagar sedang menuju ke mode tutup maka pagar akan berhenti sampai orang atau benda tersebut masuk dan tidak terdeteksi lagi, buka tutup pagar menggunakan kartu RFID dengan tipe RC-522. untuk lebih detailnya berikut adalah skema dan programnya.
a. Arduino Uno
b. RFID RC-522
c. Driver Motor IBT-2
d. Sensor Jarak HC-SRF04
e. Program Arduino IDE
#include <SPI.h> //library serial parallel interface
#include <Wire.h> //library untuk wire i2c
#include <RFID.h> //library RFID
#define sda 10 //Pin Serialdata (SDA)
#define rst 9 //pin Reset
#define trigPin 2
#define echoPin 3
int sw1 = 7;
int sw2 = 6;
int sw1x = 0;
int sw2x = 0;
int pwm1 = 4;
int pwm2 = 5;
RFID rfid(sda,rst);
long duration, distance;
void setup(){
Serial.begin(9600); //baud komunikasi serial monitor
SPI.begin(); //Prosedur antarmuka SPI
rfid.init(); //Memulai inialisasi module RFID
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(sw1, INPUT_PULLUP);
pinMode(sw2, INPUT_PULLUP);
pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
}
void loop(){
if(rfid.isCard()){
if(rfid.readCardSerial()){
Serial.print(rfid.serNum[0]); //serial no.1
Serial.print(" ");
Serial.print(rfid.serNum[1]); //serial no.2
Serial.print(" ");
Serial.print(rfid.serNum[2]); //serial no.3
Serial.print(" ");
Serial.print(rfid.serNum[3]); //serial no.4
Serial.print(" ");
Serial.print(rfid.serNum[4]); //serial no.5
Serial.println("");
if((rfid.serNum[0] == 11) && (rfid.serNum[1] == 197) && (rfid.serNum[2] == 157) && (rfid.serNum[3] == 35) && (rfid.serNum[4] == 112)){
Serial.println("BENAR");
//buka
analogWrite(pwm1,0);
analogWrite(pwm2,100);
buka();
//tutup
analogWrite(pwm1,150);
analogWrite(pwm2,0);
tutup();
}
delay(1000);
} }
}
void buka(){
Serial.println("BUKA");
sw1x = digitalRead(sw1);
if(sw1x == LOW){
analogWrite(pwm1,0);
analogWrite(pwm2,0);
delay(10000); //delay saat pagar buka dan mau nutup
return;
}
buka();
}
void tutup(){
Serial.println("TUTUP");
sw2x = digitalRead(sw2);
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
delay(1000);
if((distance < 100)&&(sw2x == HIGH)){
//tutup
analogWrite(pwm1,0);
analogWrite(pwm2,0);
}
if((distance > 100)&&(sw2x == HIGH)){
analogWrite(pwm1,150);
analogWrite(pwm2,0);
}
if((distance > 100)&&(sw2x == LOW)){
analogWrite(pwm1,0);
analogWrite(pwm2,0);
return;
}
tutup();
}
#include <Wire.h> //library untuk wire i2c
#include <RFID.h> //library RFID
#define sda 10 //Pin Serialdata (SDA)
#define rst 9 //pin Reset
#define trigPin 2
#define echoPin 3
int sw1 = 7;
int sw2 = 6;
int sw1x = 0;
int sw2x = 0;
int pwm1 = 4;
int pwm2 = 5;
RFID rfid(sda,rst);
long duration, distance;
void setup(){
Serial.begin(9600); //baud komunikasi serial monitor
SPI.begin(); //Prosedur antarmuka SPI
rfid.init(); //Memulai inialisasi module RFID
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(sw1, INPUT_PULLUP);
pinMode(sw2, INPUT_PULLUP);
pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
}
void loop(){
if(rfid.isCard()){
if(rfid.readCardSerial()){
Serial.print(rfid.serNum[0]); //serial no.1
Serial.print(" ");
Serial.print(rfid.serNum[1]); //serial no.2
Serial.print(" ");
Serial.print(rfid.serNum[2]); //serial no.3
Serial.print(" ");
Serial.print(rfid.serNum[3]); //serial no.4
Serial.print(" ");
Serial.print(rfid.serNum[4]); //serial no.5
Serial.println("");
if((rfid.serNum[0] == 11) && (rfid.serNum[1] == 197) && (rfid.serNum[2] == 157) && (rfid.serNum[3] == 35) && (rfid.serNum[4] == 112)){
Serial.println("BENAR");
//buka
analogWrite(pwm1,0);
analogWrite(pwm2,100);
buka();
//tutup
analogWrite(pwm1,150);
analogWrite(pwm2,0);
tutup();
}
delay(1000);
} }
}
void buka(){
Serial.println("BUKA");
sw1x = digitalRead(sw1);
if(sw1x == LOW){
analogWrite(pwm1,0);
analogWrite(pwm2,0);
delay(10000); //delay saat pagar buka dan mau nutup
return;
}
buka();
}
void tutup(){
Serial.println("TUTUP");
sw2x = digitalRead(sw2);
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
delay(1000);
if((distance < 100)&&(sw2x == HIGH)){
//tutup
analogWrite(pwm1,0);
analogWrite(pwm2,0);
}
if((distance > 100)&&(sw2x == HIGH)){
analogWrite(pwm1,150);
analogWrite(pwm2,0);
}
if((distance > 100)&&(sw2x == LOW)){
analogWrite(pwm1,0);
analogWrite(pwm2,0);
return;
}
tutup();
}
f. VIDEO HASILNYA
No comments:
Post a Comment