CW>

Projects / Getting Started

Getting Started

This section is intended as an introduction for those getting started building robots with Lego and a Raspberry Pi, so you can understand the other projects I've built and have a go at making something similar yourself. I'm writing it for my own two children to use and for anyone else who is interested in having a go.

Before you get started building robots and getting them moving and reacting to the world around them, there are some parts you will need to have or get hold of and a few things you will need to learn. In this page I will go through the things you will need, and then in the following pages I will explain some simple examples to get them working.

Lego Technic

My projects here are built with Lego, mostly Lego Technic parts. This is partly because we already had loads of it, but also because it's easy to build without any tools or glue, etc. Lego Technic is particularly good for building moving models with hinges, joints, rotating parts, gears and wheels. But you could just as well use some other building system like Mecano, or even build some models from household junk!

Once you've built some moving models, you can add some motors and lights with switches to turn them on and off. We got the Lego Power Functions set, and used it to build some simple moving vehicles, a crane with a winch, and things like that. These motors are good, because they are easy to connect to the Lego parts, but you don't need them to get started with the examples here.

The next stage is building some way to control how the models move, adding sensors to react to the environment and a way to remotely control the models. Lego make the Boost and Mindstorms kits that can do these things, but they are quite expensive and you don't really get to understand how everything works. So in these projects I decided to use a small Raspberry Pi computer and cheaply available electronics parts to have a go at doing something similar.

Raspberry Pi

In my projects I've used a Raspberry Pi Zero computer. The Pi Zero is the smallest and lowest cost version of the Raspberry Pi. It's not the best if you want to run a full graphical interface and use it as a desktop computer, but it's perfect for controlling small projects like this while still having most of the same features as a modern PC. You could definitely use any of the other Raspberry Pi models instead, if you already have one or want to use it for other things.

I got the Pi Zero WH from The Pi Hut, where they currently cost £12.80. I also got a case (£5), a 16GB SD card with the Operating System preinstalled (£7), and the essential kit (£6), which comes with the HDMI and USB cables for connecting a monitor, keyboard and mouse. You're going to need to get these things if you don't already have them. PiHut also sell kits with everything you need to get started.

The "W" version has built in WiFi and Bluetooth wireless connectivity, which is useful for connecting to it remotely without plugging in a monitor, keyboard and mouse, etc. I usually connect to the Pi Zero remotely from another computer, so I can program it without connecting anything other than the power cable. I got the "WH" version which also has pre-soldered on headers for the GPIO pins, otherwise you would have to solder these on yourself. The GPIO (General Purpose Input/Output) pins are how you will connect it up to things like motors, servos and sensors, to control your Lego models. I've written a short guide to getting started with Raspberry Pi, here.

Moving Parts

To get your Lego models moving, you will need to use motors or servos. Motors turn continuously, so they are good for driving wheels. But they need some extra electronic components to control them from the Raspberry Pi. I've used Lego Power Functions motors in my Rover project to drive it.

Servos move to a set position, so you can use them to move a part of a model a small distance, very precisely. I've used servos in my Robot Arm project to move the joints in the arm and in my Robocop project to turn the head and fire a Lego missile.

Small servos are a good place to start as they can be connected to the Raspberry Pi very easily, without any other electronic parts. In my projects I've used 9g micro servos, which are the little blue ones often used in model planes and cars. These can be bought on Amazon in packs of 5 or 6 for less than £10.

Electronic Components

If you are going to do more than connect a single servo and you want to learn more about electronics, you are going to need a few more parts. These are all things that you can buy cheaply from electronics and hobby shops like The Pi Hut, or from Amazon.

Breadboards are small plastic boards with lots of holes in (see picture), that are used to connect up electronic components without any soldering. They are not to be confused with the big wooden boards used for chopping bread on! You can buy a half-size breadboard like the one I use in my projects for £3. A breadboard has two lines of holes along each side, labelled + and -, for connecting power and numbered rows across the middle section for connecting components. If you've never used one, I've written a short guide to getting started with breadboard electronics.

Programming

I did the programming for these projects mostly in C++ as I was already familiar with the language, and I use other similar langages in my job. C++ is probably the most commonly used language for Linux based computers like the Raspberry Pi and it can be compiled to run on most other computers too. The other popular language for Raspberry Pi is Python, which I'm less familiar with, but is probably a bit easier to learn for someone starting programming. Maybe I will have a go at converting these projects to Python at some point in future. If you've done a bit of programming in other languages like Python or Basic, you can probably pick up enough C++ to understand the code for this project. And it's worth learning a bit of C++ for any programmer, as most modern languages (Java, Swift, C#, JavaScript, etc) are at least partly based on the C/C++ syntax. If you search the web for "learn C++", there are lots of free tutorials available.