Intro: Building a 3D Printed Pan Tilt Case for a Raspberry Pi
This instructable describes the building of a case for a Raspberry Pi that incorporates a pan-tilt assembly for the Pi Camera.
My goal was to design a 3D printable case that would neatly encompass the Raspberry Pi while also providing space for converting a single power source (12V) into two regulated supplies. One of those supplies would drive the Raspberry Pi and the other would drive the two servos. My experience with the Raspberry Pi has indicated that it can be very sensitive to power fluctuations. While you could drive the Pi and the servos from a single power input the starting and stopping of the servos may well cause issues for the Pi.
The end product of this instructable is a working, but very rudimentary from a software perspective, webcam . It has the mechanics, electronics, and foundational software for pan and tilt control and can be used as a development platform for your software.
There is no integrated pan tilt webcam software at the end of this instructable! What there is, however, is a hardware and software platform that can be leveraged for your own development goals.
Step 1: Case As Designed in Sketchup
From notepad concept to the renders of the case as it was designed in Sketchup. There are actually two versions of the case, one that fits the original Raspberry Pi and one for the new version.
There are also two variants of the back plate for the case with one allowing for mounting on a wall and the other assuming the case will be free standing.
Finally, the camera mount consists of two pieces with the part that will hold the Pi Camera attaching to a mounting plate that could, in theory, also host some other flavor of camera.
Step 2: Print, or Source, the Components for the Case
The case is available for a nominal fee from cgtraders (original RPi)(newRPi). The download will contain all of the needed .STL files which can then be printed on your own printer or on one of the 3D Hub Printers in your area if so desired.
The parts that you need are as follows:
- Case. This is where all the goodies sit and comes in two versions (one for the old, and one for the new, Raspberry Pi).
- Two or four spacer washers for mounting the Raspberry Pi (two if the old board and four if the new board).
- Two or four screws to attach the Raspberry Pi to the case.
- Back. This comes in two versions as well, one if the webcam is going to be free standing and one if it is to be mounted on the wall.
- Wall mount (optional). This is needed to marry with the wall mounted version of the back mentioned above.
- Pan. This is the pan platform that attaches to the servo that is mounted in the case for the pan action.
- Tilt. This attaches to the Pan Platform to allow for the tilt action.
- Tilt Servo. This attaches to the Tilt and provides a place to mount the tilt servo. The camera will be mounted on this component.
- Camera Mount (could be optional). Attaches to the Tilt Servo and provides a place for the Pi Camera to live. Is optional in the sense that some other camera 'could' be mounted on the Tilt Servo Mount's face plate.
Step 3: Gather the Parts for the Power Supply PCB
There is a custom PCB designed for the power supplies that drive this project though using the PCB is not absolutely necessary. If the PCB is used then you wil need the following parts:
- One x The PCB itself
- Two x DC-DC 5V/12V/24V 3A Step Down Power Supplies (LM2596)
- One x 5.5 x 2.1 mm DC Power Supply Jack Socket Female Panel Mount
- One x 2-Pin 2 way Screw Terminal Block Connector 5.08mm Pitch PCB Mount
- Two x Three Pin Headers for Servo Connectors (Note 1)
- Two x Two Pin Headers for Raspberry Pi Power and Signal Outputs (Note 1)
- Eight x One Pin Headers for Power Supply Mounting (Note 1)
- Four Female to Female patch cables to connect Rpi GPIO to PCB (two for power and two for GPIO pins 17 and 27).
Note 1: The one, two, and three pin headers were cut from a strip (shown above) to the length that is needed.
The Power Supply PBC is available for sale on eBay UK for a nominal fee.
Step 4: Gather the Other Miscellaneous Parts
The various other miscellaneous parts that will be needed include the following:
- 10 screws (Stainless Steel No2 x 1/2" Pozi Countersunk Self Tapping Screws M2.2 x 13mm):
- Four to secure the back to the case
- Two to secure the Pi Camera to the camera mount
- Four to secure the power board to the case
- Four 8mm to secure the camera mount to the tilt servo mount
- Two 8mm to secure the tilt mount to the pan platform
- One 16mm as a hinge for the tilt
- Nuts for the above
And finally...a Raspberry Pi and a Pi Camera Module (with the long ribbon cable), an SD card, and probably a wireless USB adapter!
Step 5: If the PCB Is Not Used
A PCB is not absolutely needed (though it does look very sharp)! There is a printed part that can serve as a platform and the various bits can be simply soldered together following the above diagram.
You might even be able to run the servos from the same power supply as the Raspberry Pi. I think this may cause you problems...but...it might not...!
Step 6: Assemble the PCB for the Power Supply
All components should be soldered to the top of the circuit board (where the lettering is printed). First start by breaking the header into the pieces you will need:
- Eight one pin pieces for the power supply inputs and outputs
- Two three pin pieces for the servo connectors
- Two two pin pieces with one for power for the Rpi and the other for the control lines from the Rpi to the servos.
Then we solder:
- Start by soldering four one pin headers to the bottom of each of the two power supplies. The short pin should extend up through the power supply board and be soldered on the top.
- Now you can solder the two power supplies onto the PCB.
- Next solder the two and three pin headers as shown in the picture above.
- Finally solder the two pin screw connector to the PCB. This is the 12V input connection.
That is almost all done. The only remaining task is to solder the short pair of conductors to the "5.5 x 2.1 mm DC Power Supply Jack Socket Female Panel Mount". This socket will be placed through the hole in the case and will provide power to the PCB.
Step 7: Insert the Raspberry Pi Into the Case
Attach the Pi Camera cable Raspberry Pi and insert the board into the case. Use the screws with the standoff spacers to secure the RPi to the case. If you are not sure that the cable is attached correctly, and that the camera is working, now might be a good time to skip ahead in the instructions just to make sure!
Step 8: Install the Pan Servo
Don't skip this step thinking that you can do it after the power supply board is in place! You will be peeved!
The servo should fit snuggly into it's resting place with the spinny bit positioned on the higher side of the case. Use a screw from the servo accessories to secure the top of the servo. The bottom should be snug in a groove printed into the case.
Step 9: Mount the Power Supply Board
Now take the assembled power supply board and mount it onto the case using the four screws. The board should be oriented with the headers on the end where the pan servo will be mounted. The power input connector will then be at the end where the power will come into the case.
Once the board is mounted you can thread the leads from the power plug through the hole, slide on the securing nut, and then attach the two leads to the two terminal block at the end of the board. I am not sure why I chose not to label which terminal is the ground and which is positive...but...the ground lead should go on the terminal closest to the wall!
Once this has been done the power supply outputs should be checked using a volt-ohm meter. Check both of the power supplies and use the little set screws to adjust to 5v output if necessary.
Step 10: Prepare the Pan and Tilt Mechanism
Collect the parts for the Pan and Tilt Mechanism as shown above. You will need the two printed parts, Pan and Tilt, two short bolts, three nuts, and two servo horns (find two that best match the indents in the printed parts).
Take the servo horns and trim them to size (if needed). Ideally you should be able to squeeze them into their recesses without requiring the use of a securing screw. If one is needed, or if you are a purist, then you will need to make your own starting hole for said screw.
Insert one of the nuts into the recess for it on one arm of the Tilt part. It should be a snug fit.
Finally, use the other two short bolts, and two nuts, and secure the Pan and Tilt parts together as shown above.
Be careful with the amount of force that you put on the printed parts! Due to their having been printed a layer at a time they are more fragile than injected parts that you might be more familiar with from other projects. If you do break a part, and they were printed with ABS plastic, remember that acetone is your friend. Just be sparing!
Step 11: Mount the Pan Tilt Mechanism Onto the Case
Before mounting the pan tilt mechanism you will want to manually center the servo. It should rotate through 180 degrees. Turn it all the way in one direction and then back 90 degrees. At this point you should be able to slip the pan tilt mechanism, assembled in the last step, onto the servo. Use one of the screws from the servo accessories to secure it.
Step 12: Mount the Tilt Servo Onto the Pan and Tilt Assembly
This is probably the biggest pain in the ass part of the assembly process. At least it usually is for me. As with the pan servo start by centering the tilt servo. Then mount it on the servo horn that is embedded in the tilt assembly as shown above. The face plate for the camera should vertical and facing towards the bottom of the case.
Remember that these printed parts can be fragile!
Complete this step by threading a bolt through the tilt servo mount into the tilt mechanism and the nut that is embedded in the plastic part for this purpose.
Step 13: Secure the Camera Mount to the Tilt Servo
Secure the camera mount to the tilt servo using four small bolts and their matching nuts.
Step 14: Install the Pi Camera
Thread the servo cable through the slot provided and down into the case. Thread the camera cable from the case through that same slot, then through the slot in the base of the camera mount. Pull the cable far enough through the case and camera mount so there is slack to work with and attach it to the camera. Then slide the camera down into the camera mount and secure with a couple of screws. Pull the slack from the camera cable, and any from the servo cable, back into the case.
Step 15: Complete the Wiring
Connect the two servo leads to the appropriate headers. Brown is ground. Tuck the two servo leads into the upper corner of the right side of the case (as you look towards the back). There is a cable tie down built into the case if you are deleterious and have tiny cable ties.
Connect the power from the PCB header to the 5V and Gnd pins of the Raspberry Pi GPIO. Connect the two GPIO signal lines to pins 17 and 27 as shown by the above diagram.
Double check, then triple check, that the power leads are where they are supposed to be!
Step 16: Put on a Back
Secure whichever back you have chosen using four screws and the assembly portion of this project is complete!
Step 17: Check for Installation of the Pi Camera Software
The stock distribution of Raspbian has included support for the Pi Camera for a while so it is likely that you will already be ready. Test a capture by typing the following command:
- raspistill -o image.jpg
The light on the camera should come on for a second or so and an image file created. If this did not happen, and/or you got an error message then run "sudo rasp-config" to make sure the Pi Camera is enabled as shown above.
If the Pi Camera does not show as an option under raspi-config you can try to do an upgrade, at your own risk, and via the following commands:
- sudo apt-get update
- sudo apt-get upgrade
If you still do not have an entry for the Pi Camera in raspi-config then you probably want to download a new image!
Step 18: Install Servo Control Software
There are a number of ways to control the servos but I like the pigpio library. You can excercise this library through a variety of methods including a:
- C function interface
- /dev/pigpio pipe interface
- socket interface (used by the pigs utility and the Python module).
My demonstration will be with the pigs utility and the socket interface.
Install the source code for this library with the following git command:
"git clone https://github.com/joan2937/pigpio"
Change directory into pigpio and do a "make" and then a "make install". There may be a delay during the make but be patient.
The pigs utility controls the gpios by passing messages to the running pigpio library. This library can be started by issuing the following command:
Normally this would be done at boot but for demo purposes we can do it at the command line.
Once the library is loaded you can do pigs at the command line to verify that it is operating. The pan servo is on GPIO 17 and the tilt servo is on GPIO 27. The command we are going to use is "SERVO u v" where "u" is the GPIO address and "v" is the pulse width. For example:
- "pigs SERVO 17 2500" will move the pan servo all the way clockwise,
- "pigs SERVO 17 500" will move it all the way counter-clockwise, and
- "pigs SERVO 17 0" will turn it off
Step 19: Install Some Webcam Software
Now that we know that the Pi Camera is working, and that we can pan and tilt, we are ready to install some software to stream images from the Pi Camera to a web browser on your network. The software that we are going to use for this purpose is called motion.
Here are the installation steps with some narrative as to what is happening:
- sudo apt-get update -->Makes sure that you are working from the most recent index of available software
- sudp apt-get install motion --> Installs the motion software
- sudo apt-get install -y libjpeg62 libjpeg62-dev
libavformat53 libavformat-dev libavcodec53 libavcodec-dev libavutil51 libavutil-dev libc6-dev zlib1g-dev libmysqlclient18 libmysqlclient-dev libpq5 libpq-dev-->Since motion does not by default support the Pi Camera we need to install some additional libraries
- wget https://www.dropbox.com/s/xdfcxm5hu71s97d/motion-... -->Download a script and configuration file that will start motion and tell it to use the Pi Camera
- tar zxvf motion-mmal.tar.gz -->Decompress the stuff from above
- ./motion -n -c motion-mmalcam.conf -->Start the motion software configured for the Pi Camera
At this point, all having gone well, you will have a running webserver broadcasting the stream from your Pan-Tilt Pi-Camera. It will be on port 8081 of the host runing motion (e.g. http://192.168.2.102:8081 if your RPi were on the address 192.168.2.102).
If your Rpi is already running a webserver on port 8081 you will need to edit the motion-mmalcam.conf file and assign a new port.
Control of the pan and tilt is via a terminal session so it is cumbersome at best!
Develop something better?
Step 20: Developed Something a Little Better
In answer to my own question, yes I can develop something better than a terminal session to control the pan and tilt. Not a whole lot better. 'Def a little better and it provides an example of what could be done.
First thing that you need to do is to install Node. There are a couple ways to do this but the following is easy as it exploits a package that Adafruit (article here) makes available:
- curl -sLS https://apt.adafruit.com/add | sudo bash
- sudo apt-get install node
The first command adds the adafruit catalog to your system and the second then installs node from that catalog.
Once you have node you can create a directory for the application. I called mine node. In that directory are two files pantilt.js which is the Node.js code that takes input from a browser and runs operating system commands (pigs) to control the servos, and panttitl.html which is the html code for the web side of our app. There is also a subdirectory named private with it having a subdirectory called images. Images is where the little icons for the web display live. The code is attached to this page.
One last thing to do. From the node directory you need to run the following commands to install some stuff that makes working with the web side of things quite easy:
- npm install express
- npm install body-parser
Express is the framework that gives you the tools to interact with the web and body-parser does just that, it parses the body of an HTTP dialog.
The command node "pantilt.js" starts the application. I have this running in ".profile" along with the starting of motion in background mode.
The above pictures shows what this app looks like. The box represents the webcam's field of vision. Press anywhere inside the box and the webcam will point there. Pressing on one of the arrows causes the webcam to move in that direction by a smaller or larger increment.
Obviously the next iteration of this app would be to combine streaming output and the webcam control into one page but I have the time right now.