Dog Bot: Lego Robot Rover With Webcam




Introduction: Dog Bot: Lego Robot Rover With Webcam

Here's how to make a Lego robot you can control from any screen over wifi. It also has a webcam so you can see where your going and LED lights for eyes ! This is a great project to do with your kids as they can do the logo building and you can involve them in as much of the electronics as they are happy with.
There is lots of flexibility in how you implement this project, and this means you can easily take it to pieces and redesign again. I found it useful, when I got stuck to simply start again. Once you have done this a few times, its pretty easy and quick to rebuild from the ground up. The great thing about this is that you don’t need to be worried about "breaking" anything as you'll develop the skills to easily rebuild.

What you need

  • Raspberry Pi B+
  • Base and motor kit
  • Lego base plate
  • Industrial strength super glue
  • Drill
  • Craft knife
  • Soldering equipment
  • SD card
  • SD card reader
  • Motor controller kit
  • 2 LEDs
  • mid priced Webcam (you could try a cheap one)
  • PC and a tablet / phone
  • Wifi Router which you have admin access to
  • Lots of bits of Lego
  • Emergency phone charger
  • 4 jump wires each with at least 1 female terminal
  • 4 AA batteries (rechargeable is good)

Step 1: Prepare the Base

I started with a base and 2 motors like this motor set at Amazon

Take the perspex base and glue a thin Lego base plate onto it. I used industrial strength super glue for this which dries in minutes and is super strong. When the glue is fixed, cut away the excess Lego base plate so it follows the line of the perspex base plate. then drill holes through the Lego plate that match the perspex base. I drilled the holes as and when I needed them. You now have the foundation for building you Lego structure.

Step 2: Assemble the Wheels

Now assemble the wheels. Follow the manufacturers instructions here. The wheels I bought had all the instructions in Chinese, which I cant read, but it was pretty easy to figure out from the pictures. Once assembled attach the wires to the motors with a small blob of solder.

Step 3: Attach Wheels to Base

Next attach the wheels onto the base plate, you can drill holes in the base if you need. You'll probably find out towards the end of the project that you want the wires led through the base somewhere else, but you can always drill a new hole and relocate later.

Step 4: Prepare the Pi

Now get the PI ready. I used a model B+ with a USB wifi dongle. I used the Full desktop image based on Debian Jessie which you can download from the Raspbian Pi site. Its worth creating the SD card from scratch, and keep the disk image handy as you can easily start again. You might need to do a bit of googling to figure out how to create the disk image on your PC. I created my image using Ubuntu like this: Right click on the disk image and select "open with disk image writer". Write the image to the SD card. The disk image is quite small, so if you have a larger SD card (eg: 30GB) you can use partitioning software like GParted to use up all the space available. Once you’ve worked out how to do this it takes about 5 minutes, and is really worth it as if you mess up the software you can just reinstall again.

Step 5: Mount the Controller Board

Next you need a motor controller board. I used the kit from RyanTek . It comes with an excellent soldering guide, but I think you can also get it pre soldered. If you haven’t done any soldering before, don’t worry, just take it slowly and practice a bit before you start on the motor board.

Now, attach the battery pack (with 4 AA batteries) and the motors up to controller board then attach the controller board onto the GPIO pins of the Pi. Again the controller board will give you more details. Don’t worry about getting the right motor wires into the right slots, so long as they are not connected up to the DC power slots. I don’t glue or screw the Pi or the Battery pack onto the Lego board as this gives you more flexibility when designing the Lego body. You can also change your design easily as you go along.

Step 6: Start Building the Lego Body

Now its time to build some Lego. At this point you can get really young kids involved, so long as they can hold a piece of Lego ! We started with just a basic square housing till everything was working then got more adventurous with the dog bot. The great thing here is that you can rebuild the robot body as much as you like.
Start by creating a home for the Pi. Remember to leave room for the USB ports and the power cables. Build a fair amount of looseness into any housing you build. Its OK for things to rattle around a bit and you cant be too accurate with Lego.

We used Lego windows to provide access to the USB ports which you'll need later.

Step 7: Prepare the Pi

Its a good time now to start up the Pi, and get the the robot moving.

