Raspberry Pi Robot


Hi, my name is Martin.

I would like to explain and show how easy and funny it is to build your own robot. Yes of course. This robot will only move around and display video from onboard camera but it is enough for beginning. Those of you who knows Raspberry Pi which I used as a base for this robot, also knows that this could not be the last stage and you can add lots of modules and sensors at the top. Hardware needed for this simple project is as listed here:

  1. Raspberry Pi (Model B or B+ is the best choice. Raspberry Pi 2 has no WebIOPi library ready at the moment of writing this article and ZERO is missing CSI port for camera)
  2. Raspberry camera (any USB web camera can be used, but I cannot help with it to be set up and running)
  3. SD card 4GB+
  4. USB WiFi dongle
  5. H-bridge (as a motor controller. I am using cheap L298N)
  6. External battery/battery pack (not necessary, but good for compact overall look)
  7. Cabling
  8. Some chassis with 2 DC motors


First step is to load a system image (Martin used Raspbian. ref: Picobot) to micro SD card. I use Win32DiskImager as I use Windows on my PC. (But there are lots of 'how to's on the Internet to do so with almost any PC OS. ref: Picobot)


Raspbian SD cakrd image installation


Once finished eject the card safely from your PC and insert it to your Raspberry Pi. Also connect the camera, WiFi dongle, keyboard, display and finally power supply. We need keyboard and display for configuration stage only. It can be done via network or UART but this is the easiest way.


When Raspberry Pi boots up for the first time it executes Raspi-Config script, so we can configure what we need to:

  • Expand Filesystem (to be able to use whole SD card capacity)
  • Boot to Console (to save some RAM)
  • Enable Camera
  • Split memory (set up to 128 or more, but 128 is enough)
  • Enable SSH (to allow future connection from PC via network. Turned on as default, but check it.)

There is nothing else we need to change, but we could also change Time zone or other details if we want to.


Raspberry config


Next step is to configure WiFi connection. I recommend to set up a static IP address to make it easy to find your Pi in the future. (Or set a DNS IP reservation if allowed by your network infrastructure. ref: Picobot )


After establishing a successful connection we need to update the OS:

sudo apt-get update

sudo apt-get upgrade -y

sudo apt-get dist-upgrade -y


It is recommended to reboot your Pi after update by using:

 sudo reboot.

After reboot we can carry on by installation. I recommend Putty SSH client as it allows to copy and paste commands directly to the Raspberry Pi console. But it is necessary to have both, PC and Raspberry Pi connected to the same network (Can be done by setting up port forwarding on your router to achieve connection from Internet, but it is not recommended. ref: Picobot). You can connect to your Raspberry Pi by entering the IP address already set above.


Putty SSH client


Now it is time to start working on our robot. Enter following commands into the terminal:


mkdir ~/Robot

mkdir ~/Robot/webiopi

mkdir ~/Robot/stream

mkdir ~/Robot/install


This was to create file structure to have place for our applications, scripts and all other necessary files. Following commands will install those.


cd ~/Robot/install

wget http://heanet.dl.sourceforge.net/project/webiopi/WebIOPi-0.7.0.tar.gz

tar xvf WebIOPi-0.7.0.tar.gz

cd WebIOPi-0.7.0

sudo ./setup.sh


So we have our WebIOPi library installed. WebIOPi is connecting GPIO pins with our Python scripts and allow web based control. Now we need to configure it to suits our needs.


sudo nano /etc/webiopi/config


Make changes as follow:


#myscript = na myscript = /home/pi/Robot/webiopi/script.py

port = to port = 80

#doc-root = na doc-root = /home/pi/Robot/webiopi

#welcome-file = na welcome-file = index.html


Raspbian Command Line


Now we can start WebIOPi:


sudo service webiopi start

sudo update-rc.d webiopi defaults


We need something to provide video signal on our web page. Mjpg-streamer is good to do this. Lets install it:


sudo apt-get install cmake libjpeg62-dev libv4l-dev imagemagick -y

wget https://github.com/jacksonliam/mjpg-streamer/archive/master.zip

unzip master.zip

