With the release of the Raspberry Pi camera module, taking high quality pictures, or video, with the Pi has become very simple.
In this tutorial, I will show you how to attach the Pi camera module to your Pi and wirelessly control a pan tilt platform. In the process, I will cover topics that include attaching the Pi camera module, connecting and controlling servos with your Pi, command line control of the camera, and some low level video streaming commands.
Gather the Components
- Raspberry Pi Model B
- Power supply
- SD card
- Raspberry Pi camera module
- Pan/tilt bracket
- Two servos
- Male-to-female hook up wires
- Pi camera mount
- USB Wifi module
- 6-32 screw to connect the mount to bracket (or whatever works for you. See instructions below.)
- Lego bricks
- USB keyboard/mouse (for setup. Only one USB is available for both of these. So a USB hub or a keyboard/mouse combo is suggested.)
- HDMI monitor (for setup)
Set Up the Pi Platform
Set up and assemble the basic Pi components with the following steps.
- Burn the latest Raspbian image to the SD card following instructions in the tutorial How to Flash an SD Card for Raspberry Pi
- Plug in the USB mouse/keyboard into the bottom USB port
- Plug in the USB Wifi adapter into the to USB port. The antenna can be positioned to bend up.
- Plug in the HDMI monitor to the HDMI port
- Plug in the power to the Pi
Configure the Pi and Enable Camera Support
Upon boot, the Pi will run through the first boot setup of raspi-config. There are a lot of options regarding SD card partition, region, keyboard, etc that are not covered here. Select the options appropriate for your setup and pay special note to the following options:
- Choose to boot to a desktop for now as it makes wifi setup simpler later on
- Select Enable Camera to enable this option
- Select Advanced Options > SSH to enable network command line access
- Select finish and allow the Pi to reboot
Setup Wifi and Reconfigure Boot Preference
After the Pi reboots to a desktop follow these instructions on how to use the GUI tool to configure your wifi network. Make sure to write down the IP address your Pi is assigned for later use when you SSH to it for remote control. Finally follow the steps below to reconfigure the Pi to boot to a text console. The graphical desktop environment will not be needed.
- Open a terminal window by double clicking on the LXTerminal icon.
- Execute the command
- Select the Enable Boot to Desktop/Scratch > Console Text console, requiring login (default) option
- Select Finish and don't allow the Pi to reboot
- Power down the Pi and unplug the power supply before continuing to the next steps
Assemble the Camera Mount
The camera mount comes with tiny plastic fasteners that fit through the four holes with nuts that screw onto them from the back. Refer to the product pictures on the supplier website to see close up pictures of how these components fit together.
- Attach the camera module to the camera mount front plate using the fasteners through the four holes. I discovered that using a helping hand rig was the easiest way to hold the mount and leave enough fingers free to get these tiny components to line up. Don’t tighten the nuts all the way while you are connecting them. Once all four are in place you can tighten as needed.
- Snap the two sections of the camera mount together at 90 degrees so they look like the product photos. The sections snap together gently.
Assemble the Pan Tilt Bracket
The bracket comes with three metal parts and a set of screws and washers. Follow the assembly guide to put the bracket together. I’ll offer a few suggestions to consider as you follow the instructions.
- As you secure the brackets together do not tighten the bolts and screws completely until all the parts have been loosely assembled. Keeping it loose will make it easier to adjust.
- Don’t over tighten the screws that connect the servo horn (wheel) to the bracket. These horns are made of soft plastic and they can strip out easily. Tighten these screws gently.
- I found that three washers between the two brackets seen on the left side of step four in the instructions was the best fit for my assembly. These additional washers filled the space and let the brackets align better than a single washer. Extra washers are provided in the kit.
Build the Base
The bracket will not stand up on its own because it is only supported by the small servo. Any method for securing the bottom axis or pan servo is acceptable. Lego can be used to make a quick and easy base. Take the time to construct a base for the bottom servo that will hold it securely in place and allow for the bracket to turn freely.
Fasten the servo into the base before connecting the camera mount to the base. This will make connecting the camera to the Pi much easier. You can see in the picture below that I was able to construct a simple base support using a set of flat Lego and two brick high walls with a two-stud wide gap. Stud is the official term for the bumps on a Lego brick according to the Lego website. Accurate vocabulary is important here.
Connect the Camera to the Pi
The process to connect the camera module is very simple. There is a detailed video with instructions on the official Raspberry Pi Camera Module page. I’ll summarize the steps here:
- Locate the connector directly behind the ethernet port on the Pi
- Using two fingers gently lift the plastic tabs on either side of the connector
- Orient the ribbon cable so the exposed tin leads point away from the ethernet connector on the board
- Slide the cable into the connector gently so it fits firmly and squarely with the board
- Using two fingers slide the plastic connector closed
Attach the Camera Mount to the Bracket
Using the 6-32 screw, screw through the mounting hole in the camera mount into the bracket. Use the smaller slot in the bracket to attach the camera near the front rather than the large center hole. I used the 6-32 because after much trial and error I discovered I did not have a nut/bolt combination small enough and in a last ditch effort to avoid a trip to the hardware store I tried out some PC chassis screws I had laying around. It happened to thread perfectly into the bracket top and did not require a bolt. So do what works for you.
Connect the Servos to the Pi
Servos have three wires for connecting to a controller. Red, Black, and White wires are for connecting power, ground, and signal respectively. For these instructions I will refer to the bottom or pan controlling servo as servo 0 and the top or tilt controlling servo as servo 1. This distinction is important and will become more clear in the Servoblaster section of the tutorial. The pinout diagram on elinux.org has nice detail on the pins and their purposes. The picture below shows the colored M/F jumpers connected to the correct pins for this tutorial.
For assembly, connect the male end of the jumper to the female servo wiring harness for each of the three colors respectively. As my jumper pack didn’t have white, I used yellow. Use a consistent color in your scheme to make sure you don’t mix up the functions.
- Connect the servo 0 red power wire to pin 2 (+5v), uppermost right pin in the picture below
- Connect the servo 0 black ground wire to pin 6 (ground), third pin from the top on the right column
- Connect the servo 0 white/yellow signal wire to pin 7 (GPIO 4), 4th pin down on the left column
- Connect the servo 1 red power wire to pin 4 (+5v), second pin down on the right
- Connect the servo 1 black ground wire to pin 14 (ground), 7th pin down on the right column
- Connect the servo 1 white/yellow signal wire to pin 11 (GPIO17), 6th pin down on the left column
Now that the hardware is all assembled and connected the software to control the servo is required. The servoblaster github site has good documentation about the software if you want to read more.
Essentially, the servoblaster install adds an init script to start the servoblaster process at boot. The servoblaster process creates a
/dev/servoblaster entry that when addressed correctly controls the PWM signal that controls the position of the servo motor. From the Pi console or from and SSH session, exececute the commands below to download and install the software.
wget https://github.com/richardghirst/PiBits/zipball/master mv master pibits.zip unzip pibits.zip #unzip the code cd richardghirst-PiBits-ab34d81/ServoBlaster/user sudo make install #install the software #change timeout from 2000 to 500 /etc/init.d/servoblaster sudo pico /etc/init.d/servoblaster sudo reboot
Send Commands to the Servos
After the Pi has restarted with the servoblaster process now running, you can try out a few commands to move the bracket around. SSH into your Pi and try out the commands below.
The following command will move the pan or left/right axis servo, servo 0. Replace X with a value between 60 and 240. This will move the servo around. Try a few different values.
echo 0=X > /dev/servoblaster
The following command will move the tilt or up/down axis servo, servo 1. Replace Y with a value between 60 and 240. This will move the servo around. Try a few different values.
echo 1=Y > /dev/servoblaster
Take Pictures and Video With the Camera
Taking pictures with the Pi is very simple. Try out the command below to snap a picture to the file named output.jpg. You’ll need to scp the file off of the Pi to your computer to see this, but you get the picture.
raspistill -o output.jpg
To take a video, the basic command is also quite simple. The following creates an h264 encoded video file that is 10000 milliseconds or 10 seconds long. Again, scp is required to copy this off the Pi and view it.
raspivid -o video.h264 -t 10000
Scripting Movements and Pictures
Now that you have the basics for moving the camera via the pan/tilt bracket and servos as well as the commands to snap pictures, it’s quite simple to write a script that will move the camera to predefined angles and and capture pictures or images. If you setup apache on the Pi, you could drop these pictures or videos into the
/var/www/ directory for later viewing.
A cron task could also be created to execute the script at periodic intervals. You have all the basics for a nature or pet watching project. Below is a sample script I wrote to test out the project and a video of the system in action.
#!/bin/bash #go to starting possition echo 0=60 > /dev/servoblaster sleep 1 echo 1=135 > /dev/servoblaster sleep 1 raspistill -o step1.jpg echo 0=240 > /dev/servoblaster sleep 1 raspistill -o step2.jpg echo 0=130 > /dev/servoblaster sleep 1 raspistill -o step3.jpg echo 0=60 > /dev/servoblaster sleep 1 echo 1=60 > /dev/servoblaster sleep 1 raspistill -o step4.jpg echo 1=140 > /dev/servoblaster sleep 1 raspistill -o step5.jpg echo 1=220 > /dev/servoblaster sleep 1 raspistill -o step6.jpg echo 1=135 > /dev/servoblaster sleep 1 raspistill -o step7.jpg
The video shows the pan/tilt bracket responding to the script above:
How to Build a Wireless Pi Camera Pan and Tilt Platform
Streaming Video Example
Still pictures are fun, but in the next experiment you will be able to live stream video from the camera to your computer across the network. Depending on the speed of your wifi quality may vary. I used a Linux computer as the target or video receiver. Mplayer and Netcat exist for both Windows and OS X however, some syntax may vary for those operating systems. Use ifconfig, ipconfig, or ipconfig to determine your IP address on Linux, Windows, or OS X respectively. In this example 192.168.1.100 is the address of the target laptop.
The command below will listen on the target computer for incoming video on port 5001 and display it:
nc -k -l 192.168.1.100 5001 | mplayer -fps 31 -cache 1024 -
The command below will start recording video on the Pi and stream it to the target display computer:
raspivid -t 100000 -o - | nc 192.168.1.100 5001 &
The & at the end of the command on the Pi drops the instruction into the background so you can use your open terminal to issue movement commands. Try some servo commands like the ones below and watch how the video responds.
echo 0=60 > /dev/servoblaster echo 1=60 > /dev/servoblaster echo 1=140 > /dev/servoblaster
Congratulations! You now have a wireless, pan/tilt camera that you can script and hack for your specific needs. As a next step you might try enhancing it by constructing a full lego case for the Pi and the bracket together. Another exercise might be to setup a cron job to take pictures at certain times and directions and email them to you.
This tutorial taught you the basics of how to attach and setup the Pi camera module, how to connect and control multiple servos, how to take command line control of the camera for still pictures and videos and finally how to test out your setup with some low level video streaming commands. With these basic tools you can now add a versatile camera platform to you future projects.
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post