I've been developing the code to use with this project at Git hub.The Read me on the git hub pages take you through the instructions on how to install all the software on the Pi, but I'll also take you through the steps here.

First start up the Pi, by plugging in the power cable. You'll need to connect the HDMI cable to a TV and use a USB keyboard and mouse. First thing is to connect up to your Wifi. you do this by clicking on the icon on the top right of the Raspian desktop and proving the details. Once connected to the Wifi you can do everything else from the command line, so if you prefer you can connect up to the Pi using ssh, but for now, as we are connected to a TV we can open a terminal from the Raspian desktop.

Step 8: Install the Software on the Pi

Open up the terminal on the Pi and type the following commands.

  • sudo apt-get update

This updates Raspian with latest software.

Next, install the webcam software

  • sudo apt-get install motion

You also need to configure motion at this point. I've listed what needs to be updated in which files on the github pages. There is also an instructable about getting a webcam working on your Pi. Get to know the settings and play around with them once you are comfortable with the setup.

  • sudo apt-get install python-dev python-pip

This installs Python libraries and also the python software packaging manager.

  • sudo pip install 'pubnub>=4.1.2'

This installs Pubnub, the software that manages the messaging between the control panel and your wireless robot.

It takes a while to install this part so go and make a cup of tea !!

Step 9: Install the Robot Software

Now you have installed all the dependent software bits and pieces, its time to install the code required to run the robot.

this will create a lego-robot folder in your home directory.

Before you can run the robot code, you need to create a PubNub account and get a publish and subscribe key. you can do this by following the getting started now instructions at PubNub. Once you're set up you need to open up the two files:

  • lego-robot/server/
  • lego-robot/client/keys.js

and replace the pub and sub keys with your keys that you can get from the pubnub dashboard.

Note that the channel value can be anything, so long as it matches on client and server.

Step 10: Install the Controller Software and Check It All Works

On a PC, clone the git hub code in the same way as we did on the robot.

You might not have git installed on your PC. If not please follow the instructions at git hub.

You also have the option of downloading the code as a zip file, although I would advise installing GIT and copying the files by cloning the repository.

you'll need to up date the lego-robot/client/keys.js file with your personal publish and subscribe keys.

When you are done, open up the lego-robot/client/buttons.html file in a web browser. You should now be able to control your robot using the control buttons.

The diagram above shows what’s happening:

  • When you click on a button a message is sent to pubnub (using your publish key)
  • PubNub then forwards to the robot who...
  • Is listening (using the subscribe key) for messages
  • The robot then translates to an action.

I used pubNub as its free, quick (realtime) and greatly simplifies the sending and receiving of messages using a really nice API.

You can dig into the client and server code and see the messages being sent using simple keys like "forwards" and "backwards".

There is also a joystick style controller at lego-robot/client/joystick.html.

I'll be improving these controllers, and adding new ones all the
time, so if you ever want to update software, you can use the command "git pull". Please also feel free to contribute to the git repository and create more user friendly controls.

Now you are all ready to check the movement works OK.
On the Pi terminal, change directory into lego-robot/server and type:

  • sudo python

this starts up a python script to listen out for incoming messages from Pubnub.

Now try pressing some buttons on the controller, you should see these responses received on the Pi Terminal (as shown in the picture above) and the translates these messages to movements by mapping them to signals on the Pi GPIO.

You should be able to move the robot around.

Step 11: Prepare the Webcam and LED Lights

You can use any webcam that's linux compatible (most of them are). I would go for a mid range one (about £15) as I tried a really cheap one and couldnt get any kind of a clear image from it. First step is to disassemble the webcam so you just have the basic board and camera.

If you want to install lights, then take an LED light and connect two jump wires to each terminal. I used jump wires that have a female terminal at one end so you can easily plug it into the Pis GPIO pins later. The other end of the wire you need to strip, to remove the terminal. You can solder here as well to make a secure connection. I also used some heat shrinking wrap, which you can use to cover up each wire, although I wouldn’t worry about making too good a job of it till you are happy that everything’s working, then you can finish up and improve. Its also a good idea to use a resistor to stop your LED from burning put.

