Arduino Tutorial 3: PIR Sensor with Digital Output and Serial Monitor
In this tutorial you will learn how to use PIR Sensor to detect human movement and give output of buzzer sound, turn on led and display some text on Arduino Serial Monitor.
PIR sensor stands for passive infrared sensor consists of a Pyroelectric sensor which generates energy when exposed to heat. That means when a human or animal body get in the range of the sensor it will detect a movement because the human or animal body emits heat energy in a form of infrared radiation.
Components needed:
- Arduino UNO
- PIR Sensor
- Buzzer
- LED
- Resistor 220 Ohm
- Breadboard
This sensor has two potentiometers. One for adjusting the sensitivity of the sensor and the other for adjusting the time the output signal stays high when object is detected. This time can be adjusted from 0.3 seconds up to 5 minutes.
As an example for this tutorial we will make a circuit that will turn on an LED and buzzer when the sensor detect an object. Here’s the circuit schematics. The output pin of the sensor will be connected to pin number A0 on the Arduino Board and when an object detected the pin number 12 will activate the buzzer while pin number 13 will activate the LED.
1- Refer to the diagram below for components wiring.
2- Download the code below, open the file and Upload.
int buzzer = 12;// declare pin 12 as buzzer pin int led = 13;//declare pin 13 as LED pin int pir;//declare pir as an integer variable void setup() { pinMode(buzzer,OUTPUT);//configure buzzer as output pinMode(led,OUTPUT);//configure LED as output pinMode(A0,INPUT);// configure pin A0 as input to read the sensor value Serial.begin(9600); } void loop() { pir=digitalRead(A0);//assign the variable pir to read the value from pin A0 Serial.println(pir);// print out the sensor value //if sensor detect motion,buzzer and LED will activate if(pir==1) { Serial.println("motion detected"); digitalWrite(buzzer,HIGH); digitalWrite(led,HIGH); } //if motion detected, buzzer and LED will be turned off else { digitalWrite(buzzer,LOW); digitalWrite(led,LOW); } delay(500); }
5- After uploading the code, go to Tools> Serial Monitor. You will see the output in the Serial Monitor displaying a value ranging from 1 or 0. If the value displayed is 1, the buzzer and LED will be turned on and vice versa.