将Arduino uno与PIR运动传感器连接

在本基础教程中,我们将了解如何将Arduino uno与PIR运动传感器(HC-SR501)连接。因此,让我们开始!

项目介绍

首先,我们需要知道什么是运动传感器以及它是如何工作的?

被动红外传感器(PIR)也称为运动传感器,是一种电子设备,它使用一对热释电传感器来检测周围环境中的热能来感应运动。这两个传感器并排放置,当两个传感器之间的信号差发生变化时(假设如果有人进入房间),传感器将接合。它基本上可以捕捉运动。它有三个端子,即 Gnd、Vcc 和带有 3V 稳压器、延时控制器、灵敏度控制器和 BIS001 的信号引脚。

PIR运动传感器

PIR 端子 – Gnd、Vcc 和信号引脚。Gnd 被视为负极引脚,并连接到系统的接地。Vcc 基本上为引脚供电,典型值为 5V。信号引脚是输出引脚。

运动传感器引脚图

Arduino uno :Arduino Uno 是基于 ATmega328 的微控制器板。它有 20 个数字输入/输出引脚(其中 6 个用作 PWM 输出,6 个用作模拟输入)、一个 16 MHz 谐振器、一个 USB 连接、一个电源插孔、一个在线系统编程 (ICSP) 接头和一个复位按钮。

现在我们可以开始在电路上工作了:

PIR 连接 – 将传感器的 Gnd 引脚连接到 Arduino 的接地。传感器的Vcc引脚到Arduino的5V。和信号/输出引脚到Arduino板的数字引脚5。

LED 连接 – LED 到 Arduino 数字引脚 9 的正极端子。负极端子应连接到电阻器的任何一条腿。电阻器的另一条腿应连接到Arduino的Gnd。

请参阅电路图以更好地理解。电路图也上传到硬件部分,以便您可以下载。

代码

const int led = 9; // Led positive terminal to the digital pin 9.              
const  int sensor = 5; // signal pin of sensor to digital pin 5.               
const  int state = LOW;            
const int val = 0;                 

void  setup() { // Void setup is ran only once after each powerup or reset of the Arduino  board.
  pinMode(led, OUTPUT); // Led is determined as an output here.    
  pinMode(sensor, INPUT); // PIR motion sensor is determined is an input here.  
  Serial.begin(9600);      
}

void loop(){ // Void loop is ran over and  over and consists of the main program.
  val = digitalRead(sensor);   
  if  (val == HIGH) {           
    digitalWrite(led, HIGH);   
    delay(500);  // Delay of led is 500             
    
    if (state == LOW) {
      Serial.println("  Motion detected "); 
      state = HIGH;       
    }
  } 
  else {
      digitalWrite(led, LOW);
      delay(500);             
      
      if  (state == HIGH){
        Serial.println("The action/ motion has stopped");
        state = LOW;       
    }
  }
}

Similar Posts

Leave a Reply