What You'll Build and What You Need
A motion detector with Arduino uses a PIR sensor (passive infrared sensor) to notice when something warm moves in front of it, then triggers an action — usually turning on an LED light or sending a signal to your computer. The sensor detects heat, not movement itself, so it works in darkness and doesn't need a camera.
You'll need an Arduino board (Uno is the most common and cheapest), a PIR motion sensor module (around $5 to $10), an LED, a 220-ohm resistor, a breadboard, and jumper wires. Most of these come together in starter kits. You'll also need the Arduino IDE software, which is free and runs on Windows, Mac, or Linux.
This project takes about 30 minutes to wire up and another 15 minutes to load the code. No soldering required — everything plugs into the breadboard.
Key Takeaways
- A PIR sensor detects infrared heat from moving objects and sends a signal to your Arduino when motion appears.
- The sensor needs 30 to 60 seconds to calibrate when you first power it on, during which it ignores all motion.
- You connect the sensor to a digital input pin on the Arduino, and the Arduino reads HIGH or LOW to know whether motion was detected.
- The Arduino code runs in a loop, constantly checking the sensor and deciding what to do when motion triggers.
- Common mistakes include powering the sensor with the wrong voltage, not waiting for calibration, or connecting wires to the wrong pins.
Wiring the PIR Sensor to Your Arduino
The PIR sensor module has three pins: power (usually labeled VCC), ground (GND), and signal (usually labeled OUT or DATA). Your Arduino Uno has a row of power pins on one side and a row of digital input/output pins on the other.
Connect the sensor's VCC pin to the Arduino's 5V pin using a red jumper wire. Connect the sensor's GND pin to any GND pin on the Arduino using a black jumper wire. Connect the sensor's OUT or DATA pin to digital pin 2 on the Arduino using a yellow or other colored jumper wire. This is the pin the Arduino will read to know whether motion was detected.
Double-check that each wire is fully inserted into both the breadboard and the Arduino pins. A loose connection is the most common reason a project doesn't work on the first try.
Adding an LED to Show When Motion Is Detected
The LED lets you see the motion detector working without opening the Arduino IDE. Connect the longer leg of the LED (the positive side) to digital pin 13 on the Arduino through a 220-ohm resistor. The resistor protects the LED from burning out. Connect the shorter leg of the LED (the negative side) to any GND pin on the Arduino.
If you're not sure which leg is longer, look at the flat edge on the plastic casing of the LED — the flat edge is on the negative side. The resistor can go on either end of the LED; it doesn't matter which direction it faces.
When the Arduino detects motion, it will send power to pin 13, and the LED will light up. When motion stops, the LED turns off.
Writing and Loading the Code
Open the Arduino IDE on your computer and create a new sketch. Copy this code into the editor:
int motionSensor = 2; int ledPin = 13; void setup() { pinMode(motionSensor, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { int motion = digitalRead(motionSensor); if (motion == HIGH) { digitalWrite(ledPin, HIGH); Serial.println("Motion detected!"); } else { digitalWrite(ledPin, LOW); } delay(100); }
This code tells the Arduino to read pin 2 (where the sensor is connected) every 100 milliseconds. When the sensor reads HIGH, it means motion was detected, so the code turns on the LED and prints a message to the Serial Monitor. When the sensor reads LOW, the LED turns off.
Connect your Arduino to your computer with a USB cable. In the Arduino IDE, go to Tools > Board and select Arduino Uno. Go to Tools > Port and select the port your Arduino is connected to (it usually shows COM3 or higher on Windows, or /dev/ttyUSB0 on Linux). Click the Upload button (the arrow pointing right). The code will compile and load onto your Arduino in about 10 seconds.
Testing and Calibration
After you upload the code, the PIR sensor needs 30 to 60 seconds to calibrate. During this time, it learns what the room looks like with no motion and ignores any movement. Do not wave your hand in front of the sensor during calibration, or it will take longer to settle.
After calibration is complete, move your hand or body in front of the sensor. The LED should light up immediately. Move away, and the LED should turn off after a few seconds. If you open the Serial Monitor (Tools > Serial Monitor, set the baud rate to 9600), you'll see "Motion detected!" printed each time the sensor triggers.
If the LED doesn't light up, check that all wires are fully inserted and that you selected the correct board and port before uploading. If the LED stays on all the time, the sensor may still be calibrating — wait another minute and try again.
Common Problems and How to Fix Them
The most common issue is the sensor not detecting motion at all. First, make sure the sensor is getting power — most PIR modules have a small LED on them that lights up when powered. If that LED is off, check that the VCC and GND wires are connected to the correct pins. If the sensor LED is on but motion isn't triggering the Arduino LED, the signal wire may be loose or connected to the wrong pin.
Another common problem is the sensor triggering constantly, even when nothing is moving. This usually means the sensor is still calibrating or the room is too warm. PIR sensors are sensitive to heat sources like radiators, sunlight through windows, and air vents. Move the sensor away from these heat sources and wait for calibration to finish.
If the Arduino IDE won't upload the code, make sure you selected the correct board (Arduino Uno) and the correct port. If you're not sure which port, unplug the Arduino, look at the list of ports, plug it back in, and look again — the new port that appears is the one you need.
What to Do Next
Once the basic motion detector works, you can expand it. Add a buzzer to make a sound when motion is detected, or add multiple LEDs in different colors. You can also add a delay so the LED stays on for 10 seconds after motion stops, rather than turning off immediately. Change the delay value in the code from 100 to 10000 (milliseconds) to make the LED stay on longer.
For a more advanced project, connect the Arduino to the internet using a WiFi shield or Ethernet shield, then send yourself a text message or email when motion is detected. You can also log motion events to a file on your computer by reading the Serial Monitor output and saving it.
Frequently Asked Questions
Why does my PIR sensor take so long to start working?
PIR sensors need 30 to 60 seconds to calibrate when you first power them on. During this time, the sensor is learning what the room looks like without motion. This is normal and happens every time you power on the Arduino. Do not move in front of the sensor during calibration.
Can I use a different Arduino pin instead of pin 2 for the sensor?
Yes. Any digital pin (0 through 13) works for reading the sensor. If you use a different pin, change the line int motionSensor = 2; to match the pin you chose, like int motionSensor = 3; or int motionSensor = 7;. You do not need to change anything else in the code.
What if I want the LED to stay on for a few seconds after motion stops?
Add a variable to track when motion was last detected, then check if enough time has passed before turning the LED off. Change the loop to store the current time when motion is detected, then only turn off the LED if motion has been absent for longer than your chosen delay (like 5000 milliseconds for 5 seconds).
Can I power the Arduino and sensor from a battery instead of USB?
Yes. Connect a 9V battery to the power jack on the Arduino (or use the VIN and GND pins). The Arduino will regulate the voltage down to 5V for the sensor. The battery will last several hours depending on the capacity and how often the sensor is triggering.
Does the PIR sensor work in complete darkness?
Yes. PIR sensors detect infrared heat, not visible light, so they work in total darkness. They also work in bright sunlight. The only thing that affects them is heat sources like radiators, sunlight through windows, and air conditioning vents, which can cause false triggers.