There is a detailed instructable here detailing how to wire LEDs.

As the picture shows, I use a Lego block with a hole in it as an easy way to mount the LED onto the robots head.

Step 12: Create the Head With Nose-cam and Lights

Create a head housing for the camera. I wont be too specific here as its time for you to be creative. However you do need to build around the camera . Dog bot integrates the camera into its nose and has the eyes as LEDs. Make sure you leave a hole at the back of the head to lead the wires out.

Step 13: Complete the Body

Now you are happy that the dog bot is basically moving you can create a housing for the battery packs. I used a pack of 4 AA batteries (provided with the wheels and base) and a emergency phone charger pack. The AA batteries are for the DC motors for the wheels and the mobile phone pack is for the Pi. Its a good idea to make the batteries easily accessible as you'll have to access them for recharging and replacing.

Once your happy, put a roof over the Pi. I don’t worry too much about hiding the wires and circuitry, as that’s part of the robots image. However its all up to you at this point. You might want to consider how heavy your bot is getting, the heavier the slower it moves.

Step 14: Mount the Head

Its time to get everything connected.

Connect the Webcams USB lead to the front of the bot, using the access holes you created. You can secure wire to the robot by creating some Lego housing. I also shortened the USB cord by cutting and re-soldering it, but only do this if you really have an urge to solder 4 tiny wires together. Might be easier to buy a short USB lead and reconnect it to the back of the camera.

You also need to connect up the LED lights to the Pis GPIO pins not taken up by the motor board controller. These are GPIO pins 20 and 21 and the two spare ground pins nearby. Use the spare ground pins nearby. If the light doesnt work then try switching round ground with the GPIO output. TO work out which pins to connect to google a good GPIO diagram.

Step 15: Test the LEDs and the Nose Cam

To start the webcam software type:

  • sudo motion

Also make sure you have started the script (as detailed in previuos instruction):

  • sudo python

You should now be able to use the lights switch on the controller to turn the lights on and off.

The web cam should broadcast on your Pis IP address on port 8080, so for example type the following into your web browser.


I found the Pi IP address by logging onto my router as an admin and looking at the attached devices. Instructions on how to do this are usually on the back of the router, or refer to your routers instructions online.

you can explore the configuration settings of the motion software, as described in the previous step. I find that sometimes the streaming pauses, but generally it works OK.

You can access this video stream from anything running a browser connected to you Wifi network.

Step 16: Finishing Up

Online access

You should now be able to control the robot from a browser on a PC. However its nice to be able to control the bot from any touch device like Ipad or phone.

To do this you need to host the client files (buttons.html etc) on a webserver.

I started by installing a webserver on the Pi, which works OK. However its easier to just upload the HTML file to any other webserver. I simply copied the HTML file to my public blue host account. You might want to file protect access to these files as they contain the pub sub keys to your pub nub channel. Once you do this you can access the controller from any device, even from outside your house.

Auto startup

Another useful thing to do is to up date the \etc\rc.local script on your raspberry Pi; adding these two lines:

  • sudo motion

  • nohup sudo python /home/pi/lego-robot/server/ &

These will run each time the Pi starts up, so you dont have to run the command manually.

Step 17: Have Fun !

Thats all.

Please feel free to leave comments if you get stuck and if you want to improve any of the software please contribute to the github repository. I'll be continuing to work on this project and here's some ideas:

  • A "big trak" style controller where you program a sequence of commands like "left 10, forward 6, back 2..." and then hit "run" and the robot carries out the commands.
  • A tilting neck, so you can look upwards with the webcam
  • A speaker that translates text from the controller to speech
  • Voice controlled control



Raspberry Pi Contest 2016

Participated in the
Raspberry Pi Contest 2016

