ne of the most popular add-ons for the Raspberry Pi, the ofcial Camera Module –
or the new High Quality Camera – turns your favourite single-board computer into
O
succeeded by the higher-spec v2 in April 2016. The High Quality Camera was launched in April
2020, offers Ultra HD image resolution, and enables you to attach any C- or CS-mount lens.
and videos from the command line and writing Python programs to automate the process.
We’ll reveal how to create time-lapse and slow-motion videos, before moving on to exciting
projects including a Minecraft photo booth, wildlife camera trap, and smart door with video.
There are just so many things you can do with a Raspberry Pi camera!
Phil King, Editor
a powerful digital camera. Launched back in 2013, the original Camera Module was
In this book we’ll show you how to get started with your Raspberry Pi camera, taking photos
5
Page 6
Contents
Chapter 1: Getting started 008
Set up and connect your camera and start taking shots
Chapter 2: Precise camera control 016
Use command-line switches to access camera options and effects
Chapter 3: Time-lapse photography 020
Take photos at regular intervals, then turn the images into a video
Chapter 4: High-speed photography 024
Film dazzling slow-motion clips of exciting events
Chapter 5: Control the camera from Python 028
Use the picamera library to access the camera in Python programs
Chapter 6: Stop-motion and selfies 034
Wire up a physical push-button to take photos
Chapter 7: Flash photography using an LED 040
Add an LED ash to shoot images in low light
Chapter 8: Make a Minecraft photo booth 046
Build a booth in Minecraft that takes photos of the real world
6THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 7
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
Chapter 9: Make a spy camera 050
Set up a motion-activated spy camera in your room
Chapter 10: Smart door 054
See who’s at the door and know when the post has arrived
Chapter 11: Car Spy Pi 062
Use ANPR to identify who’s parked on your driveway
Chapter 12: Build a wildlife camera trap 070
Detect and photograph animals in your back garden
Chapter 13: Take your camera underwater 076
Explore the underwater world with your Raspberry Pi and camera
Chapter 14: Install a bird box camera 086
Observe nesting birds without disturbing them
Chapter 15: Live-stream video and stills 092
Stream video and regular stills to a remote computer
Chapter 16: Set up a security camera 102
Protect your home from intruders using motionEyeOS
Chapter 17: Quick reference 108
A guide to the camera hardware, commands, and picamera Python library
7
Page 8
Chapter 1
Getting started
Find out how to connect your High Quality Camera or
Camera Module, enable it, and take your first shots
n this chapter, we show you how to connect the High Quality Camera or Camera Module
to your Raspberry Pi using the supplied ribbon cable. We will then enable it in Raspbian,
I
before entering some commands in a Terminal window to start shooting photos and
video. Let’s get started…
01. High Quality Camera
The High Quality Camera (HQ Camera for short) can capture higher-resolution images than
the standard Camera Module. Unlike the latter, it doesn’t have a lens already attached.
Instead, it can be used with any standard C- or CS-mount lens; 6 mm and 16 mm lenses are
available to purchase with the camera to help you get started.
6 mm CS-mount lens
A low-cost 6 mm lens is available for the
HQ Camera. This lens is suitable for basic
photography. It can also be used for macro
photography because it can focus objects at
very short distances.
8THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 9
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
The dust cap should be used when a lens is not
attached, as the camera sensor is sensitive to dust
Supplied with the camera, the C-CS adapter
should be used when attaching a C-mount lens
Use this screw to lock the back focus adjustment
ring in position
This ring can be used to adjust the focus when
using a xed-focus lens, or to alter the focal range
of an attached adjustable-focus lens
This enables you to mount the camera on a
standard tripod: take care not to damage the
ribbon when screwing the tripod into the camera
The camera is supplied with a 20 cm ribbon cable
to connect it to Raspberry Pi’s Camera port, but
longer cables are available if needed
Fitting the lens
01
The 6 mm lens is a CS-mount device,
so does not need the C-CS adapter ring (see
diagram above). It won’t focus properly if the
adapter is tted – so, if necessary, remove it.
Then rotate the lens clockwise all the way into
the back focus adjustment ring.
Back focus adjustment
02
ring and lock screw
The back focus adjustment ring should be
screwed in fully for the shortest possible back-
focal length. Use the back focus lock screw to
make sure it does not move out of this position
when adjusting the aperture or focus.
9Chapter 1 Getting started
Page 10
Aperture
03
To adjust the aperture, hold the
camera with the lens facing away from you.
Turn the middle ring while holding the outer
ring, furthest from the camera, steady. Turn
clockwise to close the aperture and reduce
image brightness. Turn anti-clockwise to open
the aperture. Once you are happy with the light
level, tighten the screw on the side of the lens
to lock the aperture.
Focus
04
First, lock the inner focus ring, labelled
‘NEAR FAR’, in position by tightening its
screw. Now hold the camera with the lens
facing away from you. Hold the outer two
rings of the lens and turn them both clockwise
until the image comes into focus – it’ll take
four or ve whole turns. To adjust focus, turn
the outer two rings clockwise to focus on a
nearby object. Turn them anti-clockwise to
focus on a distant object. You may nd you
need to adjust the aperture again after this.
16 mm C-mount lens
The 16 mm lens provides a higher-quality
image than the 6 mm lens. It has a narrow
angle of view which is more suited to
viewing distant objects.
Fitting the
01
C-CS adapter
Ensure the C-CS adapter that comes with the
HQ Camera is tted to the 16 mm lens. The
lens is a C-mount device, so it has a longer
back focus than the 6 mm lens and therefore
requires the adapter.
10THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 11
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
Fitting the lens
02
to the camera
Rotate the 16 mm lens and C-CS adapter
clockwise all the way into the back focus
adjustment ring.
Back focus adjustment
03
ring and lock screw
The back focus adjustment ring should be
screwed in fully. Use the back focus lock screw to
make sure it does not move out of this position
when adjusting the aperture or focus.
Aperture
04
To adjust the aperture, hold the camera
with the lens facing away from you. Turn the
inner ring, closest to the camera, while holding
the camera steady. Turn clockwise to close the
aperture and reduce image brightness. Turn anti-
clockwise to open the aperture. When happy with
the light level, tighten the screw on the side of
the lens to lock the aperture into position.
Focus
05
To adjust focus, hold the camera with the
lens facing away from you. Turn the focus ring,
labelled ‘NEAR FAR’, anti-clockwise to focus
on a nearby object. Turn it clockwise to focus on
a distant object. You may nd you need to adjust
the aperture again after this.
11Chapter 1 Getting started
Page 12
02. Connecting and
using the camera
With your HQ Camera with lens – or your Camera Module – ready, it’s time to connect it to
Raspberry Pi and start to capture some images.
Connect ribbon cable to camera
01
On the HQ Camera or Camera Module board, you’ll
nd a at plastic connector. Carefully pull the sticking-out
edges until the connector pulls part-way out. Slide the ribbon
cable, with the silver edges downwards and the blue plastic
facing upwards, under the ap you just pulled out, then push
the ap gently back into place with a click (Figure 1); it doesn’t
matter which end of the cable you use. If the cable is installed
properly, it will be straight and won’t come out if you give it a
gentle tug; if not, pull the ap out and try again.
Figure 1
Figure 2
12THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 13
Figure 3
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
Connect cable to Raspberry Pi
02
Find the Camera port on Raspberry Pi and pull the plastic ap gently upwards. With
Raspberry Pi positioned so the HDMI port is facing you, slide the ribbon cable in so the silver
edges are to your left and the blue plastic to your right (Figure 2), then gently push the ap
back into place. If the cable is installed properly, it’ll be straight and won’t come out if you
give it a gentle tug; if not, pull the ap out and try again.
If using a Raspberry Pi Zero, its Camera port is found on the edge of the board. However,
as it’s a smaller size than the regular one on other Raspberry Pi models, you’ll need a camera
adapter cable to use it.
Enable the camera
03
Connect the power supply back to Raspberry Pi and let it load Raspbian. Before you
can use the camera, you’ll need to tell Raspberry Pi it has one connected: in the Raspbian
menu, select Preferences, then Raspberry Pi Conguration. When the tool has loaded, click
the Interfaces tab, nd the Camera entry in the list, and click on the round radio button to the
left of ‘Enabled’ to switch it on (Figure 3). Click OK, and the tool will prompt you to reboot
your Raspberry Pi. Do so and your camera will be ready to use.
Test the camera
04
To conrm that your camera is correctly installed, you can use the raspistill tool.
This, along with raspivid for videos, is designed to capture images from the camera using
Raspberry Pi’s command-line interface (CLI). In the Raspbian menu, select Accessories, then
13Chapter 1 Getting started
Page 14
Figure 4
Terminal. A black window with green and blue writing in it will appear (Figure 4): this is the
Terminal, which allows you to access the command-line interface.
To take a test shot, type the following into the Terminal:
raspistill -o test.jpg
As soon as you hit the ENTER key, you’ll see a large picture of what the camera sees
appear on-screen (Figure 5). This is called the live preview and, unless you tell raspistill
otherwise, it will last for ve seconds. After those ve seconds are up, the camera will
capture a single still picture and save it in your home folder under the name test.jpg. If you
want to capture another, type the same command again – but make sure to change the
output le name, after the -o, or you’ll save over the top of your rst picture.
More advanced commands
05
The raspistill command has a list of options so long that it borders on the
intimidating. Don’t worry – you won’t need to learn them all, but there are a few that might be
useful to you, such as:
raspistill -t 15000 -o newpic.jpg
The -t option changes the delay before the picture is taken, from the default ve seconds
to whatever time you give it in milliseconds – in this case, you have a full 15 seconds to get
14THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 15
Figure 5
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
your shot arranged perfectly after you press ENTER. You can explore more camera options
in the next chapter, or by referring to Chapter 17.
Rotate the image
06
If the live preview was upside-down, you’ll need to tell raspistill the camera is
rotated. The Camera Module is designed to have the ribbon cable coming out of the bottom
edge; if it’s coming out of the sides or the top, as with some third-party camera mount
accessories, you can rotate the image by 90, 180, or 270 degrees using the -rot switch. For
a camera mounted with the cable coming out of the top, use the following command:
raspistill -rot 180 -o test.jpg
SHOOTING VIDEO
For shooting video, raspivid is what you need. Try it out with this Terminal command:
raspivid -t 10000 -o testvideo.h264
This records a ten-second video (10,000 milliseconds) at the 1920 × 1080 resolution. You can also
You can use VLC to play the videos back – see Chapter 4 for more details.
15Chapter 1 Getting started
Page 16
Chapter 2
Precise
camera control
Use command-line switches to access a variety
of camera options and effects
o, you’ve connected the HQ Camera or Camera Module to your Raspberry Pi and
learned how to take still photos and shoot videos from the command line. Now
S
switches and options available. We’ll also take a look at the raspiyuv command, which sends
its unencoded YUV or RGB output directly from the camera component to a le.
is the preview window that appears by default on the screen. First of all, if it’s upside-down,
justadd -rot 180 to your raspistill or raspivid command to rotate it. Also, adding -hf
and/or -vf will ip the image horizontally and/or vertically.
width. The -p switch takes four parameters: x co-ordinate, y co-ordinate, width, and height. So,
for example:
let’s explore the raspistill and raspivid commands further, including the many
Preview mode
01
When taking stills or shooting video, one of the rst things you might want to alter
Using the -p switch, you can set the window’s on-screen position, along with its height and
raspistill -o image.jpg -p 20,100,1280,720
…would place the preview window’s top-left corner at co-ordinate (20,100), with a width of
1280 pixels and height of 720 pixels.
Note that if you only want to see a preview without taking a shot, you can simply omit the
-o image.jpg part. The -t switch sets the duration of the preview: you can set it to 0 to make
it stay on screen until you press CTRL+C.
If you want a full-screen preview, this is easily achieved using the -f switch. The -op switch
can be used to adjust the preview’s opacity, from 0 (invisible) to 255 (solid). If you want to
disable the preview window completely, use the -n switch.
16THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 17
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
The preview can be resized and positioned manually, and can also have its opacity adjusted
Camera control options
02
Like most dedicated digital cameras, the Raspberry Pi HQ Camera or Camera Module
offers a range of options to adjust aspects such as brightness (-br, from 1 to 100), contrast
(-co, -100 to 100), sharpness (-sh, -100 to 100), saturation (-sa, -100 to 100), ISO (-ISO, 100 to
800), and EV compensation (-ev, -10 to 10).
In addition, there are numerous options for exposure mode for shooting in certain scenarios,
akin to the ‘scenes’ found on most digital cameras. Just use the -ex switch followed by one
of the following terms: auto, night, nightpreview, backlight, spotlight, sports, snow,
beach, verylong (long exposure), fixedfps (for video only), antishake, or fireworks.
Automatic white balance can be adjusted by following the -awb switch with one of: off, auto,
sun, cloud, shade, tungsten, fluorescent, incandescent, flash, or horizon.
You can set the shutter speed in microseconds with the -ss switch; the upper limit depends
on the exposure mode and other settings. The metering mode – used for preview and capture
– can be set with -mm to one of the following: average, spot, backlit, or matrix.
There’s also the option of restricting the region of interest to only part of the sensor, using
-roi with parameters for x and y co-ordinates (from top left), width, and height. For example,
to set a ROI halfway across and down the sensor, with quarter-size width and height, you’d use:
-roi 0.5,0.5,0.25,0.25.
Keypress mode
03
If you’d like to take a still photo at an exact time, rather than having to wait for the
-t switch delay time to elapse, keypress mode is your friend. Just add the -k switch to your
raspistill command, then press the ENTER key to take the shot: it acts like a shutter button.
To exit the procedure, press X followed by ENTER.
17Chapter 2 Precise camera control
Page 18
A multitude of real-time effects may be added to images, including emboss, as shown here
By adding %04d to the end of your le name in the command, you can save every shot you
have taken before aborting:
raspistill -o keypress%04d.jpg -k
Each shot will have a four-digit sequential number added to its le name, so you’ll get
keypress0000.jpg, keypress0001.jpg, keypress0002.jpg, etc. This is a useful technique for
time-lapses using the -tl switch, too: see Chapter 3 for more details.
Image effects
04
A whole bunch of effects can be added to the camera in real-time, shown in the
preview window. This is achieved by using the -ifx switch followed by one of the following
saturation of the image), colorswap, washedout, colorpoint, colorbalance, or cartoon.
If you’d like to take monochrome images, you can use the -cfx (colour effect) switch to
achieve this, using the following setting: -cfx 128:128.
To increase contrast between dark and light areas using DRC (dynamic range compression),
use the -drc switch to turn it on/off (it’s off by default).
18THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 19
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
Still options
05
Let’s take a look at some options that are specic to the raspistill command. As
already mentioned, we use -o followed by a le name to output to a le, and the -t switch sets
the shutter delay in milliseconds. For example, to save a photo taken after two seconds, use:
raspistill -t 2000 -o image.jpg
You can set the width and height of the image with -w and -h, each followed by a value – up to
4056 and 3040 (HQ Camera), 3280 and 2464 (Camera Module v2), or 2592 and 1944 (CM v1).
You can also set the quality of the JPEG image, using -q, from 0 to 100 – the latter is almost
completely uncompressed. Alternatively, to save it as a lossless PNG (slower than using JPG),
use -e (encoding) followed by png:
raspistill -o image.png –e png
For a full list of options, see Chapter 17. The raspiyuv command works in a similar fashion
and offers most of the same options, apart from adding EXIF tags, but sends its YUV or RGB
output directly from the camera component to le. To use RGB, add the -rgb switch.
Shooting video
06
The raspivid command is used to shoot video. In this case, the -t switch sets the
duration in milliseconds. The bitrate is set using -b, in bits per second (so, 25Mpbs is -b
25000000), while -fps sets the frame rate – see Chapter 17 for the maximum bitrate and
frame rate for your camera model. For example, to shoot ve seconds of video at 1080p
(1920 × 1080), with a bitrate of 15Mbps and frame rate of 30fps, use:
ime-lapse photography reveals exciting things about the world which you wouldn’t
otherwise be able see. These are things that happen too slowly for us to perceive:
T
shadows moving across the land. In this chapter, we’ll be making a Raspbian-based device
that lets you watch things that are too slow to observe with the naked eye. To do this, we will
capture lots of still photographs and combine these frames into a video with FFmpeg/libav,
which can then be accessed via a web browser.
bread rising and plants growing; the clouds, sun, moon, and stars crossing the sky;
Time-lapse photography is ideal for capturing lengthy natural processes
20THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 21
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
Connect the camera
01
First, with Raspberry Pi turned off, connect the HQ Camera or Camera Module to it
with the included ribbon cable. As mentioned in Chapter 1, you need to locate the correct
camera socket on your Raspberry Pi, labelled ‘CAMERA’ (or ‘CSI’ on some models). Carefully
lift up its plastic slider and pull it away from Raspberry Pi gently but rmly; it will move up a
short distance, opening up the connector. Insert the ribbon cable into the socket, with the blue
side – or white tab on a Raspberry Pi Zero camera cable – facing the plastic slider (and the
metal contacts facing the other way). Finally, hold the ribbon cable in position and push the
slider back down to clamp the cable rmly in place.
Enable and test the camera
02
Power up your Raspberry Pi and enable the camera in the Interfaces tab of the
Raspberry Pi Conguration tool (found under Preferences), then reboot your Raspberry Pi.
Open a Terminal window or establish a secure shell (SSH) connection (to access it from a
remote computer). You can test the camera by running the following:
raspistill -o testimage.jpg
After ve seconds (if using an original Camera Module v1, its red LED should light up during
this time), a JPEG image will be saved to the current directory. Ifthe camera is mounted
upside-down, you can use the rotate command-line switch (-rot 180) to account for this.
Install and configure software
03
Install a web server so you can access your captured images remotely. Run this
command to install Apache:
sudo apt-get install apache2
Remove the default page to see the contents of the directory:
sudo rm /var/www/html/index.html
Visit the IP address of your Raspberry Pi (e.g. http://192.168.1.45 – you can nd this by using
hostname -I) on another computer and you should see an empty directory listing. If you run
the following command and refresh the page, you should see an image le listed. You run this
as a superuser (by using sudo) so you can write to the directory.
sudo raspistill -o /var/www/html/testimage.jpg
Click on the le link on the remote computer and you’ll see the image in your browser.
21Chapter 3 Time-lapse photography
Page 22
Some bread dough ready to prove. Watch it rise in your video. Be careful
not to move the bowl or camera during lming
Capture the images
04
Set up your scene and check the positioning of the camera.
sudo raspistill -w 1920 -h 1080 -o
/var/www/html/testimageFullHD.jpg
The width and height have been changed to capture a smaller image in 16:9 aspect ratio. This
makes things easier later. The top and bottom are cropped, so make sure that your subject is
a set of special adapters allows you to extend using a standard HDMI cable.
Avoid unwanted reflections, and
fine-tune your video specifications
Note that if using a Camera Module v1, its red LED will illuminate when recording is taking
disable_camera_led=1 to your /boot/cong.txt
The command for capturing video with the Raspberry Pi camera is raspivid, best run from
a Terminal window. There are a number of command options that you need to specify:
• -fps sets the frame rate.
• -w and -h specify the width and height of the frames. For the fastest frame rates, set this to
640 and 480 respectively.
• -t allows you to set how long to record for. If you’re working by yourself, the easiest way to
The camera preview can be resized and positioned to your liking
28THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 29
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
With your camera already connected and enabled in Raspberry Pi Conguration, open
Programming > Thonny from the Raspbian desktop menu. Create a new le by clicking
File > New le. Save it with File > Save, naming it ch5listing1.py. Note: Never name a le
picamera.py, as this is the le name for the picamera library itself!
Now enter the code from ch5listing1.py. Save it with CTRL+S and run with F5. The full-
screen camera preview should be shown for ten seconds, and then close. Note: To be able
to see the preview when using VNC for remote access from another computer, open the VNC
Server menu and go to Options > Troubleshooting, then select ‘Enable direct capture mode’.
If the preview appears upside-down, add the line camera.rotation = 180 just above
camera.start_preview(). Other possible rotation values are 90 and 270.
You can alter the transparency level of the preview by entering an alpha value – from 0 to
255 – within the latter command’s brackets; e.g. camera.start_preview(alpha=200).
It’s also possible to change the position and size of the preview. For example, to place its
top corner 50 pixels right and 150 down, and resize it to 1024 × 576:
Here, the %s token is replaced by whatever we add after the % following the le name – in this
case, the variable i set by our for loop. Note that i will range from 0 to 4, so the images will
29Chapter 5 Control the camera from Python
Page 30
Using the ch5listing5.py code, you can view a loop of all the effects on offer
be saved as image0.jpg, image1.jpg, and so on. Once they’re all taken, the preview will close.
In this example, you’ll see the ve les on your desktop; double-click to open them.
You can also use a for loop to alter camera setting levels such as brightness over time. For
more details, see Step 04.
Control camera settings
04
Brightness is just one of numerous settings available for the camera. Here’s a list of
the main options, along with their default values (and ranges where applicable):
The resolution of the capture is also congurable. For example:
30THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 31
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
camera.resolution = (1024, 768)
The maximum resolution for photos is4056 × 3040 (HQ Camera), 3280 × 2464 (Camera
Module v2), or 2592 × 1944 (Camera Module v1). Note: you may need to increase gpu_mem in
/boot/cong.txt to achieve full resolution with the Camera Module v2.
Add image effects
05
Just as when you are using the command line, a wide range of effects can be added to
the camera in real-time, shown in the preview window. The camera.image_effect command
is used to apply a particular image effect. The options are: none (the default), negative,
colorbalance, cartoon, deinterlace1, and deinterlace2.
For instance, to take an image with a colour swap effect, enter the code from ch5listing4.py
and run it.
To loop through thevarious image effects in a preview, run the code from ch5listing5.py. Note
that this uses the camera.annotate_text command to add a text message to the preview; this
can also be applied to captured images (when using the sensor’s full eld of view).
For more details on these effects and other settings, see Chapter 17 or the ofcial picamera
documentation at picamera.readthedocs.io.
Shoot a video
06
To shoot video footage, we replace the camera.capture() command with
camera.start_recording(), and use camera.stop_recording() to stop. Enter the
example code from ch5listing6.py.
When you run the code, it records ten seconds of video before closing the preview. To play
the resulting le, open a Terminal window from the desktop and enter:
oxplayer video.h264
(Or you can use VLC instead.) Note that it may well play faster than the original frame rate. It’s
possible to convert videos to MP4 format and adjust the frame rate using the MP4Box utility
(installed with sudo apt-get install gpac), like so:
MP4Box -add video.h264:fps=30 video.mp4
All of the image effects and most of the camera settings can be applied while shooting video.
You can also turn on video stabilisation, which compensates for camera motion, by adding the
following line to your Python program:
camera.video_stabilization = True
31Chapter 5 Control the camera from Python
Page 32
ch5listing1.py / Python 3
DOWNLOAD
from picamera import PiCamera
from time import sleep
camera =PiCamera()
camera.start_preview()
sleep(10)
camera.stop_preview()
ch5listing2.py / Python 3
from picamera import PiCamera
from time import sleep
Because we’re focusing on stop-motion for our rst project, we’re using the camera’s
preview mode so that we can set up our shot before we take it, to ensure everything is in the
frame. Then, only when the button is pressed do we save an image le. Each image le will
have a different name based on the date and time at which it is taken. This makes it easy to
assemble all the images from the shoot for post-processing.
The wonderful GPIO Zero library is used to capture the button activity; we simply dene a
function that is run whenever the button is pressed. This function uses the picamera Python
library which allows us to control the camera through code, making all the normal command-
line operations available.
Download or type up the code from ch6listing1.py and either run it through Thonny or the
command line. To quit the program, press CTRL+C.
One button leg is wired to a GPIO pin
(we used GPIO14); the other to GND
35Chapter 6 Stop-motion and seles
Page 36
Other variations
04
You should be able to use this code as a template to create a program for whatever
photography project you have in mind. For example, you could alter the code so that the
camera takes continuous photos while the button is held down. Or you could add extra buttons
to make a variety of photography modes available.
With this sort of build, you can also start thinking about building a complete, portable,
wirelessly connected Raspberry Pi camera. For this, you can use a case into which you can t
a portable mobile phone battery charger, along with a screen to attach to Raspberry Pi. With
a bit of modication of the code, you can have it always show the preview of the camera on
the screen. Want to record video? More modication of the code will allow for video capturing.
The only issue you might have with both of these projects is the lack of a ash or built-in light
source, so a well-lit subject would be essential.
Selfie stick
05
Next, we’ll look at making a sele stick. A lot of people roll their eyes and complain
about vanity when it comes to the art of the sele, but we all know it’s nothing like that. New
outt? New glasses? Eyeliner wings perfectly symmetrical today? Why not chronicle it? It’s a
great condence boost.
You can use a breadboard for a small button, or connect your jumper
wires directly to the pins on a bigger one
36THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 37
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
ch6listing1.py / Python 3
from datetime import datetime
from gpiozero import Button
import picamera
import time
b=Button(14)
pc=picamera.PiCamera()
running =True
#pc.resolution = (1024, 768)
#use this to set the resolution if you dislike the
default values
timestamp=datetime.now()
defpicture():
pc.capture('pic'+str(timestamp)+'.jpg') #taking the
picture
pc.start_preview() #running the preview
b.when_pressed=picture
try:
while running:
print('Active')#displaying 'active' to the shell
time.sleep(1)
#we detect Ctrl+C then quit the program
exceptKeyboardInterrupt:
pc.stop_preview()
running =False
DOWNLOAD
magpi.cc/cameragit6
Our test sele stick is very DIY, but you can use anything as long
as you can attach a Raspberry Pi and have a long enough wire
37Chapter 6 Stop-motion and seles
Page 38
Create your stop-motion scene and use the button to trigger the camera to
take pictures and save them to timestamped le
Our Raspberry Pi-powered sele stick will use a similar hardware and software setup to the
stop-motion animation project. As before, we’re wiring up a push-button to GPIO 14 and GND
pins on Raspberry Pi, but this time we need to attach the jumpers to longer wires to put the
button at the end of the ‘stick’ – we used a spatula, but anything long will do.
Your Raspberry Pi needs to be near to the camera (unless you’ve got an extra-long ribbon
cable). Attach Raspberry Pi in a case to one end of the stick with whatever means you see t
(glue, adhesive putty, string, etc.) and then attach the button.
Add the code
06
Since the principle is the same – pressing a button to take a photo – we can use the
same code, ch6listing1.py, as for the stop-motion project. This time we don’t need the camera
preview, so you can comment out the line pc.start_preview() if you like, by adding a # to
the start of it.
Try running the code. Pressing the button will take a photo, but you’ll need to practise your
aim so you can get yourself in the frame. As before, we add a timestamp to each picture,
which helps to organise your pictures later and also results in a slight pause in the code, which
at least means you won’t take too many pictures with a slip of the button.
38THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 39
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
39Chapter 6 Stop-motion and seles
Page 40
Chapter 7
Flash photography
using an LED
Add an LED flash to shoot images in low light
he Raspberry Pi Camera Module
or HQ Camera works really well in
T
there’s less light available? Here we show you
how to set up a simple LED ash, which will be
triggered each time you take a photo, using the
picamera Python library. We also take a look at how to shoot better images in low light when
you are not using a ash.
then be triggered each time we capture a still using picamera with the ash mode set to on. To
do this, we need to edit the VideoCore GPU default device tree source. First, install device tree
compiler with:
Then grab a copy of the default device tree source with:
good lighting conditions, but what if
Download device tree source
01
Before we can wire up a ash, we need to congure a GPIO pin to use for it. This will
Edit the le using your favourite text editor, such as nano:
YOU’LL NEED
• Camera Module / HQ Camera
• White LED
• Resistor
sudo nano dt-blob.dts
40THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 41
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
You’ll need to nd the correct part of the code for the Raspberry Pi model you’re using; for
instance, the part for Raspberry Pi 4 is found under pins_4b {.
Here you’ll nd pin_config and pin_defines sections. In the pin_config section, add a
line to congure the GPIO pin (we’re using GPIO 17) that you want to use for the ash:
pin@p17 { function = "output"; termination = "pull_down"; };
Enable flash
03
Next, we need to associate the pin we added with the ash enable function by editing
it in the pin_define section. We simply change absent to internal and add a line with the pin
number, so it looks like the following:
pin-define@FLASH_0_ENABLE {
type = "internal";
number = <17>;
};
Note that it’s the FLASH_0 section that you need to alter: FLASH_1 is for an optional privacy
LED to come on after taking a picture, but we won’t bother with that.
The resistor limits the current
owing through the LED
The longest leg of the LED is the
anode: connect it to GPIO 17
Figure 1 Connect a white LED
41Chapter 7 Flash photography using an LED
Page 42
Wire the white LED to GPIO 17 and GND via a low-ohmage resistor
Compile the blob
04
With the device tree source updated, we now need to compile it into a binary blob,
using the following command in a Terminal window:
dtc -q -I dts -O dtb dt-blob.dts -o dt-blob.bin
This should output nothing. Next, you need to place the new binary on the rst partition of the
microSD card. In the case of non-NOOBS Raspbian installs, this is generally /boot, so use:
sudo cp dt-blob.bin /boot/
In you installed Raspbian via NOOBS, however, you’ll need to do the following instead:
To activate the new device tree conguration, reboot your Raspberry Pi.
42THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 43
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
You need to edit the device tree source to enable a GPIO pin for the ash
Wire up the LED
05
Connect a white LED – we used a 5 mm one – to your Raspberry Pi as in Figure 1
(previous page). The LED’s anode (long leg) is connected to our ash-enabled pin, GPIO 17. To
be sure of the LED not burning out from excess current, you should add a low-ohmage resistor
(such as 100 Ω) between the LED’s cathode (short leg) and Raspberry Pi’s GND pin. Depending
on the maximum forward voltage of your LED (ours was 3.5V), you may be able to get away
without using one, but it’s best to be safe.
If you want to use higher-powered or multiple LEDs, you’ll have to think about powering
them via a suitable driver circuit, with a transistor wired to the ash pin. You may also need a
separate power supply. Note that, due to the Raspberry Pi camera’s rolling shutter, only an LED
or equivalent ash is suitable: you can’t use a xenon ash. Alternative ash/lighting methods
include NeoPixel sticks and the LISIPAROI light ring.
Test it out
06
With the LED connected, we can now test out our ash with a short Python program.
In Thonny, create a new le and enter the code from ch7listing1.py. The camera.flash_mode = 'on' line sets the ash to trigger when we issue the capture command below; the LED will
light up briey before the image capture, to enable the camera to set the correct exposure level
for the extra illumination, before the ash proper is triggered.
If you want the ash to trigger automatically only when it’s dark enough, you can change the
penultimate line of the code to camera.flash_mode = 'auto'.
43Chapter 7 Flash photography using an LED
Page 44
Using a long exposure, you can shoot stills in very dark settings
Low-light photography
07
In low-light scenarios where you don’t want to use a ash, you can improve capture
of images using a few tricks. By setting a high gain combined with a long exposure time, the
camera is able to gather the maximum amount of light. Note that since the shutter_speed
attribute is constrained by the camera’s frame rate, we need to set a very slow frame rate.
The code in ch7listing2.py captures an image with a six-second exposure time: this is the
maximum time for the Camera Module v1 – if you have a v2 Camera Module, it can be
extended to ten seconds, or much longer for an HQ Camera. The frame rate is set to a sixth of
a second, while we set the ISO to 800 for greater exposure. A pause of 30 seconds gives the
camera enough time to set gains and measure AWB (auto white balance).
Try running the script in a very dark setting: it may take some time to run, including the
30-second pause and about 20 seconds for the capture itself. Note: if you’re getting a timeout
error, you may needto do a full Raspbian upgrade with sudo apt-get update and sudo apt-get dist-upgrade.
The particular camera settings in this
script are only useful for very low light
conditions: in a less dark environment, the
image produced will be heavily overexposed,
so you may need to increase the frame rate
and lower the shutter speed accordingly.
If the image has a green cast, you’ll need
to alter the white balance manually. Turn
AWB off with camera.awb_mode = 'off'.
Then set the red/blue gains manually;.e.g.
camera.awb_gains = (1.5, 1.5).
Even a single LED can provide illumination
for close-up photography
44THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 45
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
ch7listing1.py / Python 3
import picamera
with picamera.PiCamera() as camera:
camera.flash_mode ='on'
camera.capture('foo.jpg')
magpi.cc/cameragit7
ch7listing2.py / Python 3
from picamera import PiCamera
from time import sleep
from fractions import Fraction
# Set a framerate of 1/6fps, then set shutter
# speed to 6s and ISO to 800
camera =PiCamera(resolution=(1280, 720),
framerate=Fraction(1, 6))
camera.shutter_speed =6000000
camera.iso =800
# Give the camera a good long time to set gains and
# measure AWB (you may wish to use fixed AWB instead)
sleep(30)
camera.exposure_mode ='off'
# Finally, capture an image with a 6s exposure. Due
# to mode switching on the still port, this will take
# longer than six seconds
camera.capture('dark.jpg')
DOWNLOAD
45Chapter 7 Flash photography using an LED
Page 46
Chapter 8
Make a Minecraft
photo booth
Create a photo booth in Minecraft that takes photos
of the real world. What will you see on your travels?
ot only is Minecraft Pi great fun to play around with, you can also use Python
programming to manipulate the Minecraft world and create various structures
N
chapter, we’ll be getting Minecraft to trigger the Camera Module or HQ Camera with code when
the player enters a virtual photo booth.
interface). This enables you to connect to Minecraft and program it with Python. You also need
to import picamera’s PiCamera class to control the camera, and the time module to add a
small delay between taking each photo.
the Recommended Software tool), then enter an existing world or create a new one. Move
the Minecraft window to one side of the screen. You’ll need to use the TAB key to take your
mouse’s focus away from the Minecraft window to move it. This will be needed later when you
switch between the Minecraft and Python windows.
to write the photo booth program.
program with F5. You should see the message ‘Find the photobooth’ appear in the Minecraft
world. This is the rst part of the code. Stop the program running using CTRL+C.
within it. Going beyond this, you can even have it interact with the real world. In this
The rst thing you need to do is import the Minecraft API (application programming
Open Minecraft from the applications menu (if it's not present under Games, install it via
Open Thonny from the applications menu. This will open up the code editor which you’ll use
Enter the code from ch8listing1.py, or download it. Save with CTRL+S and run the
Camera tests
Next, we’ll make sure the camera is set up. We’ve set the camera to show a two-second
preview, so that you can strike your pose and smile before the picture is taken. The image is
stored as a le called sele.jpg in your home directory (/home/pi).
46THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 47
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
Now, you need to create a photo booth in the Minecraft environment. This is done manually,
and the booth can be built wherever you want to locate it. Using any block type, build your
photo booth. It can be any shape you like, but it should have at least one block width of free
space inside so that the player can enter.
Once you have created your photo booth, you need to be able to move your player inside and
onto the trigger block. This is the block that the player stands on to run the function that you
wrote in the rst step, which will then trigger the camera. In the Minecraft environment, your
position is given in reference to the x, y, and z axes. Look at the top-right of the window and
you’ll see the x, y, and z co-ordinates of your player – for example, 10.5, 9.0, -44.3. Assuming
you are still in the photo booth, then these are also the x, y, and z co-ordinates of the trigger
block in your booth.
Walk into your photo booth
Note down all three co-ordinates of your camera trigger block. When you’re playing Minecraft,
your program will need to verify that you areinside the photo booth. If you are, then it will
trigger the take_the_pic function and take a picture with the camera. To do this, Minecraft
needs to know where you are in the world.
47Chapter 8 Make a Minecraft photo booth
Page 48
Construct your photo booth however
you wish; just make sure the code
knows where it lives
Steve is your ‘shutter’ in the
Minecraft world: move him
to the booth to take a photo
Place the booth anywhere in your world.
Give it a special room in your house, or use
it as a trap to see if someone is in your world
To nd your position, you use the code x, y, x = mc.player.getPos(). This saves the
x, y, and z position of your player into the variables x, y, and z. You can then use print(x) to
print the x value, or print(x, y, z) to see them all if you wish, by adding it to the code. Now
you know the position of the player, you can test to see if they’re in the photo booth.
At this point we have a photo booth, the co-ordinates of the trigger block, and code to
control the camera and take a picture. The next part of the code is to test whether the program
knows when you’re in the photo booth. To do this, we create a loop which checks if your
player’s co-ordinates match the trigger block co-ordinates. If they do, then you’re standing in
the photo booth. For this, we use a simple if statement, which is known as a conditional.
Change the if line in the code to ensure the co-ordinates you enter are those of your photo
booth. Save and run your code to test it: walk into your photo booth and you should see the
message ‘You are in the photobooth!’ in the Minecraft window.
You will note that the if statement checks if the x value is greater than or equal to 10.5: this
is to ensure that it picks up the block, as it could have a value of 10.6. Remember to replace
the x, y, and z values with those from your photo booth. After the message is printed, the same
preview and camera snap will happen as before the while loop. The loop then resets itself so
you can enter it again and take another photo!
48THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 49
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
ch8listing1.py / Python 3
from mcpi.minecraft import Minecraft
from picamera import PiCamera
from time import sleep
if x >=10.5and y ==9.0and z ==-44.3:
mc.postToChat("You're in the photo booth!")
sleep(1)
mc.postToChat("Smile!")
sleep(1)
camera.start_preview()
sleep(2)
camera.capture('/home/pi/selfie.jpg')
camera.stop_preview()
DOWNLOAD
magpi.cc/cameragit8
sleep(3)
49Chapter 8 Make a Minecraft photo booth
Page 50
Chapter 9
Make a spy camera
Set up a motion-activated spy camera in your room
e’ve all been there. You’ve gone
out for the day and you know you
W
come back and it’s slightly ajar. Who’s been in
there? Were they friend or foe? In this chapter
we’ll use the Camera Module or HQ Camera
as a spy camera that takes a picture when
anyone’s presence is detected by a passive
infrared (PIR) sensor. Here we’re using a
Raspberry Pi Zero – which is easier to hide away due to its size – with a special camera cable
for it, but you can use any Raspberry Pi model. Unless you want to power it from the mains,
you’ll also need a portable power supply such as a mobile phone battery pack.
01
using a Raspberry Pi Zero, you’ll need a special adapter cable since its camera connector is
smaller: the cable’s silver connectors should face the Raspberry Pi circuit board. You’ll also
need to have enabled the camera in Raspberry Pi Conguration, as explained in Chapter 1.
We’ll be using the picamera Python library to trigger our spy camera, so if you haven’t yet
First, connect your Camera Module or HQ Camera to Raspberry Pi. Note that if you’re
YOU’LL NEED
• Camera Module / HQ Camera
• PIR sensor magpi.cc/pir
• Raspberry Pi Zero camera cable
(optional) magpi.cc/zerocamcable
• Portable power supply (optional)
• Jumper wires
Wire up the circuit
02
The circuit for this is fairly simple, especially as the PIR does not need a resistor as
part of its setup. The PIR comes with three connection pins: VCC, GND, and OUT. If you can’t
50THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 51
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
Jumper wires are used to connect the
PIR’s pins to Raspberry Pi’s GPIO, either
to the pins of a header or to the hole
contacts if none has been attached
The passive infrared (PIR)
sensor will detect the
presence of anyone nearby
Figure 1 Connect a PIR sensor
nd their labels on the bottom of the sensor, lift off the plastic golf-ball-like diffuser and you
should see them on the top of the board. VCC needs to be connected to a 5V power pin,
GND needs to go to a ground pin, and then there’s the OUT wire which will be our input. We’re
connecting it to GPIO 14.
If your Raspberry Pi Zero has GPIO pins attached, you can use female-to-female jumper
wires to make the connections, as shown in Figure 1. Otherwise you can loop the wire around
the GPIO holes and use a bit of putty to keep them in place, or a dab of glue from a glue gun on
a low setting. Soldering is an option if you want to create a permanent spy camera device.
Write the code
03
Now we’ve got it all wired up, it’s time to start coding our spy camera. In the Thonny
code editor, create a new le and enter the code from ch9listing1.py. This script uses two
libraries: GPIO Zero and the standard picamera library. GPIO Zero can be used to get a reading
from the PIR motion sensor very easily, which can then be tied into the picamera code so it
takes a photo when motion is detected.
At the top, we import MotionSensor from GPIO Zero and PiCamera from picamera. Since
we’ll be giving each photo a timestamp, we also import datetime, along with sleep from the
time library.
51Chapter 9 Make a spy camera
Page 52
Camouaged against Yoshi, the camera will take candid snaps of
anyone who comes close to your gamecollection
In a never-ending while True: loop, we use GPIO Zero’s handy wait_for_motion function
to pause the code until the PIR detects any motion. When it does, we set the photo le name
to the current time and date, then take the picture. To enable the PIR to settle, we sleep for ve
seconds before returning to the top of the loop to wait for motion again.
Final preparations
04
You can run the code rst to give it a test. You might want to change the sensitivity
and/or trigger time, which you can do by adjusting the little orange potentiometer screws on
the side of the PIR board: Sx adjusts sensitivity, while Tx alters the trigger time.
Once that’s done, we’ll get the program to start automatically whenever we boot up the
Raspberry Pi. To do so, open up a Terminal window and edit the prole cong le with sudo
nano /etc/profile. To the bottom of the le, add this line:
sudo python spy.py
In addition, to get Raspberry Pi to boot up slightly faster and, more importantly, to use a little
less power so your battery lasts longer, it’s best to get it to boot directly to the command line
rather than booting to the desktop. The easiest way to change this is to open Preferences >
Raspberry Pi Conguration from the desktop; in the default System tab, change Boot to the ‘To
52THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 53
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
ch9listing1.py / Python 3
#!/usr/bin/env python
from gpiozero import MotionSensor
from picamera import PiCamera
from datetime import datetime
from time import sleep
CLI’ option. Alternatively, open a Terminal window and enter sudo raspi-config to open the
Conguration Tool; select Boot Options > Desktop / CLI and option B2 – Console Autologin
Text console.
Hide your camera
05
Now you need to nd a good place to hide your camera. The default cable for
thecamera is limited by length, while the PIR can have its wires lengthened, so keep
thatinmind when building your system. Alternatively, you could get a camera extender
(magpi.cc/camextender) to link your cable to a standard-width one. Longer standard-width
cables – of up to 2 m – are also available if you are not using a Raspberry Pi Zero.
Hiding your Raspberry Pi and battery behind a plush toy or photo frame can work well (you
could even put a dummy photo up and cut a hole in it for the camera to look through). The PIR
has quite a wide range, so put it up high where people are unlikely to look.
DOWNLOAD
magpi.cc/cameragit9
Check for intruders
06
All you need to do now is plug in the power supply and your Raspberry Pi Zero will turn
on and automatically run the script. Do some tests to make sure the camera is facing the right
way. Leave it running during the day and then when you get back, plug it into a monitor, stop
the script, and run startx to get the GUI up. From here you can see the pictures it has taken:
crucial evidence to catch your dog or sibling red-handed.
53Chapter 9 Make a spy camera
Page 54
Chapter 10
Smart door
Adding a Raspberry Pi to your door has magical results. Want
to see who’s at the door or know when the post has arrived?
Control the lock? Read on…
s your door a bore? Open and close, open and close. Snoozefest. Surely it can do more
than that? How about a smart door that knows when someone approaches, when the post
I
arrives, and can even offer remote viewing of the peephole? You can also add intelligent
lighting, a controllable door lock, and facial recognition, all powered with your Raspberry Pi. So,
let’s ignore super-expensive door systems and build our own. You can do as much, or as little,
as you like of this project and there’s plenty of room for new and inventive uses.
Prepare your Raspberry Pi
01
Although you can use any WiFi-capable model, this is a perfect project for the
Raspberry Pi3A+. Start by attaching Raspberry Pi to the Touch Display and preparing a
microSD card with the latest Raspbian release. To allow easier access and mounting, we’ve
detached the control board from the back of the screen, taking great care of the ribbon cable.
Eventually, they’ll be put in a smart 3D-printed case. Now, get your Raspberry Pi set up and
make sure to sudo apt update && sudo apt upgrade before proceeding.
Attach the camera
02
We’re going to keep an eye on the outside world by replacing the door’s peephole
with the Raspberry Pi camera. A peep-hole is typically a two-piece barrel that screws together
YOU’LL NEED
• Raspberry Pi TouchDisplay magpi.cc/touch
• Camera Module / HQ Camera
• PIR sensor magpi.cc/pir
• 2 × Security door contact reed switch
magpi.cc/doorswitch
54THE OFFICIAL RASPBERRY PICAMERA GUIDE
• Wired doorbell magpi.cc/wiredbell
• PAM8302 amplier magpi.cc/pam8302
• Speaker magpi.cc/3inspeaker
• Magnetic access control system
magpi.cc/magneticaccess
Page 55
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
and can be easily unscrewed from the inside.
Remove the barrel and cover the hole with
the camera. We’re just going to afx this with
tape for now; a printed mount will come later.
Mount the screen and Raspberry Pi to the door
(we used Command Strips), placed so you can
attach the camera’s ribbon cable to Raspberry
Pi once it’s shut down. Make sure the camera is enabled in Raspberry Pi Conguration.
Footsteps approaching!
03
The rst smart thing our door is going to do is detect someone approaching it. A
cheap PIR sensor is perfect for the job. These cool little geodesic domes are triggered by heat
and are the same gizmos that you nd in motion-sensor lights, switches, and security systems.
Connect to Raspberry Pi as shown in Figure 1, checking whether you have a 5 V or 3.3 V
sensor. Sensitivity and duration of a ‘detection’ can be controlled by the two potentiometers on
the PIR board. Mount this outside in a suitable location to ‘watch’ your door.
Monitor the door and letterbox
04
We have two magnetic reed switches, the type you nd on windows and doors for
security systems. They are made up of two parts: the wired part is a reed switch and the
unwired a magnet. When the magnet meets the switch, it closes. If we attach the magnet
to the door and the switch to the frame, when the door opens, so does the switch. There’s
no polarity to worry about, so connect
one wire to GPIO 26 and the other
to the adjacent GND. Repeat for the
letterbox using GPIO 19. You may need
a breadboard.
NIGHT IS DARK
If you want the camera to work well at night,
you may want to consider a Pi NoIR Camera
Module supported with some infrared lighting.
Figure 1 The GPIO
wiring that’s needed
for the various
inputs and outputs
Ding dong!
05
If we replace the doorbell with
our own button, we can take a photo
with the Camera Module or HQ Camera
when someone presses it and send
a notication. Way better. Mount a
standard wired doorbell, which after all
is just a momentary contact button, to
the outside door frame and wire it back
to Raspberry Pi using GPIO 13 and an
available GND pin. If you’re prototyping
on a breadboard, a tactile switch will
do ne.
55Chapter 10 Smart door
Page 56
The screen automatically
displays video when an
approaching person is detected
Cheap and simple reed switches
monitor the door and the letterbox
Sounds good
06
There’s little point in a doorbell that makes no sound. We can use the small, but
surprisingly powerful, PAM8302 amplier with a speaker to make some noise. Supply power
by soldering ‘Vin’ to an available 3V3 pin on Raspberry Pi, and ground to GND. To get an audio
signal, you can tap the audio connector’s signal and ground, then connect them to A+ and Arespectively. Finally, solder the speaker to the larger + and - terminals. When prototyping, you
can skip this and use any active or passive speaker via the audio connector on Raspberry Pi.
Code
07
Double-check all your connections and then power up your Raspberry Pi. To use the
code published here (overleaf), open a Terminal and enter:
Now type in the code as shown. Alternatively, to download all the code:
cd
git clone https://github.com/themagpimag/cameraguideCh10
To enable it to play our doorbell sample:
sudo apt install mpg123
Now test with:
python3 ~/smartdoor/smartdoor_test.py
Watch the console output. If everything is working, you should be able to trigger the PIR, the
reed switches, and the doorbell. The camera will capture ten seconds of video when motion is
detected, and a photo when the doorbell is pressed. These are both saved to the desktop.
Get alerts!
08
Let’s make this useful. Install Pushover on your phone, head over to pushover.net,
sign up for a trial account, then log in and make a note of your User Key (a long string of
characters). Now create a new Application
and give it a name. Once created, you’ll
see an API Token. Make a note of this
too. From the GitHub repository, edit
smartdoor.py and add the User Key and
API Token where shown. Run this version
and you’ll get phone alerts for each event
and even a photo attachment when the
doorbell is pressed.
Intelligent
09
porch light
Following on from the Trådfri lighting
tutorial in The MagPi #75 (magpi.cc/75),
if you have an external porch light, why
not make it smart! The le porch.py will
connect a Trådfri smart light to an API
that provides sunrise and sunset times
for your location. Leave the script running
and the light will switch on and off at the
correct times. Additionally, it monitors the
The web app can run on the touchscreen,
as well as on mobile devices or desktop
browsers. Release the door fromanywhere!
57Chapter 10 Smart door
Page 58
You may want to prototype this project and test it before taking a drill to your door!
58THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 59
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
PIR sensor and will switch to full brightness
when someone approaches! To use the script,
get your latitude and longitude (you can use
Google Maps or Earth) and edit porch.py as
directed in the le.
Door lock
10
If you’re interested in being able to control your door’s lock, you may see that some
solutions are very pricey. One that is perfect for experimentation is the magnetic hold lock,
which uses an electromagnet to hold the door closed. The one we’ve used can withstand
180 kg of force, although stronger ones are available. The magnet mounts on the door and
the electromagnet on the frame. The provided PSU contains a relay that can be powered by
Raspberry Pi by simply connecting it to a spare GPIO line and ground. Please note this is no
replacement for a proper door lock system.
Web app
11
If would be great to see what our door has been up to remotely, so a web app seems
the next logical step. In the directory called webapp is a Python script that uses Flask
to provide a web server that is usable on mobile devices. You can take a photo from the
peephole, see the last recorded video, and even control the magnetic door lock from Step 10.
Simply run the app alongside the others. Better still, set smartlights.py, porch.py, and
webapp/smartdoor.py to start on boot (see the repository README).
GET THE RIGHT LOCK
Magnetic door locks vary in size and shape;
measure twice and order once!
Facial recognition
12
Once a futuristic technology, decent facial recognition is now well within the grasp
of Raspberry Pi. Using the doorbell photo taken by Raspberry Pi, we can recognise a face
using reference photos and send an alert to Pushover with the name of the caller! In a secure
environment, a recognised face could even trigger the lock or you could play a welcome
announcement. The install process is a little complicated, so if this interests you, see the
documentation in the GitHub repository in the face_recognition directory of the
‘smartdoor’ repository.
Over to you
13
Here we’ve given you the basics to get going, but more complex events are possible.
You could alert different people based on facial recognition or play custom doorbell tones.
And, if you had problems with deliveries, video evidence can build up automatically. On a
serious note, remember a lot of this is ‘just for fun’ and designed to inspire, so unless you’re
prepared to put in the work hardening the code and including failsafes, don’t rely on this, or
possibly make it as a fun kids’ door project (but maybe without the lock!).
59Chapter 10 Smart door
Page 60
smartdoor_test.py / Python 3
DOWNLOAD
from picamera import PiCamera
from gpiozero import MotionSensor
from gpiozero import Button
from time import sleep
import os
import subprocess
import sys
print('Getting smart...')
# Set up all our devices
camera =PiCamera()
motion =MotionSensor(17)
doorSensor =Button(26)
letterbox =Button(19)
doorbell =Button(13)
defmotionDetected():
print('Motion detected, video recording')
os.system('DISPLAY=:0 xset s reset') # Wakes the display up
camera.start_preview()
camera.start_recording('/home/pi/Desktop/motion.h264')
sleep(10)
defmotionStopped():
print('Stopping video recording')
camera.stop_recording()
camera.stop_preview()
purchase and implement. Now, even a Raspberry Pi has the ability to read number-plates with
high accuracy using the Camera Module (or HQ Camera) and open-source software. Let’s see
what’s possible by building a system to detect and alert when a car comes onto the driveway.
01
applications, we’re going to see who’s home (or not) by reading number-plates of cars
coming and going on a driveway. This means our Raspberry Pi is probably going to live
outside; therefore, many environmental constraints come into place. You’ll need USB 5 V
power to your Raspberry Pi and a mounting position suitable for reading plates, although the
software is surprisingly tolerant of angles and heights, so don’t worry too much if you can’t
get it perfectly aligned.
02
you’ll need an appropriate enclosure. For a proper build, get an IP67 waterproof case (e.g.
magpi.cc/ip67kit). We’re opting for homemade, and are using a Raspberry Pi 3 A+ with the
RainBerry – a 3D-printable case that, once you add some rubber seals, provides adequate
protection. Make sure whatever you choose has a hole for the camera.
more commonplace. Once the
Pick a spot
First things rst: where are we going to put it? Although this project has lots of
Get an enclosure
As your Raspberry Pi is going to live outside (unless you have a well-placed window),
YOU’LL NEED
• Camera Module / HQ Camera
• Suitable outdoor enclosure,
e.g. magpi.cc/rainberry
• Pushover account (optional) pushover.net
62THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 63
The Camera Module is
protected from the elements by
a clear cover and rubber O-ring
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
This case is water-resistant with
rubber seals protecting the gaps
Prepare your Raspberry Pi
03
As we don’t need a graphical user interface, Raspbian Lite is our operating system
of choice. Any Raspberry Pi can handle this task, although if you want the fastest image
processing possible, you probably want to avoid the Zero models and get a nice, zippy Model
3B+ or 4. Get your operating system set up, make sure you’ve done the necessary sudo apt
update && sudo apt -y upgrade and have congured WiFi if you’re not using Ethernet.
Finally, make sure your Camera Module (or HQ Camera) is connected and enabled. You can
check this by running sudo raspi-config and looking under ‘Interfacing Options’.
Install openALPR
04
Thankfully, we don’t need to be experts in machine learning and image processing to
implement ANPR. An open-source project, openALPR provides fast and accurate processing
just from a camera image. ‘ALPR’ is not a mistake: this US project is ‘Automatic License
Plate Recognition’. Thanks to APT, installation is straightforward. At the command line, enter
the following:
This may take a while, as many supporting packages need to be installed, such as Tesseract,
an open-source optical character recognition (OCR) tool. This, coupled with code that
identies a number-plate, is what works the magic.
63Chapter 11 Car Spy Pi
Page 64
Protect your Raspberry Pi with a waterproof case
Time for a test
05
Once installed, you’ll be unceremoniously dropped back to the command prompt.
OpenALPR has installed a command-line tool to make testing its capabilities easier and they
have also kindly provided a sample image. On the command line, enter the following:
cd
wget http://plates.openalpr.com/ea7the.jpg
This is a sample USA plate image and a tough one too. Wget grabs the le from the web and
places it in your home directory. Let’s see if we can recognise it:
alpr -c us ea7the.jpg
All being well, you’ll see a report on screen. The most likely ‘match’ should be the same as the
le name: EA7THE.
Install Python libraries
06
We can use openALPR in Python, too. First, install the libraries using pip. If you don’t
have pip installed, run:
64THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 65
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
sudo apt install python-pip
To install the libraries:
pip install openalpr picamera python-pushover
Now test everything is working by running Python (type python) and enter the following code
If you’ve not seen JSON-formatted text before, this might seem a bit much, but you should see
the correct plate number returned as the rst result.
Get a Pushover token
07
So that we can get an alert when a car arrives or leaves, we’re using old favourite
Pushover (pushover.net), which makes sending notications to mobile phones a breeze.
There’s a free trial, after which it’s a at fee of $4.99 per device, with no subscription or
limits. Once logged in, go to ‘Your Applications’ and make a note of your User Key. Then click
‘Create an Application/API Token’. Call it
‘ANPR’, leave all the other elds blank, and
click ‘Create Application’. Now make a note
of the API Token; you’ll need this and the
User Key for your code.
Typing time
08
Now you have everything you need
to create your ANPR application. Enter the
code listing shown here or download it from
magpi.cc/cameragit11. Save it as anpr.py in
your home directory. Edit the le and enter
your User and App tokens where prompted.
Save the le, then test by entering:
When a car arrives or leaves our driveway,
we receive an alert in seconds
65Chapter 11 Car Spy Pi
Page 66
python anpr.py
The code makes use of the Camera Module (or HQ Camera) and openALPR in tandem.
Every ve seconds, the camera takes a picture which is passed to openALPR for analysis.
If a licence plate is found, we get the number. If there has been a change, an alert is sent to
Pushover, which is then forwarded to any registered mobile devices.
Make your list
09
If you want to have more friendly names rather than just the plate number, try adding a
Python dictionary just after the import statements, like this:
On reboot, the code will start up and run in the background.
66THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 67
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
Our target. The software does a great job of recognising number-plates
from different heights and angles
Add logging and curfews
11
One use of this installation is to track the times cars come and go. This can be
especially useful for young drivers who have curfew restrictions on their insurance. See if you
can augment the code to check whether a registration plate has not been seen after a certain
time. For example, if your younger family members have such a restriction, send them an alert
to their phone if their car isn’t in the driveway 30 minutes beforehand. You might save them an
insurance premium increase! Also, why not log all the comings and goings to a le? A bit of
data analysis might help reduce car usage or fuel costs.
Make it your own
12
As ever, it’s over to you. Now you have the ability to track and record registration
plates, there are many different applications for you to explore. Since all the analysis of the
image is done locally, no internet connection is required for the system to work. Is there
someone ‘borrowing’ your parking space at work? Catch ’em in the act! Why not take your
CarSpy Pi on the road? It could record every vehicle you encounter, which may be useful
should something untoward happen. Combine a Raspberry Pi Zero with a ZeroView
(magpi.cc/zeroview) and you’re all set.
67Chapter 11 Car Spy Pi
Page 68
anpr.py / Python 3
DOWNLOAD
from openalpr import Alpr
from picamera import PiCamera
from time import sleep
import pushover
# Pushover settings
PUSHOVER_USER_KEY ="<REPLACE WITH USER KEY>"
PUSHOVER_APP_TOKEN ="<REPLACE WITH APP TOKEN>"
# 'gb' means we want to recognise UK plates, many
others are available
alpr =Alpr("gb", "/etc/openalpr/openalpr.conf",
"/usr/share/openalpr/runtime_data")
camera =PiCamera()
pushover.init(PUSHOVER_APP_TOKEN)
last_seen =None
try:
# Let's loop forever:whileTrue:
# Take a photoprint('Taking a photo')
camera.capture('/home/pi/latest.jpg')
magpi.cc/cameragit11
# Ask OpenALPR what it thinks
analysis = alpr.recognize_file("/home/pi/latest.jpg")
# If no results, no car!iflen(analysis['results']) ==0:
print('No number plate detected')
# Has a car left?if last_seen isnotNone:
pushover.Client(PUSHOVER_USER_KEY).send_message(
last_seen +" left",
title="Driveway")
Once installed, test it by typing in cd ~/pi-timolo and then ./pi-timolo.py to run the
Python script. At this point, you should be alerted to any errors such as the camera not being
installed correctly, otherwise the script will run and you should see debug info in the Terminal
window. Check the pictures by waving your hand in front of the camera, then looking in
Pi-timolo > Media Recent > Motion. You may need to change the image size and orientation
of the camera; in the Terminal window, enter nano config.py andedit these variables:
imageWidth, imageHeight, and imageRotation.
While we’re here, if you get a lot of false positives, try changing the motionTrackMinArea
and motionTrackTrigLen variables and experiment with the values by increasing to reduce
sensitivity. See the Pi-timolo GitHub repo (magpi.cc/pitimologit) for more details.
71Chapter 12 Build a wildlife camera trap
Page 72
Unleash your inner
There will also be some editing of the pi-timolo.py le, so don’t close the Terminal
window. Code needs to be added to import some Python libraries (ch12listing1.py), and
tweeting (ch12listing2.py). We can do this now in preparation of setting up our Google and
Twitter API. You should still be in the Pi-timolo folder, so type nano pi-timolo.py and add
UserMotionCodeHere() function and where it’s called from. Add the new code into the function
(line 240), before the return line. Also locate where the function is being called from (line 1798),
up the APIs.
Springwatch
Animal detection and tweeting
We will be using Google Label Detection, which returns a list it associates with the image. First
off, you will need to install the Google Cloud Vision libraries on your Raspberry Pi, so type pip
install --upgrade google-cloud-vision into your Terminal window. Once nished, run
pip install google-cloud-storage.
a new project (you may need to log in or create a Google account). Go to the API Dashboard
and search for and enable the Cloud Vision API. Then go to API & Services > Credentials,
click on Create Credentials and select Service Account from the drop-down. Fill in the details,
then click Create. Don’t bother to select a Role, just click Continue. Click Create Key and you’ll
72THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 73
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
pi-timolo folder and make a note of the le path. Next, go back to pi-timolo.py and add the
line: os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path_to_your_.json_credential_file" below import os to reference the credentials in your JSON le.
Finally, set up a Twitter account if you haven’t already and install Tweepy by entering sudo
pip install tweepy into a Terminal window. Once set up, visit apps.twitter.com and create
a new app, then click on Keys and Access Tokens. Edit the code in userMotionCodeHere()
with your own consumer and access info, labelled as ‘XXX’ in the code listing. Finally, place
your camera in front of your bird feeder and run ./pi-timolo.py. Any pictures taken of a bird
should now be tweeted! If you want to identify a different animal, change the line if "bird"
in tweetText: animalInPic = true.
Please note that although the API works well, it can’t always discern exactly what’s in
the picture, especially if partially in view. It also won’t distinguish between types of bird, but
you should have more success with mammals. You can test the API out with some of your
pictures at magpi.cc/visionai and visit twitter.com/pibirdbrain to see example tweets (scroll
down a bit). Good luck and happy tweeting!
Get great photos with night-vision cameras
73Chapter 12 Build a wildlife camera trap
Page 74
ch12listing1.py / Python
DOWNLOAD
# add this in at the very top, under
print('Loading ....') along with the other
libraries imported
import io
import tweepy
from google.cloud import vision
from google.cloud.vision import types
from google.cloud import storage
magpi.cc/cameragit12
ch12listing2.py / Python
# search for userMotionCodeHere.
# There will be 2 results,
# edit the second so you are passing filename to the function
userMotionCodeHere(filename)
# make sure you include filename as a parameter in the function
defuserMotionCodeHere(filename):
# we need to create an instance of the Google Vision API
client = storage.Client()# instantiates a client
client = vision.ImageAnnotatorClient()
# loads the image into memorywith io.open(filename, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
# performs label detection on the image file
response = client.label_detection(image=image)
# pass the response into a variable
labels = response.label_annotations
# we have our labels, now create a string to add to the text # for debugging - let’s see what Google thinks is in the imageprint('Labels:')
# add labels to our tweet text
tweetText ="Labels: "
74THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 75
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
ch12listing2.py (cont.)/ Python
animalInPic =Falsefor label in labels:print(label.description)
tweetText = tweetText +" "+ label.description# edit this line to change the animal you want to detectif"bird"in tweetText: animalInPic = true
# set up Tweepy
# consumer keys and access tokens, used for authorisation
# authorisation process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# creation of the actual interface, using authentication
api = tweepy.API(auth)
# send the tweet with photo and message
photo_path = filename
# only send tweet if it contains a desired animalif animalInPic:
api.update_with_media(photo_path, status=tweetText)
return
75Chapter 12 Build a wildlife camera trap
Page 76
Chapter 13
Take your camera
underwater
Explore the underwater world with your camera
here are plenty of underwater sports cameras available, but they can be quite
expensive, especially if you want to be able to control them remotely. In this
T
customisable camera unit. There are lots of options and alternative sources of components
for a project like this. For example, the Pimoroni Enviro board (or earlier Enviro pHAT) can
report back information about the environment in which the camera is operating, especially
how much light is available.
chapter we’re going to use readily available Raspberry Pi add-ons to make a cheaper,
Find a suitable container
01
To protect the electronics inside it, the container for your Raspberry Pi and camera
needs to be watertight and to have at least a see-through lid. You can nd food container
boxes with a very tight seal, but these tend to be translucent rather than transparent. The
size of box will probably determine your choice of Raspberry Pi model andpower source.
Raspberry Pi Zero W boards are great as they are so small and have built-in wireless LAN.
You can also save space by using a LiPo battery instead of a standardpower bank (although
you’ll need a boost regulator too, such as the Pimoroni ZeroLiPo).
YOU’LL NEED
• Camera Module / HQ Camera
• Transparent, waterproof box
magpi.cc/waterproofcase
• Portable power source
• hostapd and dnsmasq packages
76THE OFFICIAL RASPBERRY PICAMERA GUIDE
• Python Flask library
• WiFi dongle (if not using a Raspberry Pi model
with built-in wireless LAN)
• Enviro board (optional) magpi.cc/enviro
• ZeroView (optional) magpi.cc/zeroview
Page 77
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
You can save space by using a LiPo battery (via a boost regulator) instead of a power bank
Set Raspberry Pi as a wireless access point
02
Start from a fresh Raspbian install on a microSD card. Open up a Terminal window and
enter the following commands to update the APT database and install the required packages:
You’ll still have to get pretty close to the water yourself
Now restart the dhcpcd daemon and set up the new wlan0 conguration:
sudo service dhcpcd restart
Next, you need to edit the /etc/dnsmasq.conf and /etc/hostapd/hostapd.conf les – see
magpi.cc/accesspoint for details – ensuring that the IP addresses are consistent with your
settings in /etc/dhcpcd.conf. Then reboot your Raspberry Pi.
Add the Enviro board
03
Pimoroni’s Enviro board (or the earlier Enviro pHAT, which is still available from some
online retailers) enables you to send back environmental data from the camera. With your
Raspberry Pi powered off, mount the board on its GPIO header.
Now power up Raspberry Pi and install the Python library and dependencies using the
Then use the desktop File Manager to move the Flask folder within cameraCh13 to your
Raspberry Pi’s home directory (or use the mv command in a Terminal window).
A makeshift handle to lower the waterproof box into the water
79Chapter 13 Take your camera underwater
Page 80
To run the program, enter the following Terminal command:
sudo python3 /home/pi/Flask/ch13listing1.py
To see the generated webpage from another computer, you just have to open a web browser
and enter your Raspberry Pi’s static IP address. Using the on-screen buttons, we can also
switch between recording modes (video or continuous still frames) or take photos on demand
– by selecting QuickSnap and then clicking the Take button. This control of the camera is
achieved via the picamera library, which is used for the three main functions – timelapse,
video, and snapstart – dened in our Python script. You could enhance the project by adding
additional exposure and shutter speed controls to your interface if you want.
Note: To see the latest image taken, press an on-screen button or reload the webpage.
Set the code to run at boot
06
Naturally, we’ll want the code to run automatically whenever the Raspberry Pi boots
up. To do so, add this line to your /etc/rc.local le, immediately above the exit 0 line:
environmental information and
lets you control the camera
80THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 81
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
ch13listing1.py / Python 3
from flask import Flask, render_template,
request, redirect, url_for
import time, os, shutil
from picamera import PiCamera
from datetime import datetime, timedelta
from threading import Thread
from ltr559 import LTR559
from bme280 import BME280
from smbus2 import SMBus
ltr559 = LTR559()
bus = SMBus(1)
bme280 = BME280(i2c_dev=bus)
temp =round(bme280.get_temperature(),2) # temperature
press =int(bme280.get_pressure()) # pressure
lux = ltr559.get_lux() # light levels
df = os.statvfs('/') # check if we're running out of disk
# do 0.0.0.0 so that we can log into this webpage
# using another computer on the same network later
# specify port 80 rather than default 5000
app.run(host='0.0.0.0',port=80,debug=True)
84THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 85
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
85Chapter 13 Take your camera underwater
Page 86
Chapter 14
Install a bird
box camera
Observe nesting birds without disturbing them
hile it’s simple enough to set up a
Camera Module in a weatherproof
W
garden, for this project we’ll be installing a
camera inside a bird box. Since it’ll be dark
inside, and we can’t use a standard light
source, we’ll need to use a Pi NoIR Camera
Module. ‘NoIR’ stands for ‘no infrared’, as
it omits the IR lter found in the standard
camera. This enables you to use an infrared
light source to see in the dark. Note that we’ll
need to adjust the xed focus of the camera by
unscrewing the lens.
01
insects and predators, and so would deter any birds from nesting there. So we need to use a
Pi NoIR Camera Module (Figure 1). Apart form the omission of an infrared lter, this works
exactly the same way as the standard camera, so you can connect it up to your Raspberry Pi
as in Chapter 1 and use all the same Terminal commands. So, for instance, you can obtain a
video preview with:
raspivid -t 0
box to observe wildlife in your
Set up the Pi NoIR
We can’t use a standard light source inside the bird box, since this could attract
YOU’LL NEED
• Pi NoIR Camera Module
• Focus adjustment tool
• Bird box
• IR LED
• Female-to-female jumper wires
• Power source
• Ethernet cable (optional)
You’ll notice that everything looks a little strange; this is because you’re looking at a
combination of visible light and infrared light. To test it out in darkness, turn the lights off, aim
86THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 87
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
a TV remote control at your face and press
the buttons to produce an IR light source.
You should see your face illuminated in the
darkness. The image will look black and white
(greyscale), because there are no wavelengths
of light from the visible spectrum being
detected. However, a black and white image
is good enough to allow you to watch what’s
happening inside a bird box, and it doesn’t
disturb or interfere with the birds in any way. Press CTRL+C to exit the preview.
Wire up an IR LED
02
We’ll need a suitable infrared light source in the bird box. In this example we’re using
a single IR LED, but alternatives include small IR lamps and the IR version of the LISIPAROI
(lisiparoi.com). Our 890 nm IR LED is an identical component to the ones found inside TV
remote controls; the only difference is that we’re going to keep it on constantly when shooting
video or stills in the bird box.
As usual, you should turn off your Raspberry Pi before connecting anything up. If you’ve
wired up an LED to Raspberry Pi’s GPIO pins before, then please note that this LED needs to
be done slightly differently. Since an infrared LED requires more current than the GPIO pins
can provide, it needs to be connected directly to the 5V supply of Raspberry Pi with a 220 Ω
resistor inline; without the resistor the current will be too high, and the LED will burn out after
about ten seconds.
HQ CAMERA
If you want to use an HQ Camera for this
project, you’ll need to remove its IR lter so
it can capture infrared images in the dark.
See the leaet supplied with the camera
for more details, but note that this is an
irreversible process.
Figure 1 The
PiNoIR Camera
Module can see
in the dark with
infrared lighting
87Chapter 14 Install a bird box camera
Page 88
The longest leg of the IR LED is
the anode – connect it to a 5 V pin
The 222 Ω resistor limits the
Figure 2
Figure 2 shows how the LED should be wired up. You’ll notice that the LED has two legs, one
slightly longer than the other. The longer of the two is called the anode and the shorter is the
cathode. The LED needs power to ow into the anode and out of the cathode; if you get the
polarity wrong then nothing will happen.
Use a couple of female-to-female jumper wires to make the following connections. Connect
the anode (long leg) to 5 V, which is the rst pin on the outside row on the GPIO. Connect the
cathode (short leg) to the 220 Ω resistor. Connect the other side of the resistor to ground
(GND), which is the third pin in on the outside row of the GPIO.
current owing through the LED
Test the LED
03
With everything wired up correctly, turn your Raspberry Pi back on. You’ll notice that
the infrared LED doesn’t appear to be working, but in fact it is. Your human eyes can’t see it,
but the Pi NoIR camera can. Turn on the camera preview again with raspivid -t 0. Hold the
LED in front of the camera and you should see it lit. If not, then you may have mixed up the
polarity of the anode and cathode. Double-check your wiring against the Figure 2 diagram.
Try turning out the lights and aiming the LED at yourself; don’t look directly into it, however, as
infrared light can still cause harm to your eyes. You’ll see from the Pi NoIR camera preview
that it will illuminate you quite well. Press CTRL+C when you want to exit.
Adjust focus
04
By default, the Pi NoIR Camera Module has a xed focal length of 50 cm and depth
of eld of 50 cm to innity. This means that objects will only appear in focus if they’re at least
50 cm away from the lens of the camera. The Gardman box we’re using in this example has an
interior height of 18 cm, so we’ll denitely need to shorten the focal length.
88THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 89
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
Like the standard Camera Module,
the Pi NoIR has a lens that can rotate to
adjust the focus. If your camera came
supplied with a focus adjustment tool (a
little plastic wheel), you can use this to turn
the lens, removing three blobs of silicone
in the process. If you don’t have the tool,
you could always 3D-print one; there are
numerous design available on Thingiverse,
e.g. thingiverse.com/thing:2533691.
Alternatively, you could always use a scalpel
Figure 3 Pinch the lid and then use a pen
to mark a cross where your thumb is
Carefully rotate it anticlockwise a few times – be careful not to rotate the lens too far,
otherwise it will pop out, and it can be a bit tricky to get it back in and on the thread. If this
does happen, just put it back in gently and rotate clockwise until it catches.
Now reconnect the camera to Raspberry Pi. Place an object – such as a watch or business
card – in the bottom of the bird box, then remove its roof (remove the screw), hold the camera
at the approximate height of the roof, and look at the camera preview. You may wish to put
something under the object at this point to simulate the height of a nest, to make doubly sure
that the birds will be in focus. Remember that once birds move in, you can’t come back and
adjust the camera if the focus is wrong.
to carefully remove the silicone blobs and
then use tweezers to rotate the lens – see
magpi.cc/adjustfocus for more details.
Install the camera
05
Place your nger on the roof, approximately above the centre of the main body of
the bird box. Lift up the roof and place your thumb directly below your nger, so that you’re
pinching the lid as shown in Figure 3. Your thumb is now where the camera needs to be.
Take a pen and mark this spot with a cross. Cut out a rectangle of cardboard approximately
4 cm× 2 cm (1.5" × 0.75") and fold it in half
lengthways. Use some tape to secure it to
the underside of the roof so that it’s a few
millimetres below the cross. This is going to
be used to compensate for the angle of the
roof, so that the camera points directly into
the middle of the bird box.
Next, take the Pi NoIR and slide the
exible cable down between the roof hinge
and the back wall of the box – Figure 4. Do
this with the tin connectors facing away
from the back wall. If you wish, you can
Figure 4 Slide the camera cable between
the roof hinge and the back wall of the box
89Chapter 14 Install a bird box camera
Page 90
remove the two middle staples holding the
hinge in place; this will make the ex exit
the bird box more neatly.
Take some tape and put it across the
top of the Pi NoIR board – do not cover the
camera lens! Secure the camera in place
so that the central lens is directly over the
cross that you drew earlier. The camera
should sit at an angle – Figure 5. Close the
lid and inspect the camera angle from the
Figure 5 When in place, the camera should sit
at an angle to compensate for the roof slope
alternative to taping it in place would be to use the four mounting holes to screw it to the lid
via a wedge of wood instead of the cardboard.
Add the LED
06
Secure the infrared LED to the underside of the roof. Don’t attach it too close to the
camera, or you’ll see a lot of glare on the video. The LED can go anywhere, but it can help to
bend its legs by 90 degrees, as shown in Figure 6, and secure it to the roof that way. You may
also wish to blank off the end of the LED with correction uid or by ling it down with a nail le.
This will prevent any spotlight effect on the video and give a more diffuse light.
side: it needs to point directly at the centre
of the base. If it doesn’t look right, then go
back and adjust it until you’re happy. An
Test it again
07
Now reconnect your Raspberry Pi and test the focus once again. We recommend
connecting the camera ex coming from the back of the bird box to Raspberry Pi rst. Then
connect the LED and resistor, followed by the screen, keyboard, and nally the power supply.
When testing this setup, it can be helpful to rest Raspberry Pi upside-down on the roof of the bird
box, but do whatever works best for you.
Boot up as usual and then start the video
preview with raspivid -t 0. With the
roof of the bird box closed, you should be
able to see the inside in black and white.
This shows that the infrared illumination is
working; you should even be able to cover
the hole and still see the inside. It will look
similar to Figure 7, but will be slightly more
zoomed in. This is because this image was
taken using the raspistill command and
not raspivid. If you can’t see anything
at all, then it’s likely the LED is not wired
90THE OFFICIAL RASPBERRY PICAMERA GUIDE
Figure 6 The IR LED is taped to the underside
of the roof, not too close to the camera
Page 91
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
up correctly: double-check the wiring and the
polarity of the anode and cathode.
It’s now helpful to put an object with some
black-on-white text into the bird box to verify
the focus, such as a watch or business card.
Ensure that the text is in focus and readable;
adjust the camera focus again as necessary
before continuing. Remember to compensate
for the nest height. Press CTRL+C when you
want to stop the camera preview.
Weatherproof it
08
While you can attach your Raspberry Pi directly to the outside of the bird box, an
alternative is to use a longer camera cable. Either way, you’ll need to put Raspberry Pi inside a
weatherproof box. Preventing water getting into the bird box should also be a priority. The roof
could be sealed using silicone sealant, which is often used to seal the edges of windows and
bathroom sinks. Choosing a site which is beneath the overhang of an existing roof will help a
lot, so the bird box will not be rained on directly.
Lastly, you need to consider how you will get power and an internet connection to the bird
box? You could use a wireless USB dongle, or the built-in wireless LAN of a Raspberry Pi 3 /
3B+ / 4 / Zero W, but Ethernet is more reliable for streaming video, especially in built-up areas
that have a lot of wireless trafc.
TURN OFF RED LED
If you are using an original (Rev 1.3) Pi NoIR
Camera Module, you’ll need to disable its
red LED that lights up whenever the camera
is on. Enter sudo nano /boot/cong.txt
and add the following line to the end of the
le: disable_camera_led=1. Save and exit
the le, then reboot.
Obtain images
09
With everything installed, connected, and powered up, you can SSH in to your
Raspberry Pi from another computer (see magpi.cc/ssh for details) to control it remotely.
You are then able to enter standard Terminal commands such as raspistill and raspivid
to obtain stills (including time-lapses – see
Chapter 3) and video footage. You could
also write one or more Python scripts
using the picamera library.
Note that you can’t view the live camera
preview via SSH. However, you are able to
live-stream video from the bird box. This
could be achieved using a client-server
setup, as described in Chapter 15, to pipe
the output to a video player on the client
computer. Alternatively, you could make
use of an internet video service offering
Figure 7 Make sure that the test object is
raised up slightly and the text is in focus
livestreaming, such as YouTube (see
magpi.cc/birdboxyt for details).
91Chapter 14 Install a bird box camera
Page 92
Chapter 15
Live-stream
video and stills
Stream video and regular stills to a remote computer
ne of the drawbacks of using SSH
or VNC to access your Camera
O
Raspberry Pi remotely from another
computer is that you can’t (typically) view
the camera preview via these methods. To
get around this, you’ll need to stream live video across the network. While there are various
methods available for doing this, in this chapter we’ll show you how to create a client-server
setup for video streaming using the picamera Python library. We’ll also explore how to send
a stream of stills over the network.
01
there is an easier method, explained in Step 03.
read the video stream (which we’ve yet to write to the code to create) and pipe it to a media
player. Note that while you can use a Raspberry Pi 4 for the task of remote playback, earlier
Raspberry Pi models won't work in this role since the CPU is not powerful enough to do the
video decoding (and neither VLC nor MPlayer supports doing this using the GPU). Therefore
you may need to run this script on a faster machine, although even an Atom-powered
netbook should be quick enough for the task at non-HD resolutions.
connections on 0.0.0.0:8000, i.e. all IP addresses on the local network. We then accept a
single connection and make a le-like object out of it.
use MPlayer instead of VLC, add a # to the start of the cmdline = ['vlc… line to comment
it out, and remove the # from the cmdline = ['mplayer… line.
Module- or HQ Camera-equipped
Server-side script
Note: If you are using a Linux-based computer for playback of the video stream,
First, we’ll write a Python server script, ch15listing1.py, for the remote computer that will
After importing the libraries required at the top of the script, we start listening for
In the try: block, we run a media player from the command line to view it – if you want to
YOU’LL NEED
• Camera Module / HQ Camera
• Remote computer
92THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 93
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
The remote server script reads the video stream and pipes it to a media player, such as VLC
In the while True: loop, we repeatedly read 1kB of data from the connection and write it to
the selected media player’s stdin (standard input) to display it.
Note: If you run this script on Windows or macOS, you will probably need to provide a
complete path to the VLC or MPlayer executable/app. If you run the script on macOS, and
are using Python installed from MacPorts, please ensure you have also installed VLC or
MPlayer from MacPorts.
Client-side script
02
Now we’ll create a client script, ch15listing2.py, on our Raspberry Pi with the
Camera Module or HQ Camera equipped. This will connect to the network socket of our
server (playback) script to send a video stream to it.
After importing the required libraries at the top, we connect a client socket to
my_server:8000 – you’ll need to change my_server to the host name of your server
(thecomputer that will playing back the stream). If you are using a Linux PC or Mac, just
type hostname -I in a Terminal window to nd it out; in Windows, it’s the Computer Name
inControl Panel > System.
We then create a le-like object from the network socket before triggering the camera
to start recording. In this example we’re using a resolution of 640×480 with a frame rate of
24 fps, but you can adjust these numbers to your requirements. We’ve also set the camera
to record for 60 seconds with camera.wait_recording(60); again, you can change this
number to suit your preference.
93Chapter 15 Live-stream video and stills
Page 94
An alternative is to set Raspberry Pi as the server and open the
network source in VLC or another media player
Run the server script, then the client script. You should see the video stream played in
your chosen media player. You may notice some latency; this is normal and due to buffering
by the media player.
Server-side script
03
As mentioned, if you’re using a Linux PC for playback of the video stream, there is a
much quicker and easier way to achieve what we’ve done in Steps 01 and 02. On the server
(playback) machine, enter the following command into a Terminal window:
nc -l 8000 | vlc --demux h264 -
Then, on the client – your Raspberry Pi with the Camera Module or HQ Camera – issue the
An alternative method is to reverse the direction so that Raspberry Pi acts as a
server. We can then get it to wait for a connection from the client before streaming video.
Enter the ch15listing3.py example on Raspberry Pi and run it.
94THE OFFICIAL RASPBERRY PICAMERA GUIDE
Page 95
THE OFFICIAL RASPBERRY PI CAMERA GUIDE
The big advantage of this method is that you then only need to use a single command to
initiate playback on the remote computer:
vlc tcp/h264://my_pi_address:8000/
…replacing my_pi_address with your Raspberry Pi’s IP address (again, discovered using
hostname -I). Or, in VLC running on the desktop, go to File > Open Network and enter the
same address: tcp/h264://my_pi_address:8000/
Stream stills
05
Now let’s stream camera stills taken at regular intervals in a variation on a standard
time-lapse setup. Entered on a remote computer (which could be another Raspberry Pi), the
server script, ch15listing4.py, starts a socket to listen for a connection from your Raspberry
Pi with the camera. At the top, we import the required libraries; here we’re using PIL (you
can install it using sudo pip install pillow) to read JPEG les, but alternatives include
OpenCV and GraphicsMagick. The script then checks the image length and, if it is not zero,
constructs a stream to hold the image data and then reads it from the connection. The
image.show() command will open each image in the default image viewer: it can create a
lot of windows if left going for a while! Now to create a client script…
Stills client script
06
On your Raspberry Pi with the camera, the client script, ch15listing5.py, sends a
continual stream of images to the server. We’ll use a very simple protocol for communication:
rst, the length of the image will be sent as a 32-bit integer (in little-endian format), then this
will be followed by the bytes of image data. If the length is 0, this indicates that the connection
should be closed as no more images will be forthcoming. As before, for connecting the socket,
you should replace my_server in the script with the host name of the remote computer. We then
make a le-like object out of the connection. Before constructing the stream, we start a preview
to let the camera warm up for two seconds. Further down, the line if time.time() - start
> 30: limits the streaming period to 30
seconds, though you can alter this.
Note that the server script should be run
rst to ensure there’s a listening socket
ready to accept a connection from the
client script.
Taking it further, you may want to add a
way of closing each image window before
the next is generated. Also, rather than
simply showing the images, you could use
the numerous functions of PIL to process
them (see effbot.org/imagingbook).
Multiple stills are streamed to the remote
computer and displayed
95Chapter 15 Live-stream video and stills
Page 96
ch15listing1.py / Python 3
import socket
import subprocess
# Start a socket listening for connections on 0.0.0.0:8000
# (0.0.0.0 means all interfaces)
camera = picamera.PiCamera()
camera.resolution = (640, 480)
camera.framerate =24# Start a preview and let the camera warm up
camera.start_preview()
time.sleep(2)
# Start recording, sending the output to the connection for 60
camera = picamera.PiCamera()
camera.resolution = (640, 480)
# Start a preview and let the camera warm up for
camera.start_preview()
time.sleep(2)
# Note the start time and construct a stream to
# hold image data temporarily (we could write it
# directly to connection but in this case we want
# to find out the size of each capture first to keep
# our protocol simple)
start = time.time()
stream = io.BytesIO()
99Chapter 15 Live-stream video and stills
Page 100
ch15listing5.py (cont.)/ Python 3
for foo in camera.capture_continuous(stream, 'jpeg'):