Simple library for controlling a raspberry pi based robot
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.


This will run a simple robot with a webserver on a raspberry PI with the Adafruit Motor Hat. I wrote this up for myself for fun and to help me remember how I set things up.

High level overview can be found in this article:


To get started, you should be able to make the robot work without the arm, sonar and servo hat.


  • program will run commands from the commandline
  • tests sonar wired into GPIO ports
  • tests simple DC motor wheels
  • tests a servo controlled robot arm
  • implements a simple driving algorithm using the wheels and sonal
  • runs an image classifying microservice

Example Robots

Here are two robots I made that use this software


Wiring The Robot


If you want to use the default sonar configuation, wire like this:

  • Left sonar trigger GPIO pin 23 echo 24
  • Center sonar trigger GPIO pin 17 echo 18
  • Right sonar trigger GPIO pin 22 echo 27

You can modify the pins by making a robot.conf file.


You can easily change this but this is what expects

  • M1 - Front Left
  • M2 - Back Left (optional - leave unwired for 2wd chassis)
  • M3 - Back Right (optional - leave unwired for 2wd chassis)
  • M4 - Front Right


basic setup

There are a ton of articles on how to do basic setup of a Raspberry PI - one good one is here

You will need to turn on i2c and optionally the camera


Next you will need to download i2c tools and smbus

sudo apt-get install i2c-tools python-smbus python3-smbus

Test that your hat is attached and visible with

i2cdetect -y 1

Install this code

sudo apt-get install git
git clone
cd robot

Install dependencies

pip install -r requirements.txt

At this point you should be able to drive your robot locally, try:

./ forward


To run a webserver in the background with a camera you need to setup gunicorn and nginx


Nginx is a lightway fast reverse proxy - we store the camera image in RAM and serve it up directly. This was the only way I was able to get any kind of decent fps from the raspberry pi camera. We also need to proxy to gunicorn so that the user can control the robot from a webpage.

copy the configuration file from nginx/nginx.conf to /etc/nginx/nginx.conf

sudo apt-get install nginx
sudo cp nginx/nginx.conf /etc/nginx/nginx.conf

restart nginx

sudo nginx -s reload


install gunicorn

copy configuration file from services/web.service /etc/systemd/system/web.service

sudo cp services/web.service /etc/systemd/system/web.service

start gunicorn web app service

sudo systemctl daemon-reload
sudo systemctl enable web
sudo systemctl start web

Your webservice should be started now. You can try driving your robot with buttons or arrow keys


In order to stream from the camera you can use RPi-cam. It's documented at but you can also just run the following

git clone
cd RPi_Cam_Web_Interface
chmod u+x *.sh

Now a stream of images from the camera should be constantly updating the file at /dev/shm/mjpeg. Nginx will serve up the image directly if you request localhost/cam.jpg.


There is a great project at that gives instructions on installing tensorflow on the Raspberry PI. Recently it's gotten much easier, just do

sudo pip install tensorflow-0.11.0-cp27-none-linux_armv7l.whl

Next start a tensorflow service that loads up an inception model and does object recognition the the inception model

sudo cp services/inception.service /etc/systemd/system/inception.service
sudo systemctl daemon-reload
sudo systemctl enable inception
sudo systemctl start inception