How to Detect Movement with the Arduino - dummies

How to Detect Movement with the Arduino

By John Nussey

A Passive Infrared (PIR) sensor is a common sensor in some homes and most commercial buildings and allows you to detect movement with the Arduino. You may have seen this sensor in the corner of a room, blinking red every once in a while. It registers heat given off by people, animals, or other heat sources as infrared radiation.

Infrared radiation is invisible to the human eye but is easy for the sensor to distinguish. The sensor itself is similar to the sensor found in a digital camera, but without the complex lenses to capture a detailed picture.

Most commonly, this type of sensor is used for motion detection in burglar alarms. Rather than detect motion, it actually detects changes in temperature.

You have two ways to get a PIR sensor. The first is to take apart a PIR burglar alarm, which is likely pre-packaged with a lens and a sensor. The second method is to buy one specifically intended for microcontroller projects. This most often comes with a basic, ping-pong-ball-styled lens and a bare circuit board underneath. The latter is easier to work with because all the details are known.

Consider the following during planning:

  • Complexity: It can be tricky to hack an existing PIR sensor made for a specific system. Because it needs to communicate with that system, however, the sensor usually has clearly marked connections on the back.

    One of the benefits of using an existing sensor is that it is prepackaged, which reduces the amount of time you have to spend putting components together. Prepackaged systems are designed to be easy to install, so you may also be able to use manual calibration.

    If you’re using a PIR sensor that is not prepackaged, it should be a lot more straightforward on the hardware and software side but requires some careful thought concerning the housing. Some PIR sensors have their own on-board logic and operate like a switch, going HIGH when movement occurs over the threshold. This kind of sensor needs calibration to identify change from the norm.

  • Cost: A household PIR sensor costs between $15 and $45 (£10 and £30). The main expense is the housing, usually designed to be discreet or look suitably high-tech. Bare PIR sensors cost a fraction of the price at around $10 (£6.50), but need a suitable housing to be of any real use.

  • Where: Many housings allow you to neatly fit the sensor against a wall, or you might consider using mini tripods for direction. Some of the tripod mounts also come with a suction-cup mount, which is perfect for fixing your sensor to glossy surfaces such as glass.

Most PIR sensors come ready for action, needing only power. They calibrate themselves based on what they can see and then send a HIGH or LOW value when they detect change. This makes them extremely easy to program because you are dealing with the same signals as with a pushbutton.

In this example, you learn how to use the SE-10, a PIR sensor available from all the major Arduino retailers. This particular PIR sensor has three wires: red, brown, and black. The red wire is the power source and should be connected to 5V. Oddly, the black wire is the signal wire and not the ground. Brown should be wired to ground and black to pin 2.


The signal pin is known as an open-collector and needs to be pulled HIGH to start with. To do so, you use a 10k resistor to connect it to 5V as well. The pin, therefore, reads HIGH when no motion occurs and is pulled to LOW when there is motion.

You need:

  • An Arduino Uno

  • A breadboard

  • An SE-10 PIR Motion Sensor

  • A 10k ohm resistor

  • Jump wires

Lay out the circuit as in the layout and circuit diagrams.


Complete the circuit and choose File→Examples→01.Basics→DigitalReadSerial from the Arduino menu to load the sketch.


This sketch is intended for a pushbutton but follows the same principles. If you want to make the sketch more specific, you can save it with a more appropriate name and variable names.

 Reads a digital input on pin 2, prints the result to the serial monitor
 This example code is in the public domain.
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
 // initialize serial communication at 9600 bits per second:
 // make the pushbutton's pin an input:
 pinMode(pushButton, INPUT);
// the loop routine runs over and over again forever:
void loop() {
 // read the input pin:
 int buttonState = digitalRead(pushButton);
 // print out the state of the button:
 delay(1);  // delay in between reads for stability

Press the Compile button to check your code. Doing so highlights any grammatical errors and turns them red when they are discovered. If the sketch compiles correctly, click Upload to send the sketch to your board. When it is done uploading, affix the PIR sensor to a surface that is free of movement and open the serial monitor.

Opening the serial monitor resets the sketch and the sensor calibrates itself in the first 1 to 2 seconds. When movement is detected, you should see the buttonState value change from 1 (no movement) to 0 (movement).

If nothing happens, double-check your wiring:

  • Make sure that you’re using the correct pin number.

  • Check the connections on the breadboard. If the jump wires or components are not connected using the correct rows in the breadboard, they will not work.

  • Try restarting the PIR sensor by disconnecting and reconnecting the GND wire, and be sure that it does not move during or after calibration.