Test PIR Sensor with Arduino | PIR 感應器

In this tutorial, I would like to show you how to make a PIR sensor to work with Arduino.  PIR stands for Passive Infrared sensor. What it does is to measure infrared (IR) light radiating from objects in its field of view.

A frequently use case is installing a PIR sensor at the hall way and whenever PIR sensor detects people walk towards the hall way, then the PIR sensor turn on the light.   Please refer to Wikipedia for more information about PIR.

Material needed for this tutorial:

  • Arduino Nano * 1 (All other kinds of Arduino board will do)
  • PIR sensor * 1
  • Buzzer * 1
  • LED * 1 (You may just utilize the LED on Arduino Nano)
  • 220 resistor * 1 (No need you use the LED on Arduino Nano)
  • Jump wire ~ up to 10 (female to female)

Wire Connection as follow:

  1. PIR Ground pin to Nano(or Uno) GND pin
  2. PIR Power pin to Nano +5V pin
  3. PIR data pin to Nano D2 pin
  4. LED ground to Nano GND pin
  5. LED power pin to 220K resistor pin
  6. 220K resistor pin to Nano pin 13
  7. Buzzer postive pin to Nano D8 pin
  8. Buzzer ground pin to Nano GND pin

Schematic: (I use www.tinkercad.com to draw this schematic below, just in case you interested.)




Programming logic:
Once the PIR sensor detects objects entered to its detection range, light up LED and sound the Buzzer for one second.

Arduino code here:

#define NOTE_C5  523
const int PIRSensor = 2;
const int ledPin =  13;
int sensorValue = 0;
void setup() {
  pinMode(PIRSensor, INPUT);
  pinMode(ledPin, OUTPUT);
}
void loop() {
sensorValue = digitalRead(PIRSensor);
if (sensorValue == HIGH) { 
    // Object detected, turn on LED, sound the buzzer for 1 second
    tone(8, NOTE_C5);
    digitalWrite(ledPin, HIGH);
    delay(1000);
}else{
    // No object detected, turn off LED, mute the buzzer
    noTone(8);
    digitalWrite(ledPin, LOW);
}



See the demo on Youtube:





Done.
==== 中 文 版 本 ==========================================
這次的教學, 我想試試看 Arduino 的 PIR 感測器.  PIR 是被動式感應器的縮寫. 它可以偵測在它的視線內從物體發射出的紅外線.

最常使用到 PIR 感測器 的地方就是不常有人經過的地方,當有人經過時,PIR 感測器感應到紅外線就會把燈打開。 請參考維基百科的資訊。

這次教學所需的材料:
  • Arduino Nano * 1 (其他 Arduino 板子也可以)
  • PIR 偵測器 * 1
  • 蜂鳴器 * 1
  • LED * 1 (也可以使用 Arduino Nano 上的內建 LED)
  • 220 電阻 * 1 (若使用 Arduino Nano 上的內建 LED就不需要)
  • 杜邦線 大約十個 (母對母)

連線方式:

  1. PIR Ground pin to Nano(or Uno) GND pin
  2. PIR Power pin to Nano +5V pin
  3. PIR data pin to Nano D2 pin
  4. LED ground to Nano GND pin
  5. LED power pin to 220K resistor pin
  6. 220K resistor pin to Nano pin 13
  7. Buzzer postive pin to Nano D8 pin
  8. Buzzer ground pin to Nano GND pin

線路圖: (我是使用 www.tinkercad.com 來會畫這張線路圖, 如果你有興趣可以參考一下)




程式邏輯:
很簡單,當 PIR 感測器在他的視線內感應到紅外線, 將 LED 和蜂鳴器開啟一秒鐘.

Arduino 程式碼在這:

#define NOTE_C5  523
const int PIRSensor = 2;
const int ledPin =  13;
int sensorValue = 0;
void setup() {
  pinMode(PIRSensor, INPUT);
  pinMode(ledPin, OUTPUT);
}
void loop() {
sensorValue = digitalRead(PIRSensor);
if (sensorValue == HIGH) { 
    // Object detected, turn on LED, sound the buzzer for 1 second
    tone(8, NOTE_C5);
    digitalWrite(ledPin, HIGH);
    delay(1000);
}else{
    // No object detected, turn off LED, mute the buzzer
    noTone(8);
    digitalWrite(ledPin, LOW);
}




看看Youtube上的示範:



Comments

Popular posts from this blog

Arduino - DFPlayer Mini MP3 Module

Android control color RGB LED using HC-05 Bluetooth with Arduino (Part I)

Arduino #27 AsyncWifimanager ElegantOTA ESP32 (WiFi Password Mgnt. + WiFi Firmware Update)