HC-SR501 Cảm Biến Chuyển Động PIR Với Arduino

Phần cứng cần thiết:

Code:

//define the pins
int Load = A1;
int PIR = A0;
int pirState = 2;

void setup() {
 //define the Relay pin as output
 pinMode(Load, OUTPUT);
 pinMode(pirState, OUTPUT);
 Serial.begin(9600);
 //define the sensor pin as input
 pinMode(PIR, INPUT);
}

void loop() {
 //using the digitalRead function we will read the signal of the sensor
 int value = digitalRead(PIR);
 //if its high or if an any object is detected it will activate the Relay Module
 if (value == HIGH){
 Serial.println("Phat hien chuyen dong !!!");
 bat_Den();
 digitalWrite(pirState, HIGH); //For activating the Relay we will send a LOW as the Relay input pin works inversely.
  delay(1000);
 }
 else {
   //digitalWrite(LED, LOW);
 tat_Den();
 digitalWrite(pirState, LOW);
 Serial.println("Khong phat hien chuyen dong");
 delay(1000);
 }
}
void coi_bao()
{
 digitalWrite(Load, HIGH);
 delay(250);
 digitalWrite(Load, LOW);
 delay(250);
}
void bat_Den(){
 digitalWrite(Load, HIGH);
}
void tat_Den(){
 digitalWrite(Load, LOW);
}
 

File: 

HC-SR501 cam bien chuyen dong pir, tlc47 truong cong ly, trương công lý