Using Legos to turn a Raspberry Pi into a mobile device

Lead Image © Daniel Villeneuve, 123RF.com

Let's Play

We show how to build a remote control for a toy car built out of Lego parts, a Rasp Pi, a game pad, and a Java program.

You could easily build a remote control for a toy car using LEGO® Mindstorms [1] parts, or you could even add remote control functionality using the infrared components in the Lego Power Functions (PF) series [2]. However, buying all these components won't teach you much, and the toy you get for your efforts becomes boring pretty quickly. If you like to tinker, you might want to build a remote-controlled Lego car yourself and save the expense of setting yourself up with a Mindstorms kit. Adding a Raspberry Pi to your Lego car gives you freedom and flexibility that Mindstorms could never match.

Of course, if you want your car to move around, you'll need more than just a Raspberry Pi. The project described in this article requires parts that can interface a Lego car to the control electronics. The motors from the Lego PF series are very well suited for this task. You can integrate PF series parts directly into your existing Lego set without a problem.

The 42006 "Excavator" Lego set is a good one to start with, and you can augment it with two PF motors (Figure 1). Constructing electrical drives in this set is very easy, and you will not need any other parts for gears and steering.

Figure 1: The starting point for this project is the 42006 Lego Set, together with two modified motors from the PF XL Lego series.

As you can see from Figure 1, I have replaced the original Lego connectors for the motors. I soldered the ends of the jumper cables in order to connect them to the plug board. The PF motors have four wires (Figure  2). The red wire and the blue wire are used to carry electrical current, and the two wires in between with the yellow cable ends control the car through the PF IR receiver [3]. When you use the Raspberry Pi to control the car, you only need the inner two wires.

Figure 2: The cable ends of the PF XL motor are adapted for connection to the plug board.

Motor Drivers

The ends of the jumper cable are male, because most of the time it is a not good idea to connect motors directly to the GPIO pins of the Rasp Pi. Instead of connecting the motors directly, the better approach is to use a so-called motor driver chip. A variety of chip models are available that all work pretty much the same way. Some of the chips come on their own board that also includes connectors. You will not need an additional plug board if you choose one of these chips. A good example is the motor driver chip L293E, which you can purchase inexpensively on eBay. Figure 3 shows two L293E chips that have already been connected to a plug board.

Figure 3: The L293E chips connected to the plug board are used to control the motors in a later step of the project.

One L293E chip controls up to two motors. Connecting the motors according to the chip specifications is important (Figure 4). In this example, you should connect the inner cables of the first motor to Output 1 and Output 2 and the cables of the second motor to Output 3 and Output 4.

Figure 4: The pin allocation for the L293E motor chip.

A notch on the upper side of the L293E helps with orientation. Next, connect the input pins of the chips to the appropriate GPIO pins of the Rasp Pi (Figure 5). For example, you could combine L293E Input pins 1 and 2 with GPIO pins 11 and 12 and L293E Input pins 3 and 4 with GPIO pins 15 and 16.

Figure 5: The GPIO pin layout for the Raspberry Pi.

To avoid damage to the Rasp Pi resulting from incorrect voltage, you can add diodes to the circuit directly behind the GPIO pins. These diodes make sure that electricity can only flow toward the chip and not in the other direction. You should also connect the power for the motors (Vs pin) and the voltage supply for the chip itself (Vss).

Officially, the PF motors have a 9-volt capacity. However, they can deliver more power when supplied with 12 volts, and typically they handle the three extra volts without any problem. Loading a battery box with 10, 1.2-volt AA batteries (Figure 6) to power the motors can deliver up to 12 volts. The voltage supply for the motor driver chip comes from the 5-volt pin on the Rasp Pi.

Figure 6: A battery box filled with 10, 1.2-volt AA batteries provides the power source for propulsion.

In addition to the pin for supplying power to the chip, you should also supply both of the enabling pins Chip Enable 1 and Chip Enable 2 with 5 volts. These two pins serve to activate and deactivate the accompanying output. You can also use them to control the speed of the car via a PWM signal (see the "PWM" box) As a last step, connect the ground (GND)  – as well as the Sense pins  – with the 0-volt pin of the Rasp Pi and the corresponding port on the Rasp Pi.

PWM

Pulse Width Modulation (PWM) makes it possible to modulate the width of electrical impulses according to a desired target value. For example, the target could be a predefined value for the revolutions per minute (RPM) of an electrical motor.

After putting the connections in place, the configuration should look like Figure 7. At this point, you can attach the control device you have created to the chassis. Additionally, you should add 5 volts of power for the Rasp Pi. The EasyAcc PB12000A is one of the 5-volt USB batteries on the list of batteries that have been tested on the Raspberry Pi for attached devices [4].

Figure 7: Connecting the Rasp Pi to the L293E control chip and the power supply.

Configuring the Rasp Pi

In this article, I assume you are using Raspbian on the SD card for the Rasp Pi [5]. I used a Model B Rev. 2 Rasp Pi board with Raspbian Version 3.6.11+.

To connect to WiFi, you will also need a USB WiFi adapter. The WL-167G V3 from Asus works well with the Rasp Pi [6], and you will only have to configure the connection. Open the /etc/network/interfaces configuration file and add one more entry. If this file does not yet exist, you will first need to create it. The following code shows an example of a configuration for when the system recognizes the WiFi adapter as a wlan0 device:

[...]
auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa.conf

The last line of this example configuration references the /etc/wpa.conf file that you will also have to create and edit. This file contains information that is important for a WiFi connection (Listing 1). In the example, DHCP is used to refer to a dynamic IP address, and WPA2 is used for encryption. You will need to adapt the configuration of your Rasp Pi to both of these configuration files.

Listing 1

/etc/wpa.conf

network={
  ssid="networkname"
  proto=RSN
  key_mgmt=WPA-PSK
  pairwise=CCMP TKIP
  group=CCMP TKIP
  psk="networkkey"
}

You also will need a Java Runtime Environment (JRE) on the Rasp Pi. Using your Internet connection, you can install the open JDK-JRE on a Debian-based distribution with the command:

$ sudo apt-get install openjdk-7-jre

Oracle's Java 7 JDK is preinstalled on recent Raspbian distributions. Practically speaking, it doesn't make a difference which of these environments you choose, as long as the choice you have made supports all of the functions of the application.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content