Trang chủ Điện - Điện tửArduinoHC-SR501 Cảm Biến Chuyển Động PIR Với Arduino

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

bởi Admin
11 lượt xem 1 Phút đọc

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: Download

Video:

Để lại bình luận

Focus Mode

Đã phát hiện trình chặn quảng cáo

Vui lòng hỗ trợ chúng tôi bằng cách tắt tiện ích chặn quảng cáo (AdBlocker) trên trình duyệt của bạn khi truy cập trang web của chúng tôi.