We are making a robot from a Raspberry Pi Zero. This project will be updated as we are building and learning. We want to share our progress with the community. Videos will be posted to our YouTube channel.
I'm using a Mac, but I'll try to link alternative methods for Windows users too.
How to use this guide:
Steps you need to do are bulleted items. All command line code and robot code is provided in the code blocks and on our GitHub. The text blocks are general explanations. I will be linking back to all the resources that we use to give them credit, provide you with more in-depth information, and these sites might update something.
Noob notes -- Text in this quote format is further explanation of a topic. It is meant for noobs like me. More experienced makers can skip these and just use the code and steps.1. Installing Rasbian Image
This is a video walkthrough of installing Rasbian and setting up the Pi with both the peripherals and headless version.
Preloaded (Option 1)
We purchased a kit that came with a pre-loaded microSD card. If you don't have a pre-loaded card. This is a link to the instructions to download NOOBS, which contains the Raspberry Pi OS, Raspbian.
Unzipping NOOBS (Option 2)
https://www.raspberrypi.org/learning/software-guide/quickstart/
- Use microSD that is larger than 4Gb adapter and insert into computer
- Reformat SD to FAT32
- Download NOOBS
- Unzip file
- Copy and paste contents of NOOBS onto formatted SD card
Flashing the Rasbian Image (Option 3)
https://www.raspberrypi.org/learning/software-guide/quickstart/
https://www.raspberrypi.org/documentation/installation/installing-images/
- Use microSD that is larger than 4Gb adapter and insert into computer
- Reformat SD to FAT32
- Download latest versions of Raspbian
- Go to etcher.io and install software for flashing OS to SD
- With etcher: Select your image (Rasbian). Select your drive (SD card). Flash the SD (this takes a bit)
I will show you two options for setting up the Pi Zero. Method 1 uses a keyboard, mouse, and monitor. Method 2 uses your computer's command line. The second method is called headless.
I prefer to create and execute code from my computer and not the Pi Desktop. I will show you how to command the Pi from your computer via secure shell (SSH). You can also alter the Pi's files from your computer using file transfer protocol (FTP). These are both made easier by setting up a static IP for the Pi. If you want, you can skip these steps and just use the Pi desktop. I will be using the headless version in this project.
2a. Using Peripherals (Method 1)The microSD, HDMI, and peripherals should be connected before applying power to the Pi.
- Insert the microSD card.
- Plug HDMI into Pi and TV/external monitor. We used an mini HDMI adapter that came in our kit.
- The microUSB port closest to the HDMI port is for peripherals. We used an adapter to plug in a bluetooth connection for a keyboard with a mouse.
- Plug power cable into board and wall. The second microUSB is for the power.
Install OS
Once you apply power to your Pi, a screen will pop up showing you a box with Raspbian asking you if you want to install it.
- Click on Raspbain
- Reboot after install
It will take a while for the OS to install. Once it installs, the Pi will need to reboot.
Configuring PI
- Click on the menu in the upper left corner.
- Go down to preferences.
- Change the name of your Pi and the password.
- Go to the interfaces menu.
- Enable SHH (optional - this is for controlling your Pi from your computer)
- Enable I2C
- Enable SPI
- Your Pi will need to reboot again to make these changes.
- Turn on WiFi in the upper right corner of the desktop
- Chose the wifi network your computer is connected to and enter the WiFi password.
SSH (Optional)
Enabling Secure Shell (SHH). SHH will allow us to control the Pi from the command line of our computer, and we won't need these peripherals anymore. This is optional. You can just use the setup you currently have, but the rest of the examples in they project will be using my computers command line.
You type in the same commands you would enter in the terminal of the Pi but from your computer. For Macs, you can use terminal. For Windows, PuTTY is a popular SHH program. This article is a brief overview of linux command line.
Go to the command line of your computer and enter the following:
ssh pi@<nameofpi>
The default name is RaspberryPi and the default password is raspberry. If you changed your username and password, use that.
We no longer need the external monitor and keyboard to control the Pi.
Create Static IP (Optional)
You can just use the name of your Pi for SHH. It is easier to use a static IP address for the Pi for some of the IoT projects we plan on doing.
I setup a static IP for the RPi. There is a detailed video by Adafruit and a post on StackExchange. This is method for Raspbian Jessie and above.
- Use ifconfig to find your IP address.
ifconfig
- Then open the config file.
Noob note: sudo is advanced permissions that allow you to more access and higher permissions. Nano is a text editor for RPi, which we need to open documents like this config file.
sudo nano /etc/dhcpcd.conf
- Add the following to the the bottom of the config file (ip_address is the static IP of the Pi), save, and exit by hitting enter (I always forget that this is how you get out of nano).
interface wlan0
static ip_address=192.168.1.XX
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
- Then reboot.
Noob note: to shutdown your Pi, you don't unplug it. You type sudo shutdown -h. You can add now to the end of it if you want to do it immediately. Sudo halt works too.
sudo reboot
- Check in terminal to see the address.
nslookup <NameofYourPI>.local
Setting up FTP (optional)
Personally, I like to open the files form the Pi on my computer, alter the code, and then run the code from the computer's command line. You can just write the code on the Pi or use a text editor, like nano, in the command line. To alter files on the Pi from my computer, I use File Transfer Protocol.
- I installed netatalk (for Mac). If you have windows, a popular one seems to be Fillezilla.
sudo apt-get install netatalk
- On the Mac, I hit command+k to bring up the "connect to server" window.
- Enter the address. The server's address is afp://<static IP address of Pi>
afp://192.168.1.XX
It asks for a name and password.
- The name is pi.
- The password is whatever you changed it to on the Pi desktop.
- I added this to my favorite servers.
It will popup a Home Directory and show you the file system for your Pi.
2b. Headless (Method 2)If you don't want to hook up a monitor, keyboard, and mouse, you can create files in the boot setction of the SD card that will allow you to connect to the internet and enable SHH.
Connect to the internet
Walkthrough on Windows.
- Go to the boot partition of the Rasibian image.
- Create a plain text document named wpa_supplicant.conf.
- Enter the text below replacing it with your information. Adafruit also adds scan_ssid=1, but that is only needed for hidden SSIDS.
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
network={
ssid="YOUR SSID"
psk="YOUR PASSWORD"
}
Plain text editors -Notepad for Windows. TextEdit for Mac. Nano on Linux. For the .config extension, you might have to save the file as .txt and then change the extension when you are done. If you need to edit the contents of the file, you change the extension back to .txt.
Enable SSH
- Create plain text file with nothing in it.
- Name it ssh with no extension.
Enabling Secure Shell (SHH). SHH will allow us to control the Pi from the command line of our computer. This file will only work for one boot up. We will have to enable SSH in the config setting of Pi still.
For SSH, you type in the same commands you would enter in the terminal of the Pi but from your computer. For Macs, you can use terminal. For Windows, PuTTY is a popular SHH program. This article is a brief overview of linux command line.
- Put microSD card in Pi.
- Plug in power cable to Pi and wall power.
- Go to the command line of your computer and enter the following:
ssh pi@<nameofpi>
- The default name is RaspberryPi
- The default password is raspberry.
Configure Pi
Use this to enable I2C and SPI (for robot), change password, and rename Pi. You need to change the config to enable SSH. The text doc trick only works once. You'd have to add it every time you boot.
sudo raspi-config
- Go to change user password and hit enter.
- Hit enter when prompted
- Type in a new password and reenter it
- Arrow down to Network options and hit enter
- Hit enter on Hostname
- Hit enter on <ok>
- Enter a new name for your Pi (to SSH you will note use this name pi@NewName)
- Arrow down to <ok> and hit enter
- Arrow down to Interface options and hit enter
- Arrow to SHH and hit enter
- When cursor is one <Yes>, hit enter and wait.
- Using the Interface Options menu, enable SPI and I2C just like you did SSH
- Arrow down to <Finish> and enter
- Hit enter on <Yes> to reboot the Pi for the configuration changes to take place.
Create Static IP
You can just use the name of your Pi for SHH. It is easier to use a static IP address for the Pi for some of the IoT projects we plan on doing.
I setup a static IP for the RPi. There is a detailed video by Adafruit and a post on StackExchange. This is method for Raspbian Jessie and above.
- Use ifconfig to find your IP address.
ifconfig
- Then open the config file.
Noob note: sudo is advanced permissions that allow you to more access and higher permissions. Nano is a text editor for RPi, which we need to open documents like this config file.
sudo nano /etc/dhcpcd.conf
- Add the following to the the bottom of the config file (ip_address is the static IP of the Pi), save, and exit by hitting enter (I always forget that this is how you get out of nano).
interface wlan0
static ip_address=192.168.1.XX
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
- Then reboot.
Noob note: to shutdown your Pi, you don't unplug it. You type sudo shutdown -h. You can add now to the end of it if you want to do it immediately. Sudo halt works too.
sudo reboot
- Check in terminal to see the address.
nslookup <NameofYourPI>
Setting up FTP
Personally, I like to open the files form the Pi on my computer, alter the code, and then run the code from the computer's command line. You can just write the code on the Pi or use a text editor, like nano, in the command line. To alter files on the Pi from my computer, I use File Transfer Protocol.
- I installed netatalk (for Mac). If you have windows, a popular one seems to be Fillezilla.
sudo apt-get install netatalk
- On the Mac, I hit command+k to bring up the "connect to server" window.
- Enter the address. The server's address is afp://<static IP address of Pi>
afp://192.168.1.XX
It asks for a name and password.
- The name is pi.
- The password is whatever you changed it to on the Pi desktop.
- I added this to my favorite servers.
It will popup a Home Directory and show you the file system for your Pi.
3. Setting up Motor ControllerHow to install and use CircuitPython on Raspberry Pi Zero
We already had an Adafruit Feather Motor Controller board. There are Arduino and CircuitPython libraries written for this Feather board. CircuitPython can be installed on the RPi in order to use the motor libraries. We followed instructions in the two links below to install the proper libraries.
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/overview#
https://learn.adafruit.com/adafruit-io-basics-servo/python-setup
What I learned -- CircuitPython, created by Adafruit, is a special version of MicroPython. Embedded systems and microcontrollers, such as Arduinos, often use C. The program is complied and the machine instructions are sent to the board. Python is a higher level interpreted language that has been too large and inefficient to use on boards. MicroPython is a special implementation of Python that can be run directly on boards. It is a subset of Python with special features to talk to aspects of boards like GPIO.
First, we updated the Pi.
sudo apt-get update
sudo apt-get upgrade
sudo pip3 install --upgrade setuptools
We had already enabled SPI and I2C interfaces when we setup the RPi, but you can do it from the command line if you forgot. We also had python 3 installed on the Pi.
We installed the library for accessing GPIO pins. We needed to install the adafruit_blinka library. Blinka "translates the CircuitPython hardware API to whatever library the Linux board provides." We also installed the libraries used by the Feather motor board (PCA9685 and Motor). Motor Kit looked like even cleaner code than the examples in the Motor library. So we installed that library too.
If you have permission problems, you can using sudo pip3 to install the libraries.
Pip is a package manager for python like npm is Node Package Manager for JavaScript. The packages will will be installing are on PyPi. CircuitPython uses Python 3, which is why we are using pip3. This how to install pip and its setup tools. https://bigchaindb-server.readthedocs.io/en/v0.6.0/appendices/install-latest-pip.html
pip3 install RPI.GPIO
pip3 install adafruit-blinka
pip3 install adafruit-circuitpython-PCA9685
pip3 install adafruit-circuitpython-motor
pip3 install adafruit-circuitpython-motorkit
- Write the motor test file file.
You can can write code below using nano, on the Pi, or on your computer. I wrote it on my computer, saved it to the Pi using FTP, and executed it on the terminal.
import time
from adafruit_motorkit import MotorKit
kit = MotorKit()
try:
while 1:
kit.motor1.throttle = 0.3
time.sleep(1)
kit.motor1.throttle = 0
time.sleep(2)
kit.motor1.throttle = -0.2
time.sleep(1)
kit.motor1.throttle = 0
time.sleep(2)
except KeyboardInterrupt:
kit.motor1.throttle = 0
pass
Execute motor test file.
When you execute a file on the Pi, you need to file go to the directory where that file is. You can use cd to drill down into a director. You can use ls to list all the filed and folders in the current directory. You can use the tab key to autocomplete a line instead of typing the whole file or folder name.
python3 motorTest.py
NOTE some libraries might need -
pip3 install circuitpython-build-tools
Comments