Introduction: 3D Printed Raspberry Pi Zero Robot
Have you ever wanted to build a robot, but just didn't have all the materials to build one without ending up with an unnecessarily bulky chassis? 3D printers are here to save the day! Not only can they create parts to be compatible with virtually any hardware, they can do it in a very space efficient way. Here I will show you how to create a very basic robot which features 3D printed parts, the Raspberry Pi Zero, and the Pi Camera. I would encourage you to take and modify what I have done to fit your practical or entertainment needs. To control the robot and view the camera feed, I built an IOS app (the PiBotRemote app) that you can feel free to use and modify. However, the real power in projects such as this derives from the diversity in the possibilities in both hardware and software. So I would encourage you to be creative and add to what I've done depending on what you know how to do. For example, I think it would be cool to make this robot use machine vision to recognize its surroundings and navigate similarly to a self-driving car.
Step 1: Requirements
- Materials
- Required (Roughly $75)
- Optional (Roughly $45)
- Tools
- 3D Printer & Filament
- Computer (I'll be using a mac, and you will need one if you wish to use the PiBot Remote app)
- iPhone/iPad/iPod Touch (If you will be using the app)
- Drill
- Screwdriver with changeable tips
More info on parts
- Pi Zero: If you want to use the Pi Zero for only this project, you will be fine with running headless all the time. Otherwise, if you will ever want to connect an HDMI output or USB peripheral you will need to purchase additional adapters. In this case, it is most likely the most cost-effective option to buy a Pi Zero kit, like this ($24) one, I bought off amazon. Although I still had to buy a micro SD card, this kit came with the Pi Zero, both necessary adapters, and many different headers. All of which can be useful.
- Micro SD Card: You can use any Micro SD card as long as it has a minimum of 8GB of storage.
- Jumper Wires: I like jumpers like these because they come as a connected bundle. This allows me to separate, say, a 9-wire section, and neatly connect the Pi and the motor driver.
- USB Battery: The battery I bought off Sparkfun has since been discontinued. As a result, you will need to find one elsewhere. The one I linked looked similar to mine but I have not bought it, and you may need to modify the print files to fit your battery. Make sure to find a battery with an attached micro USB cable, as this allows you to plug directly into the pi without excess wire.
- Motor Driver: I would recommend using the diver I linked to since it is quite cheap, and the print is designed to exactly fit that board. Additionally, other boards may function differently, and you may have different results.
- 14mm Steel Ball: I used this ball simply because I happened to have one laying around. Feel free to use other sizes, but you may need to change the socket size. The ball will serve as the 3rd wheel for our robot. This is one of the areas of design on my robot that is the most problematic right now and could use the most improvement. While it works fine on smooth, hard surfaces, it has trouble on carpets and rougher surfaces. Feel free to change this area of your design.
- Screws, Nuts, Standoffs: You may need to work a little to find screws that work for you. I simply found the screws mounting the Pi, as well as the screws keeping the Pi Camera mount together in my dad's screw collection. For the motor mounts and sockets, I used these ($2.95) screws, and these ($1.50) nuts, which are both available at Sparkfun. The standoffs and 8 screws (I accidentally only included 4 in the picture) that hold the robot together I took from my school's unused VEX kits.
- LEDs: I'm sure you know where you can easily find you some LEDs. Choose whichever colors you want to represent the functions: power, connection, robot replaying path, and robot receiving instruction.
- Camera and Servo: Depending on what you wish to do with your robot, you may choose not to include the Camera and Servo since they are not necessary for basic movement, and add $45 to the cost of the robot.
Step 2: Pi Zero Setup
Follow this link to set up a headless install on your Raspberry Pi Zero W
- Don’t forget that the Pi Zero cannot connect to a 5GHz Wi-Fi network
Be sure to follow the instructions for Raspbian Stretch or later
Once you have successfully connected via SSH to your pi, run
sudo raspi-config
and change the following configurations:
- Change your password. It is very dangerous to leave the default password raspberry. Make sure you remember this password.
- In Network options, change the hostname from raspberrypi to something shorter like pizero, or pibot. I will use pibot for the rest of this tutorial. Be sure to remember what you put here.
- In Boot options -> Desktop / CLI, select Console Autologin
- Go to interfacing options and enable the Camera
Select Finish and reboot the device.
Step 3: Set Up the AdHoc Network
By setting up an AdHoc network, we will be able to connect our controlling device directly to the robot without any intermediaries. This will allow for quicker streaming of video and lower latency of control. However, this step is not necessary as everything will still work through a normal wifi network.
First, you will need to download and unzip all the necessary files from GitHub. In terminal, navigate to the downloaded folder and send the PiBotRemoteFiles folder to the pi with the command:
scp -r PiBotRemoteFiles/ pi@pibot.local:Desktop/
This sends all the necessary files to the robot that will control it and set up the AdHoc network. Make sure that the files are in a folder called "PiBotRemoteFiles" that is located on the desktop; otherwise many things will fail to work down the road. If you will be using the PiBot Remote app, you can switch between normal Wi-Fi and an AdHoc network in the app settings. Otherwise, you can manually change it through SSH with one of the following commands:
sudo bash adhoc.sh
sudo bash wifi.sh
Of course, make sure you have navigated to the PiBotRemoteFiles folder before you run the preceding commands. Any change between AdHoc and Wi-Fi will only take effect after the next restart. If an AdHoc is set up, then you should see a PiBot network appear when the Pi Zero boots.
Step 4: Add a Power LED
While certainly unnecessary, it can be useful to have a power light. In order to activate this, SSH into the Pi Zero and run the command:
sudo nano /etc/bash.bashrc
And add the following line to the end of the file:
python /home/pi/Desktop/PiBotRemoteFiles/startup.py
We will later change the GPIO pin associated with the power LED.
Step 5: RPi Cam Web Interface Setup
In order to tap into the Raspberry Pi Cameras video stream, we will use the RPi-Cam-Web-Interface. Information about this module can be found here and their code is on GitHub. To install the module we first need to update our Pi. This can take up to around 10 minutes.
sudo apt-get update sudo apt-get dist-upgrade
We then need to install git:
sudo apt-get install git
And we can finally install the module:
git clone https://github.com/silvanmelchior/RPi_Cam_Web_Interface.git
RPi_Cam_Web_Interface/install.sh
After the module is installed, a configuration window will appear. If you wish to add a username and password, be sure to use the same username and password as your pi’s account. Otherwise, the PiBot Remote app will be unable to receive the camera stream.
Now, if you go to a browser on a device on the same network as the Pi and if the camera is connected to the pi, you can receive the stream by going to http://pibot.local/html/#. The RPi interface allows for easy control of the camera, and by tapping or clicking on the video, it can become full screen. We will be using this later with the PiBot remote app.
We are now done setting up the Pi Zero, on to the fun stuff!
Step 6: Print Everything Out
Although I used a Dremel 3D printer with PLA filament, feel free to use your own printers and materials. All the STL files are in the folder you downloaded from GitHub. I was able to print everything out in four batches: the top plate, the bottom plate, all the mounts and sockets, and the ring. Be creative in your color choice and take full advantage of 3D printers abilities. My printer did not have dual extrusion or any such fancy features, but if have access to such a printer, I would recommend printing the decorations on the top of the top plate in a contrasting color. You will likely need to file and drill to make some pieces fit.
Feel free to paint the top plate to make LED symbols and decorations visible.
You may have noticed two mounts on the ends of the bottom plate that resemble the GoPro mounting system. Feel free to use these to attach whatever you like to the front or back of the robot. In the blender file, you can find a dry-erase marker mount I used, as well as a template object you can modify to hold your object.
Also, feel free to define whichever direction as forward; I've swapped at least three times so far.
Step 7: Solder on the Headers
Although I chose to solder on headers to the PiZero, you could solder your wires directly to the pi. If you choose to solder headers like I did, I would recommend using one with a right angle like mine. It keeps the wires much more hidden and makes everything look much neater.
Now its time to solder the motor driver. The bottom plate is designed especially for this Sparkfun motor driver, and allows for space for the for pin header to stick out the bottom. This allows for easy changing of motor pins so that you can swap left and right, and forward and backward. Although I include the following step now, I would heavily recommend waiting for a few steps until you know exactly how long your wires must be. Cut off a 9-wire section of jumper pins that are compatible with the header pins you just soldered into the pi. Carefully solder each wire so that group can collectively lie flat and wrap around the battery. Measure out the length of wire beforehand so you don't end up with too little or too much.
Finally, it's time to solder the LEDs. Stick them into their respective locations on the top plate, and fold all the ground pins on top of each other. Solder one wire to the ground, and one wire to each LED. From left to right, the functions of the LEDs are: robot power, app connectivity to the robot, the robot is replaying a saved path, and instructions are being received by the robot.
Also solder wires to each motor so that they can plug into the headers coming from the motor driver.
Step 8: Screw on the Motor and Socket
First, insert each motor into a motor mount. Then insert each screw partly, just until the tip reaches the surface of the mount or socket. Then, for each screw, hold in place a nut on the other side of the plate as you tighten each screw. Remember to place the bearing in-between the two sockets as you screw on the second. Place the motor driver in its position and plug the motors in. It doesn't matter which motor is plugged into each output as you can easily change that once the robot is up and running.
Step 9: Ready the Camera and Servo
Plug the Pi Zero adapter ribbon into the camera and screw the camera case together. Place the servo in its position. You may drill the screw holes for the servo, but it is plenty snug enough. Attach the camera to the servo in whatever way you find best. Currently, I have two holes in the mount, with a staple passing through the servo horn and the camera case. However, that leaves much wiggle room so you may want to use super glue. Point the camera in whichever direction you wish, and screw the servo horn in place. Fit the camera ribbon through the slit by the raspberry, and plug it into the pi. Finally, fold the ribbon to keep it flat against the battery.
Step 10: Putting Everything Together
It's finally time for everything to become one piece. Plug the wires from the LEDs, motor driver, and servo into the Pi in such a way that you use only valid pins, but keep them close to their exit. Then place the wires through their slots and screw the pi in place. This is designed to be a tight fit to keep things neat, so don't give up when it seems there is not enough room for those big jumper pins.
Screw in each standoff into the bottom plate so that each is secure. Insert the battery and make sure that the power cable can fit through the slot and into the Pi Zero's power port. Wrap the motor driver wires around it and fit the ring pice around everything. Once you have squeezed all the wires into the space between the battery and the top plate, small ridge on the bottom plate into the ring, and the two tall points on the ring into the top plate. You can now screw the top plate tight and you have built your robot!
Step 11: Open the Xcode Project
The next few steps only apply if you will be using the PiBot Remote app, which requires a Mac and an IOS device.
Because I am cheap and don't have paid Apple Developer account, I can only share the Xcode project, not the app itself. You can then open the project yourself, change the signing, and launch it on your own device.
If you don't have Xcode already, download it from the app store on your Mac. Once Xcode loads, choose "Open another project" in the lower right-hand corner, and navigate to the "PiBot Remote" folder in the GitHub download.
Once the project opens, click on the root file in the view on the far left called "PiBot Remote".
Change the "Bundle Identifier" to something unique. You could replace my name with yours, or add something to the end.
Change the Team, to your own personal account. If you don't have one, select "Add an account".
Hit command-B to build, and hope that everything works correctly. Once you have successfully built the project, plug your device into your computer. Click the button to the right of the play and stop buttons in the top-left hand corner, and select your device.
Hit command-R and the app should launch on your device. Your device may need to verify identities before it runs and will need internet access at only this time.
Step 12: Final Adjustments
You can adjust the pin numbers for everything except for the power LED in the PiBot Remote app. To change the pin for the power LED, SSH into the PI, and run the command:
/home/pi/Desktop/PiBotRemoteFiles/startup.py
Change the two instances of 36 to whatever GPIO pin you used. Then hit control-X, y, enter.
The app and server are both prone to errors. Use the console in debug mode to figure out what is going on. If in doubt, try restarting the Pi and/or restarting the app. Sometimes after a code failure, the app is unable to reconnect because the address is already in use. In this case, simply change the port and the app should connect.
Also, when driving the robot with the accelerator on your device, you have to use some inconvenient gestures to calibrate, stop/start, adjust the camera, and show/hide the tab bar
- Calibrate: Tap and hold with two fingers for .5 sec (if your device supports it, you will feel haptic feedback once the device has calibrated
- Camera Adjust: The most tricky gesture, do what was previously described to calibrate, then drag your fingers up to move the camera up, and drag down to move the camera down. The adjustment will be made once you lift your fingers.
- Stop/Start Toggle: When you go to the accelerometer view, the robot is initially set to ignore movement commands. To toggle this setting, double tap with two fingers.
- Show/Hide Tab Bar: To enable fullscreen viewing while in accelerometer driving, the tab bar will automatically hide after a few seconds. To show it again, swipe up. To hide it, swipe down.
If you get frustrated with the problems and inconveniences associated with my app, just remember that I have had no formal education in programming of any kind. So I welcome advice and suggestions. Feel free to fork my GitHub files.
If I make any adjustments on GitHub, apply them to the robot by downloading the files and sending them via recursive SCP to the Pi in the appropriate location. If you cloned the Xcode project simply pull the change. Otherwise, you can download the project and follow step 11 to open the app on your device.
If you do anything interesting with this tutorial, please let me know in the comments, I'm interested to see how it can be used as a template for all kinds of fascinating projects.