Hardware stores often sell motion-activated floodlights that you can mount outside your house to turn on whenever someone walks through your yard. However, with an inexpensive motion sensor, you can build your own motion-activated light—or any other motion-activated device—with an Arduino.
For this project, we’ll be using a Passive InfraRed (or PIR) motion sensor. This sensor works by detecting infrared light, like the kind human bodies give off in the form of heat. Since this kind of infrared light is visible to the sensor even when it’s too dark for human eyes to see, they make excellent components for security or safety systems. Image: Arduino with PIR
Most basic PIR sensors have three pins. One ground, one power, and a third signal pin. When the sensor detects movement, the signal pin returns HIGH (or 1). When no motion is detected, it returns LOW (or 0). There are no varying levels in between—the sensor can’t tell the difference between someone slowly walking by, versus a fast movement, for example—but it’s more than enough to work for most projects.
Some more advanced PIR sensors like the HC-SR501 have extra modules that allow you to adjust the sensitivity and trigger modes of the sensor. However, we’ll be focusing on a straightforward PIR sensor to keep things simple for this project.
Most of the components for this project can be found in starter electronics parts kits. To demonstrate how the sensor works, we’ll be using it to control a simple LED. With that in mind, here are the parts you’ll need:
Arduino Uno | ![]() |
A PIR motion sensor | ![]() |
Arduino IDE | ![]() |
An LED | ![]() |
A resistor
Most LEDs will burn out if you connect them directly to the 5V output of an Arduino. Place a resistor from your kit between the output pin and the LED.
|
![]() |
You’ll also need a USB cable if you haven’t got one lying around.
The circuitry for this project will be fairly simple, but you’ll learn how to add a motion sensor to any project in the future.
The code to activate an LED using a motion sensor is relatively straightforward. You’ll need to map the sensor and LED to pins and a simple if-else loop can be used to detect the motion and activate the LED.
First, here’s the full code. You can add this to your Arduino IDE and upload it to the Arduino Uno before you set up the wiring.
int ledPIN = 8; // LED output
int motionPin = 2; // PIR sensor pin
int motionStatus = 0; // Motion detected status
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(motionPin, INPUT);
Serial.begin(9600);
}
void loop(){
motionStatus = digitalRead(motionPin);
if (motionStatus == HIGH) { // When motion is detected
digitalWrite(ledPIN, HIGH); // turn LED ON
Serial.println(“Motion detected.”); // Print motion detected message
}
else {
digitalWrite(ledPIN, LOW); // Turn LED OFF if no motion is detected
Serial.println(“All clear.”); // Print all clear message
}
}
Now, let’s go over the code piece by piece. First up, the variables:
int ledPIN = 8; // LED output
int motionPin = 2; // PIR sensor pin
int motionStatus = 0; // Motion detected status
Prior to the setup() section, we’ll initialize a few variables. First, ledPIN will assign the LED to pin 8. Next, motionPin will assign the motion sensor signal connector to pin 2. Finally, motionStatus will store the state of the motion sensor.
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(motionPin, INPUT);
Serial.begin(9600);
}
This section of code will go inside the setup(). The first two lines here will initialize the LED and sensor pins, assigning them to OUTPUT and INPUT pins, respectively.
The last line will initialize the serial monitor. We’ll use this to confirm the motion sensor is working, even if the LED doesn’t. This is useful as you troubleshoot new projects where you might have a more complex circuit than the LED we’re going to use here.
motionStatus = digitalRead(motionPin);
if (motionStatus == HIGH) { // When motion is detected
digitalWrite(ledPIN, HIGH); // turn LED ON
Serial.println(“Motion detected.”); // Print motion detected message
}
else {
digitalWrite(ledPIN, LOW); // Turn LED OFF if no motion is detected
Serial.println(“All clear.”); // Print all clear message
}
Inside the main loop() section of the sketch, the first line will assign the status of the sensor to the variable motionStatus.
Next, you’ll find a simple if-else loop. If the variable motionStatus is HIGH—meaning motion is detected—it will turn on the LED and print “Motion detected.” to the serial monitor. If it’s set to LOW—meaning no motion is detected—then the LED will be turned off and the message “All clear.” will be sent to the serial monitor.
Once you’ve uploaded the sketch to your Arduino, you’re ready to set up the wiring.
Setting up the PIR sensor and LED should be a simple affair. If you have a breadboard, it will make everything a lot easier to test with. First, we’ll connect the PIR sensor.
● Connect the ground pin on the sensor to GND.
● Connect the power pin on the sensor to 5V.
● Connect the signal pin on the sensor to digital pin 2.
Next, we’ll need to set up the LED. The Arduino Uno comes with a built-in LED that you can technically use for testing, as in the default Blink sketch. However, using an external LED will help you learn how to connect other lights or outputs in the future. Using an external LED is a common task, so you might be familiar with this already, but we’ll go over it away.
● Add an LED to your breadboard.
● Use a wire to connect the shorter LED pin to GND on your Arduino.
● Connect the longer LED pin to one end of a resistor.
● Use a wire to connect the other end of the resistor to digital pin 8.
Once all your connections are made, you’re ready to turn on your Arduino. Try waving your hand in front of the motion sensor to see the LED light up. If it’s not working, check your serial monitor to confirm if the sensor itself is working or not. Now, try adding other outputs to your project, like controlling a servo or adding a relay to control larger lights with your motion sensor.