cp -r ~/Robot/install/mjpg-streamer-master/mjpg-streamer-experimental/* ~/Robot/stream

cd ~/Robot/stream

make clean all


To make it start together with our Pi we will create short batch script.


sudo nano ~/Robot/webiopi/stream.sh


Insert following rows into the script file:


cd /home/pi/Robot/stream


./mjpg_streamer -o "output_http.so -w ./www" -i "input_raspicam.so -fps 10"


And now make it executed at OS start:


sudo nano /etc/rc.local


Insert following:


sudo bash /home/pi/Robot/webiopi/stream.sh


Stream script in Command Line


Having all parts installed, now it is time to do some work on scripts for robot:


cd ~/Robot/webiopi


Here we will create the file script.py and start creating our program.


sudo nano script.py


Program is here with comments to make it easier to understand and modify to particular needs. My H-bridge has been connected to GPIO pins 7, 17, 27 and 22. :


# library import

import webiopi

import time


# Enable debug output



# referencing library

GPIO = webiopi.GPIO


# output

IC1A = 4

IC1B = 17

IC2A = 27

IC2B = 22


# Web page connector

def setup():

   webiopi.debug("Script with macros - Setup")

   # Setting GPIOs to output

   GPIO.setFunction(IC1A, GPIO.OUT)

   GPIO.setFunction(IC1B, GPIO.OUT)

   GPIO.setFunction(IC2A, GPIO.OUT)

   GPIO.setFunction(IC2B, GPIO.OUT)


   #Setting GPIOs to off state

   GPIO.digitalWrite(IC1A, GPIO.LOW)

   GPIO.digitalWrite(IC1B, GPIO.LOW)

   GPIO.digitalWrite(IC2A, GPIO.LOW)

   GPIO.digitalWrite(IC2B, GPIO.LOW)


# Robot's loop

def loop():



# Reset GPIO pins at the end

def destroy():

   webiopi.debug("Script with macros - Destroy")

   GPIO.setFunction(IC1A, GPIO.IN)

   GPIO.setFunction(IC1B, GPIO.IN)

   GPIO.setFunction(IC2A, GPIO.IN)

   GPIO.setFunction(IC2B, GPIO.IN)


# Commands after call


def MoveLeft():

   GPIO.digitalWrite(IC1A, GPIO.HIGH)

   GPIO.digitalWrite(IC1B, GPIO.LOW)

   GPIO.digitalWrite(IC2A, GPIO.LOW)

   GPIO.digitalWrite(IC2B, GPIO.HIGH)



def MoveRight():

   GPIO.digitalWrite(IC1A, GPIO.LOW)

   GPIO.digitalWrite(IC1B, GPIO.HIGH)

   GPIO.digitalWrite(IC2A, GPIO.HIGH)

   GPIO.digitalWrite(IC2B, GPIO.LOW)



def MoveForward():

   GPIO.digitalWrite(IC1A, GPIO.HIGH)

   GPIO.digitalWrite(IC1B, GPIO.LOW)

   GPIO.digitalWrite(IC2A, GPIO.HIGH)

   GPIO.digitalWrite(IC2B, GPIO.LOW)



def MoveBackward():

   GPIO.digitalWrite(IC1A, GPIO.LOW)

   GPIO.digitalWrite(IC1B, GPIO.HIGH)

   GPIO.digitalWrite(IC2A, GPIO.LOW)

   GPIO.digitalWrite(IC2B, GPIO.HIGH)



def Stop():

   GPIO.digitalWrite(IC1A, GPIO.LOW)

   GPIO.digitalWrite(IC1B, GPIO.LOW)

   GPIO.digitalWrite(IC2A, GPIO.LOW)

   GPIO.digitalWrite(IC2B, GPIO.LOW)


nano - GPIO init


We need to create web page which will provide interface with direction buttons and camera output. The file will be a content of webiopi and will be named, obviously index.html.:


sudo nano index.html


Copy following lines and replace YOUR_IP with your IP address set previously (this is standard HTML/CSS and JavaScript page what allow easily modify and recreate it):




    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />


    <script type="text/javascript" src="/webiopi.js"></script>

    <script type="text/javascript">

    webiopi().ready(function() {

        var button;


        // Command buttons

        button = webiopi().createMacroButton("macro", "<", "MoveLeft");

        $("#middle").append(button); // middle button


        button = webiopi().createMacroButton("macro", "X", "Stop");

        $("#middle").append(button); // middle button


        button = webiopi().createMacroButton("macro", ">", "MoveRight");

        $("#middle").append(button); // middle button


        button = webiopi().createMacroButton("macro", "/\\", "MoveForward");

        $("#up").append(button); //top button


        button = webiopi().createMacroButton("macro", "\\/", "MoveBackward");

        $("#down").append(button); // bottom button






    <style type="text/css">

        button {

            margin: 5px 5px 5px 5px;

            width: 45px;

            height: 45px;

            font-size: 24pt;

            font-weight: bold;

            color: black;



        .LOW {

            background-color: White;



        .HIGH {

            background-color: Red;







    <div id="content" align="center">


        <img src="http://YOUR_IP:8080/?action=stream" width="480" align="center">



        <div id="up"></div>

        <div id="middle"></div>

        <div id="down"></div>






nano - web page edit


We have all parts set and ready so we can reboot our Pi:

sudo reboot

 If it runs OK we will see red LED on camera glowing indicating use of camera. Open browser on your PC and enter IP of robot. When log in window pops up enter following login name: webiopi and password: raspberry (these can be changed and it is highly recommended, in webiopi config file). If everything works fine you should see view from camera and 5 buttons underneeth. Now your Pi is ready to be connected to H-bridge on your chassis with power supply and motors.Good luck.


WebIOPi login page        WebIOPi User interface


And this is my one:

Image of Robot


I hope this guide was clear and understandable and you learnd something new. I am also ready to answer all questions or provide support with your own projects.