Be the First to Share


    • Fix It Speed Challenge

      Fix It Speed Challenge
    • Jewelry Challenge

      Jewelry Challenge
    • Anything Goes Contest 2021

      Anything Goes Contest 2021



    2 years ago

    Hi Pete,
    Ok, I'm looking at the stepper code here, and wondering if I'm misunderstanding the hardware required. I have the Pi connected via pins 17,18,22 and 23 to an L298N motor controller, which deals with the basic movement. uses pins 10,9,11 and 25 for the stepper code, but I don't know where they're supposed to connect to. The L298N only has 4 pins available, which of course are already in use.

    It looks from the photos as though your motor controller has a lot more pins than mine :)


    Question 3 years ago

    Hi again, having more problems, and I'm just posting in case there's a quick and obvious thing I should be trying!

    I've not succeeded in getting server and client to communicate at all so far. I start up the server on the Pi and get the 'connected' line as in your screenshot above, but nothing at all when I open buttons.html on the laptop, and click buttons. As a Javascript ignoramus (and not much better in Python..), is there some basic troubleshooting I could try? I have my pub and sub keys entered correctly (as far as I can see) in keys.js and


    Reply 3 years ago

    you could opening up the javascript console in your webbrowser (F12). are there any errors from the client JavaScript?
    Also, there are two channels (blue and green) make sure you have the correct one selected in the buttons.html page.
    you could also take a look in pubnub and see if the server has connected and if any messages have been received from the client.
    I think its likely to be something simple like the wrong keys on server or client.
    Let me know how you get on....


    Reply 3 years ago

    Thanks very much for responding, especially at this point in December :)

    Ok, the client JavaScript all seems fine - no errors.
    I've tried both channels on the web page.
    On the Pubnub portal, messages are coming through. I've turned on realtime analytics and I can see the 'publish' message as I start the server, and the message count going up as I click buttons on the client.

    It's showing 'top channels' as dog-bot and my_channel, and I'm wondering if this is anything to do with channel names. On server/, I've left the line CHANNEL="XXXX" unchanged - is that right?

    And one more thing I should have asked earlier: is the script going to require changes to work with a different motor controller board?

    Thanks again...


    Reply 3 years ago

    Hi, sounds like you are almost there... here is a copy of my setup:
    CHANNEL="other-bot" #name of the app in pubnub

    * client PUBNUB keys
    var green = PUBNUB.init({
    publish_key: 'pub-c-a20...',
    subscribe_key: 'sub-c-bb0...'
    var blue = PUBNUB.init({
    publish_key: 'pub-c-501...',
    subscribe_key: 'sub-c-26e...'

    So the channel is actually used and determines the pubnub app you sign up to.
    Then your pubnub app (in my case called other-bot) should have keys that match the values in client keys.
    the is very experimental. I got it working with the rotation buttons on buttons.html, but the movement was not fluid. it works in small increments when you press the button. something I was actually planning to work on over christamas !!
    Sounds like you are almost there and it is just a case of getting the config right...
    let me know how you get on....


    Reply 3 years ago

    So... I have a Pubnub app called RobotProj, containing 3 keysets: demo, robot and robot2.

    server/ contains the pub and sub keys from robot, and CHANNEL="RobotProj".

    In client/keys.js, var green has the keys from robot2, and var blue has the keys from robot.

    But the server still doesn't appear to be receiving messages.

    One last question: about this function in

    def connect(message):
    print pubnub.publish(channel='my_channel', message='Hello from the PubNub Python SDK')

    Why is it 'my_channel', rather than green or blue, and why does the 'Hello' message never appear on the server console?

    Anyway, 11pm on Christmas Eve isn't really the time for this :), and I'm certainly not expecting a reply anytime soon, but when you do have a moment, any other suggestions gratefully received!
    thanks once again, and Merry Christmas!


    Reply 3 years ago

    Hi, I think I know what wrong... in the client side code, there is a file called "messenger.js", its got hard coded values for channels, you need to update like this:

    channel: 'RobotProj',
    message: {"move":direction}

    and in "keys.js" the blue channel should have the correct pub-sub keys that you are subscribed to on the server (
    your question about the


    that is wrong but on the server we never publish info (push messages) only subscribe so this line is redundant.

    Thanks for taking the time on this project, I'm going to tidy up the code based on the problems you've been having... and also update to latest versions of SDK.
    let me know how you get on. would be great to see some images of the robot once its working !!


    Reply 3 years ago

    Ha! Thanks very much; the change to messenger.js got the server receiving messages.

    No actual movement so far, but I'm pretty sure that's a matter of more playing about with pins. I'm assuming it's just GPIO pins 17,18, 22 and 23 for the basic controls, then 20 and 21 for the LEDs?


    Reply 3 years ago

    Hi, I just updated the code in github to work with latest version of pubnub. I also tidied up the code and added comments and updated instructions. I was wrong about the channel, it doesnt have to match the pubnub app... it can actually be anything so long as it matches in client and server.
    so... with new version of code all you need to do is update keys.js and with pubnub keys and it should work. I tagged the old code as v1 so you can always get back to that.
    Movement, yes 17,18, 22 and 23 is the mapping of pins for the RyanTek motor controller, but it will vary on the controller head you are using... you could start testing with LED lights as they are the simplest.
    Thanks again for working with this, it prompted me to do some well over due updates... I'm working on getting the project working with caterpillar tracks over christmas !!!
    Let me know if you have any other questions...


    Reply 3 years ago

    Many thanks Pete. Slight hiatus here as we wait for arrival of a board to power Pi with a small lithium battery rather than the hulking phone charger we're currently using.
    I've found another SD card, so will do a completely separate install of all the new stuff and let you know how it goes. The 6-year-old is proving a dab hand with the jumper wires...


    Reply 3 years ago

    Hi again, and a happy new year!
    Just a quick update: new battery
    installed, cheap Logitech webcam disembowelled and wired in, all working
    fine. LEDs and lego next, then some tidying up of wiring before we
    contemplate getting ambitious and adding some sensors...

    One small issue: /etc/rc.local is definitely running, because it kicks off motion, but for some reason isn't starting As you suggest, the end of rc.local looks like this:

    #Robot listens to commands
    sudo python /home/pi/lego-robot/server/ &

    #Robot starts webcam
    sudo motion

    exit 0

    If I run it manually, this is the output

    pi@raspberrypi:~/lego-robot/server $ sudo /etc/rc.local
    My IP address is
    [0:motion] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf
    [0:motion] [NTC] [ALL] motion_startup: Motion 4.0 Started
    [0:motion] [NTC] [ALL] motion_startup: Logging to file (/var/log/motion/motion.log)

    So no errors, but nothing about either. Any ideas?


    Reply 3 years ago

    Ha - success! Sorry, should have thought of that...


    Reply 2 years ago

    Spoke too soon there: it ran manually but still didn't at startup. Finally solved by adding a 'sleep 10' command to the start of /etc/rc.local.


    Reply 3 years ago

    Sorry, me again... Now that I've added a connection to ground, we have movement!!

    No luck with clockwise/anticlockwise. Looking at the call to stepper.motor in, I guess it requires connections from pins 10,9,11 and 25, but I don't know if my controller board has the necessary pins to connect to...


    Reply 3 years ago

    great news you have movement!!!
    you can use the stepper directly connected to the Pi, test it our, see if it works. the controller board only takes up 4 pins, and on mine the rest are mapped onto its own pins which you can connect to. I found a map of the controller pins showing which map onto the base Pi pins.


    Question 3 years ago

    Hi Pete, there seem to have been a lot of changes to the PubNub software since you wrote this. I've made some alterations to to deal with them, but am getting totally out of my depth :) and still getting errors. Would it work to revert to versions of Python and PubNub from the time when you published the project, or is that opening more cans of worms..?


    Answer 3 years ago

    Hi, thanks for using the project. What sort of errors are you getting? Although the project works on old versions it would be better to upgrade and resolve issues. Why don’t you fork the github project and share details with me and I could help out ?


    Reply 3 years ago

    (sorry, I've only just seen your reply - thought I'd get a notification from Instructables.)
    I was getting a lot of Python errors from changes in Pubnub syntax, I think. I reverted to PubNub 3.9, which seems to have sorted them out.
    I'm now stuck on puzzling out pin assignments on an L298N motor controller, but hoping trial and error may get me there! (any hints gratefully received though...)
    I'd love to try to resolve the issues properly, but I think that'll have to be after the 25th - this is supposed to be a Christmas present and time is getting tight :)

    many thanks for the reply, and for putting the project up,


    5 years ago

    Good work! Congrats!!!