Build a DIY Infrared Motion Sensor for Raspberry Pi – Step‑by‑Step Guide
In July 2015 I completed a high‑school internship at Colorado State University, where I designed a short‑range infrared proximity sensor for the Raspberry Pi. The sensor can power line‑following robots, motion‑activated cameras, and more.
Below is a complete guide – from components to code – that demonstrates how to build a reliable, low‑cost infrared motion detector.
Materials
- Raspberry Pi (any model with GPIO)
- Single‑sided perforated prototyping board (any size)
- 1× Breadboard (for prototyping)
- Coated jumper wires (red, black, yellow)
- 1× IR LED emitter
- 1× IR LED receiver (phototransistor)
- 1× Standard LED (any colour) – visual indicator
- 1× 5 V power supply from the Pi’s 5 V GPIO pin
- 1× LM358N9013 dual operational amplifier
- 1× 1 kΩ trimmer potentiometer
- Resistors: 39 Ω, 1 kΩ, 10 kΩ, 6.8 kΩ, 3.5 kΩ
- Optional: electrical tape, 3× header pins, female‑female jumper cables, wire stripper, cutter, soldering iron, solder
Step 1 – Prototype on Breadboard
Follow the schematic (linked below) and power the circuit from the Pi’s 5 V GPIO pin. The LM358 outputs a voltage that is reduced to 3.3 V through a resistor divider so it stays within the Pi’s GPIO input limits.
After wiring, connect the breadboard to the Pi using female‑female jumper cables. If the LED does not light, adjust the potentiometer right until the LED turns on. If it turns on immediately after power‑on, turn the knob left until the LED turns off, then gently turn right again and stop when the LED lights.
Step 2 – Transfer to Perforated Board
Once the prototype works, solder components onto the perforated board. Keep a clear “+” and “–” side for power. Solder each part in place and use jumper wires for any necessary connections. Attach ground, output, and power to the header pins. Trim excess wire, test with a multimeter, and verify continuity.
Optional: wrap the IR LED with electrical tape to narrow its beam and extend effective range.
Step 3 – Programming the Pi
Use the following C program (requires WiringPi) to trigger the Pi camera when motion is detected. The script prompts for a file name and delay, then takes a picture.
#include <stdio.h>
#include <string.h>
#include <wiringPi.h>
#define SEN 0
#define MAXSIZE 50
#define A "Y"
#define B "O"
int main() {
wiringPiSetup();
pinMode(SEN, INPUT);
char answer1[MAXSIZE];
char answer2[MAXSIZE];
char name[MAXSIZE] = B;
char firstprefix[MAXSIZE] = "raspistill -hf -t ";
char secondprefix[MAXSIZE] = "xdg-open ";
printf("\nDo you want to name your picture first? (Enter Y/N and press ENTER): ");
scanf("%s", answer1);
if(strcmp(answer1, A) == 0){
printf("What do you want to call your video?: ");
scanf("%s", name);
strcat(name, ".jpg");
}
printf("How long do you wish the camera to wait before taking a picture? (Enter time in milliseconds and press ENTER): ");
scanf("%s", answer2);
strcat(firstprefix, answer2);
strcat(firstprefix, " -o ");
if(digitalRead(SEN) == LOW)
printf("\nCAMERA OFF\nError: Please check wiring OR move away from sensor.\n");
else {
printf("\nCAMERA ON\n1. To take picture, stand in front of camcorder.\n2. Your picture will be taken after the specified time.\n3. To view image, enter the command provided.\n\n");
for(;;){
if(digitalRead(SEN) == LOW){
system(strcat(firstprefix, name));
break;
}
}
}
strcat(secondprefix, name);
printf("Type *%s* and press ENTER to view your picture!\n", secondprefix);
return(0);
}
For more detail, visit the original project page: DIY Infrared Motion Sensor System for Raspberry Pi.
Manufacturing process
- SmartThings Motion Sensor Using Computer Vision on Raspberry Pi 3
- Secure Home Monitoring with Home Assistant on Raspberry Pi: Motion, Alarm, and Video Capture
- Control an LED with a PIR Motion Sensor on Raspberry Pi
- Build a Raspberry Pi Geiger Counter – Open‑Hardware Radiation Sensor Tutorial
- Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
- Master Raspberry Pi GPIO: Interfacing a PIR Motion Sensor on B+/Model 2
- How to Connect, Calibrate, and Program the HC‑SR501 PIR Motion Sensor with a Raspberry Pi
- Build a Raspberry Pi Obstacle‑Avoiding Robot – A Beginner’s Guide
- Smart Indoor Air Quality & Waste Monitoring System
- Infrared Eye‑Motion Tracking with Arduino Pro Mini – LED Control Demo