About a year ago, I decided to try an build a night camera with my Raspberry Pi Zero that would be small and compact and take photos in the dark when triggered by a motion detector.
I hit a few challenges along the way and then some months went by without doing anything and now that we are back around to November, I decided to dust it off and give it another go.
Raspberry Pi Zero
The center piece of this entire thing. It is basically a much smaller version of the standard Raspberry Pi with a size of 65mmx30mmx5mm. The trade off for this is that it is slightly less powerful in terms of processing power and you have to solder on your own pins, but the version that I bought introduced the camera slot which was crucial for this. They have since released a version that has built in Wi-Fi, but the version that I have doesn't.
Pi Noir Camera Module
This is a Raspberry Pi compatible night vision camera. By night vision, I mean that it detects infra-red light (which is what we will use to light up the night instead of visible light). It works with any Raspberry Pi that has the camera port and the libraries for it are already installed on the OS.
Raspberry Pi Zero Camera Adapter
As the Pi Zero is smaller, so is the camera port. For that reason, to use the above camera on a Pi Zero, you will need to buy this ribbon. The ribbon on the camera is easily interchangeable so that makes it very easy.
PIR Motion Sensor Module
Finally, I used this motion sensor to trigger the camera. It requires no additional software and can provide input to the Raspberry Pi depending on whether it detects movement or not.
Mini RTC Module
This is not necessary for the camera itself but I wanted to make sure that the Pi had the correct time when I switched it on so that the photos have the correct timestamp. The Pi has no built in ability to keep the time when it is switched off. Instead, it uses the Internet to sync the clock when it boots up. As my camera will not have Internet access, this was not possible. This module is basically a clock with a battery built in that keeps the time. I then have to configure the Pi to use this time instead of its own.
How they connect
I tried to make a diagram to illustrate this but it took too long, so here is a an image of the pins on the Pi Zero and I will describe it to you (found here):
This is actually an older version of the Pi that doesn't have the camera port.
The motion sensor has 3 inputs that I connected with jumper cables. The power input which was plugged into number 2 that provides 5 volts of power. The ground cable that completes the circuit, I plugged into number 6 (but could have plugged into any of the other ground pins). The data pin that actually provides input could be plugged into any of the GPIO pins but I plugged it into pin 19 which is GPIO 10 (this become relavant in the code).
The clock could have been conntected with jumper cables, but I plugged it directly onto the pins such that power plugged into port number 1 which provides 3 volts. It takes 5 pins (so 1, 3, 5, 7 and 9). Plugging into the board directly is a lot cleaner than 5 more cables.
Here is a photo of everything plugged in together:
Lets start with programming the motion sensor and camera. What I want is for the camera to take a photo when the detector detects movement.
You may already know that scripts in the standard Pi OS are generally written in Python and there is no exception here.
Here is the script and an explanation of each part:
import RPi.GPIO as GPIO import time from picamera import PiCamera
So here, I am just importing some built in libraries that I will use later. The most notable one is the PiCamera.
You may have noticed from the above diagram that there are different ways of numbering the pins. This just sets it so that we use the GPIO numbers.
PIR_PIN = 10 GPIO.setup(PIR_PIN, GPIO.IN)
Here, I am creating a variable that stores the GPIO pin number of the data input of the motion detector. If you remember earlier, I plugged it into GPIO 10. I then set up that pin to expect an input (rather than an output).
try: except KeyboardInterrupt: print "Quit" GPIO.cleanup()
The entire code will go within the try block. This is because it will run until I stop it with the keyboard. When I do that, the
KeyboardInterrupt is detected and the program ends cleanly.
GPIO.cleanup() is important as if I didn't do this, when I run the script again it will error as it attempts to reassign an already in use pin.
Within the try block have the following code.
print "Motion Triggered Camera" camera = PiCamera() camera.exposure_mode = 'night' time.sleep(2) print "Ready"
The printing and sleeping is just for aesthetics (which won't be necessary when this program runs in the background). The important part here is that I am creating a new camera object and setting it to be in night mode.
while True: if GPIO.input(PIR_PIN): filename = time.strftime("%d_%m_%Y_%H_%M_%S") camera.capture('/home/pi/pic_'+filename+'.jpg') print "Picture Taken" time.sleep(3)
So I have a while loop as I want the program to keep working until I stop it. I have an if statement:
GPIO.input(PIR_PIN). So on every loop (which runs split seconds at a time) it will check if it received an input from the motion detector. If it did, it will run the code beneath it. If not, the loop will come round again and it will run the if statement again.
I supposed in theory there is a possibility that you could be detected in between the if statement failing and the next loop starting and not be captured, but this is hard, which is why there are delays when the picture is taken.
So I create a new file name using the current date and time.
I simply run the
capture function passing it the full path to save the picture as a parameter. It really is that simple!
I added a 3 second delay otherwise I would be flooded with pictures if something passed by the camera. So for 3 seconds, it will not attempt to check the motion detector again.
And that's it really! With just a few lines of code, I was able to get a motion triggered camera up and running.
Now for the clock. This required a system setup rather than code. I used this tutorial to set it up.
I won't copy and paste the instructions here. Go and check them out as they are pretty easy to follow. Once I had that, I was able to keep the correct time for the Pi even between power ups.
So that is Phase 1 of my project complete. In reality I actually add to clock last but I changed the order for these blog posts. But you can see just how simple it was to add a simple detector.
Next time, I will talk about how I added infra red illumination as well as how I powered it all away from the mains!