loading

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)

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.

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); }

2. The second, "pivid", prompts the user to both name and set the length of the recording before recording a video upon motion detection. The program then automatically plays the recording on the screen.

//PiVid: Records video when motion is detected. Will prompt user to supply video name and 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] = "raspivid -hf -hf -t "; char secondprefix[MAXSIZE] = "omxplayer "; printf("\nDo you want to name your video 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, ".mp4"); } printf("How long do you want to record? (Enter time in milliseconds and press ENTER): "); scanf("%s", answer2); strcat(firstprefix, answer2); strcat(firstprefix, " -o "); if(digitalRead(SEN) == LOW) printf("\nCAMCORDER OFF\nError: Please check wiring OR move away from sensor. \n"); else { printf("\nCAMCORDER ON\n1. To record video, stand in front of camcorder. \n2. Your video will record for the specified time. \n3. The video will automatically play after it is finished recording.\n\n"); for(;;){ if(digitalRead(SEN) == LOW){ system(strcat(firstprefix, name)); break; } } } strcat(secondprefix, name); printf("Type *%s* and press ENTER to view your video again!\n\n", secondprefix); system(secondprefix); return(0); }

Step 4: Your Done!

Congratulations! You have successfully completed your Raspberry Pi Motion Detection System! As a follow-up, consider writing code that would enable constant surveillance with the ability to record and save repeatedly without requiring user input.

I hope you had fun! Let me know what you think!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Acknowledgements:

I'd like to thank Associate Professor Sudeep Pasricha, the wonderful graduate students from Embedded Systems Lab, as well as my Internship Coordinator Kelly Kappel for making this phenomenal learning experience possible.

~Micah

<p>Range in this sort of device can be 5 or more meters, think about an ir remote. The secret is to modulate the ir and have a tuned receiver. The pi can probably provide the modulated signal via pwm or something similar ( otherwise perhaps a 555 timer ). Tuned recievers are available off the shelf for cheap. Look at ir remote projects.</p>
<p>Cool raspberry pi project. </p>

About This Instructable

4,636views

34favorites

More by m_zhang:DIY Infrared Motion Sensor System for Raspberry Pi 
Add instructable to: