How to Make a Raspberry Pi Lego Robot: Part 3

Welcome to the final part of a 3-part series on building a Lego robot which can be controlled by a Raspberry Pi. Previously in part 1, I showed the how to wire the breadboard, including the L293D microchip pins and the Raspberry Pi's GPIO pins.

On other pages:

Part 3: The Software

As with the majority of Raspberry Pi projects, I'm going to use Python, especially as there seem to be a big push to use Python in schools.

Connecting remotely

It's difficult to plug in a monitor, mouse and keyboard if your Raspberry Pi is moving around on top of a robot. So, you will need another computer to connect from. The question is, how will you find your robot on the network? Computers on networks are assigned numbers called "internet protocol addresses" (IP addresses). By default, you might get a different number each time you switch on (DHCP). The solution is that you need to give your robot a static IP address - static meaning "always the same". This is done from two files: /etc/network/interfaces and /etc/wpa_supplicant/wpa_supplicant.conf

Start by plugging in your Raspberry Pi to a monitor, mouse and keyboard, logging in and going to the command line:

sudo nano /etc/network/interfaces

Now edit this file so it looks something like:

auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
iface homewifi inet static

You will need to change the netmask, gateway and dns-nameservers to match your home network. See your broadband router's documentation for help.

address will need to be changed to something that isn't currently used on your network. I suggest one more or one less than your gateway - but don't use 0 or 255. We will refer to this as your "static IP address".

Save and exit nano. Next, edit the wireless configuration file:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Now edit this file so it looks something like:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
  pairwise=CCMP TKIP
  group=CCMP TKIP

You will need to change the ssid and psk to match your wifi network. The above example assumes you have WPA2 WiFi security; you'll need to do some research if your setup differs.

Reboot, log back in, go to the terminal and type ifconfig and you should see that your Raspberry Pi has been assigned the static IP address. You can now find it on the network, even if it has no monitor attached. Test you can see the internet by doing:

ping -c 5

You should see 0% packet loss. If your packet loss is greater, go on to some Raspberry Pi forums and ask for help.

We'll use SSH to connect from another computer. Recent distributions of Raspbian should already have the SSH server already turned on; if not, the you will need to enable it from sudo raspi-config .

Now go to your other computer. If it's a Linux or MacOS computer, you should be able to go to the terminal and type:

ssh pi@

...where pi is your username and is the static IP address you assigned in /etc/network/interfaces . If you're using Microsoft Windows, you can download and install the PuTTY program (see the DOCS link for how to use it). The host name for PuTTY will be the static IP address, and the username will be pi unless you've changed it to something else.

Congratulations, you are now logged in remotely to your Raspberry Pi. You can do anything in SSH that you can do in the terminal.

Shutdown your Raspberry Pi with sudo shutdown -h now, remove the monitor, mouse and keyboard and set it back up on the robot. Plug in the batteries, turn on the battery box if it has a switch, wait 1-2 minutes whilst it boots back up again, and you should be able to connect remotely again.


My goal was to use the robot as an introduction to programming, to make my children enthusiastic about programming. I absolutely did not want to bore them to death with six lessons about GPIO ports, I wanted them to feel the instant gratification of getting a robot to obey their commands. So I wrote a module to do all the hard work. Sure, when my children are more proficient with IT, I'll explain it and get them to write programs which directly address the GPIO ports, but at the time of writing, my children are 4 and 7, so that's a big ask. You can download my module here:

However, we only have SSH access, so we can't use the web browser on the Raspberry Pi itself. How will we download the file? We'll use wget . Log in through SSH and do:

sudo apt-get install -y wget
wget ""

Don't worry if it tells you wget is already installed, that's fine.

If you're intending to hear the robot talk, you will also need the espeak program and a way to control the volume. Install these utilities and set the volume to maximum with:

sudo apt-get install -y espeak alsa-utils
amixer cset numid=3 100%

Don't worry if it tells you espeak or alsa-utils are already installed, that's fine.

Now use nano to create a new program in the same directory with: nano

import arthurbot
myArthurBot.say("Silly sausages")
myArthurBot.say("Thank-you for your photo")

Remove the say line and/or the takePhoto line if you don't have a speaker or a camera.

Run the program with sudo python and you're done!

Try changing the numbers in the program. The numbers represent the number of steps, or the amount of turn, that the robot will take.

Where did the photo files go?

Photos are stored in the current directory, and named arthurbot-YYYYMMDD-HHMM-SS.jpg where the capitals are the date and time.

If you want to copy them, from another Linux or MacOS computer you can do:

scp pi@*.jpg .

...where pi is your username and is the static IP address you assigned in /etc/network/interfaces . If you're using Microsoft Windows, you can use PuTTY to open an SCP file management window.

How do I stop it?

If you put in a really large number of steps, it might keep going for several minutes - hold down the CTRL key and press C and the program will stop - but the robot will probably carry on. You can either turn off or disconnect the batteries, or you could quickly write a program to stop it:

import arthurbot

I really need to extend to catch the CTRL-C exception and to fire off allStop() itself, but spare time ran out. Keep checking back here to see if I've updated it.

That's all, folks!

You were expecting more? A line-by-line explanation of ? An introduction to Python programming?

Nope. The point of this exercise was to provide a robot that could be used to learn Python programming. The above demonstrates all the commands that arthurbot supports, now go forth and use them in your own programs!

The code is extensively commented. Even if you're not familiar with Python, there's lots to read in there. You can change the GPIO pins, set a "dry run" mode that just prints to the console instead of moving the robot, set the directory where photos will be saved to, and lots more. Read the code and have a play.

There's a bug! It doesn't work!

Quite possibly. Although I did test my robot and programs, as did my daughter, I could easily have made a mistake writing up this document. However, telling me "it doesn't work" isn't very helpful. What I want to know is, what did you do to fix my mistakes? Email me with any fixes, and I'll correct this document.

That said, it would be rather mean not to have a troubleshooting section, so here we go.


On other pages:

Just to be absolutely clear: all the programs and listings on this page are my own work and I have granted them to the public domain. You can copy and re-use them in any way you like, without having to ask me, and without having to credit me (although thanks would be nice).

Public Domain - Andrew Oakley - 2013-09-19

Top - More Computing Articles - Article Index -