Example Code for Arduino-Human Detection
Last revision 2025/12/26
This guide details the integration of a PIR motion sensor with Arduino for human detection, featuring hardware setup, software installation, wiring diagrams, and sample code for effective motion sensing.
Hardware Preparation
| Name | Model/SKU | Quantity | Purchase Link |
|---|---|---|---|
| PIR Motion Sensor | SEN0171 | 1 | DFRobot |
| Indicator LED | - | 1 | - |
| Arduino/Raspberry Pi Controller | - | 1 | - |
Software Preparation
- Development Tool: Arduino IDE (Latest Version). Download link: Arduino IDE
- Library Installation: Follow the guide here for library loading.
Wiring Diagram

Other Preparation Work
When the sensor just powers on, there will be unstable for a short time. The output level will be jittered, but stabilize shortly.
Sample Code
// #
// # Editor : Youyou from DFRobot
// # Date : 04.06.2014
// # E-Mail : [email protected]
// # Product name: PIR (Motion) Sensor
// # Product SKU : SEN0171
// # Version : 1.0
// # Description:
// # The sketch for using the PIR Motion sensor with Arduino/Raspberry Pi controller to achieve the human detection feature.
// # Hardware Connection:
// # PIR Sensor -> Digital pin 2
// # Indicator LED -> Digital pin 13
// #
byte sensorPin = 2;
byte indicator = 13;
void setup()
{
pinMode(sensorPin,INPUT);
pinMode(indicator,OUTPUT);
Serial.begin(9600);
}
void loop()
{
byte state = digitalRead(sensorPin);
digitalWrite(indicator,state);
if(state == 1)Serial.println("Somebody is in this area!");
else if(state == 0)Serial.println("No one!");
delay(500);
}
Result
When the sensor detects the living movement body within detection range, the output pin will output high voltage of 3V, while the output indicator is lighted.Otherwise,after 2~3s,the output pin will output high voltage of 0V, while the output indicator is extinguished.
Note: When the sensor just on electricity, there will be unstable for a short time, the output terminal will be level shake!
Additional Information
The pyroelectric infrared motion sensor integrates a digital integrated body pyroelectric infrared detector, whose model is AM412. This detector is a digital intelligent PIR sensor. It interfaces directly with up to two conventional PIR sensors via a high impedance differential input. The PIR signal is converted to a 15 bit digital value on chip. A LED indicates whenever the PIR signal is above the selected threshold. All signal processing is performed digitally.

Was this article helpful?
