DIY Infrared Motion Sensor System for Raspberry Pi

During the month of July 2015, I completed a high school internship at Colorado State University, and one of the projects that I worked on was that of a infrared proximity sensor for Raspberry Pi.

This short range infrared sensor can be attributed to a variety of applications, including line-following robots and motion activated cameras.

You will need:

1. Raspberry Pi (1)

2. Breadboard (1)

3. Perforated Prototyping Printed Circuit Board – Any size (1)

4. Coated Wire (Preferably Red, Black, and Yellow).

5. Infrared LED Emitter (1)

6. Infrared LED Detector (1)

7. Female – Female Jumper Cables (3)

8. Ordinary LED – any color (1)

9. Electrical Tape – OPTIONAL (1)

10. Header Pins – OPTIONAL (3)DIY Infrared Motion Sensor System for Raspberry Pi

11. LM358N9013 Operational Amplifier (1)

12. 1k Trimmer Potentiometer (1)

13. 39 Ohm Resistor (1)

15. 1k Ohm Resistor (1)

16. 10k Ohm Resistor (1)

17. 6.8k Ohm Resistor (1)

18. 3.5k Ohm Resistor (1)

19. Small Screwdriver – For use with Potentiometer (1)

20. Wire Stripper (1)

21. Wire Cutter (1)

22. Solder and Soldering Iron (1)

Step 1: Prototyping'

Before you can actually create your device, first take time to create a prototype using the breadboard and following the schematic provided above. Power should derive from the Raspberry Pi's 5v GPIO pin and is distributed to the infrared emitter, receiver, operational amplifier, and potentiometer: each with their respective resistor. Note that the operational amplifier is connected to two different resistors; this is intentional, and acts as a voltage divider that reduces the output voltage to 3.3v (maximum voltage allowed to input pins). Once completed, attach your prototype to the 5v and ground GPIO pins located on the Raspberry Pi using the female-female jumper cables. If the LED does not light, carefully rotate the knob on the potentiometer to the right and stop immediately after the light turns on. Doing so will maximize the range and sensitivity of the sensor. If the light turns upon supplying power, turn the knob to the left until the light turns off and then gently rotate the knob to the right, again stopping as soon as the light turns on.

Step 2: Building Your Device

Once a working prototype has been created, carefully transfer its components onto the perforated circuit board. Designate a side of the circuit board for the positive and negative terminals (similar to power strips on the breadboard) and make sure that each component is properly oriented. Solder each component into place and connect them using jumper cables as needed. Finally, attach the ground, output, and power to the 3 respective header pins. Use a wire cutter to remove any protruding wires and use the female-female jumper cables to test if your device is working properly. If not, consider using a multi-meter to verify all connections have been properly made.

Note: If you'd like, add a piece of electrical tape to the infrared transmitter. Doing so will increase the range of your sensor.DIY Infrared Motion Sensor System for Raspberry Pi schematic

Step 3: Code

After a functioning device has been created, code must be written to integrate the sensor with the Raspberry Pi Camera. Two examples have been provided below:

1. The first, “picam”, prompts the user to both name and set the preview window before taking a picture upon motion detection. The program then outputs the command needed to view the picture.

//PiCam: Takes picture when motion is detected. Will prompt user to supply picture name and preview length.
#include #include #include #include #define SEN 0 #define MAXSIZE 50 #define A “Y” #define B “O”

int system(const char *command);

int main() { wiringPiSetup(); pinMode(SEN, INPUT); char answer1[MAXSIZE]; char answer2[MAXSIZE]; char name[MAXSIZE] = B; char firstprefix[MAXSIZE] = “raspistill -hf -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 picutre will be taken after the specified time. \n3. To view image, enter in 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: DIY Infrared Motion Sensor System for Raspberry Pi

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top