CW>

Projects / Lego Rover

Electronics

The two Lego motors are driven by an L293D motor driver IC (Integrated Circuit). This allows the motors to be powered by the 9V battery pack, but controlled by the 5V outputs from the Raspberry Pi. There are four outputs from the Pi to the motor driver, these control forward and reverse motion for each of the two motors. The rover is controlled by a Raspberry Pi Zero which is powered by the same battery pack as the motors via an L7805CV 5V power regulator. All the electronics components for the battery power, 5V power regulator, LED, motor driver, motor outputs and Raspberry Pi are connected on the breadboard by coloured jumper wires. I will explain below how to connect everything up to get it working. Although there are lots of wires on the finished breadboard, it doesn't look so bad when you see them added step by step. The labelled photo below shows the electronic components of the rover.

Power

The power for the rover is supplied from the 9V battery pack that sits underneath the breadboard. The wires from the battery pack go into a 2 pin terminal block on the breadboard. The 9V power for the lights is also connected from this same terminal block. This is shown in the bottom left of the following labelled photo, which is actually at the back of the rover on the right hand side. The 9V power is connected on the breadboard to the input on the 5V power regulator (top left in photo) and to the 9V power pin on the motor driver (center). The 5V power regulator has three legs which are pushed into the breadboard. It also has a small round indent on the front left side of it. The leg nearest to this is the 9V input. The middle leg is connected back to the ground of the power supply. The remaining right hand leg is the 5V output. The 5V output and ground are connected to the power lines down the side of the breadboard which are used to connect the other 5V components and Raspberry Pi. The power regulator chip gets a bit hot after it's been on for a while, so be careful!

Power connections and 5V regulator
Back of the 5V power regulator

Motor Driver

The L293D motor driver chip has 16 legs, 8 on each side, and a little round indent at one end to show which way round it goes (labelled with white arrow in photos). Breadboards are layed out so that you can put chips like this in the middle and the legs are connected to the numbered rows on either side of the board. Each leg is then connected up to the other components in the circuit by wires on the board. It's really important to connect everything up correctly here, because the 9V power supply could damage the 5V controller and Raspberry Pi The numbered photos below show the connections being added on the breadboard step by step. The instructions below the photos, explain each step in more detail.

1. Motor driver 9V power and ground connections
2. Motor driver outputs connect to motors
3. Motor driver 5V connection to enable motors
4. Pi outputs connect to motor driver control inputs
  1. The 9V power is connected to the corner leg, directly from the battery terminal, as shown in photo 1. This is the trickiest bit. Note carefully the position of the indent on the chip, shown with a white arrow in the photo. It's important this is connected to the correct leg, as all the other corner legs are for connecting 5V only. The middle two legs on either side are ground connections. These should all be connected back to the ground connection on the battery terminal as shown. I've used red wires for power and black for ground throughout.
  2. The two legs either side of the middle two ground legs are for connecting the 9V output to the motors. These will be turned on and off by the motor controller to drive the motors forwards or backward. Each side of the chip controls a different motor. So those two legs should be connected to one of the motors terminals and the two on the other side to the other motor terminal. I've used green wires for one motor and yellow for the other. I also connected a 10nF capacitor (labelled 103) across each of the motor terminals to reduce electrical "noise" from the motors and prevent it interfering with the controller.
  3. 5V power is connected to the three remaining corner legs. This is to power the control logic and to enable each of the motors. Note these connections are to the 5V power rail down the side of the breadboard, which comes from the power regulator 5V output. They must NOT be connected to the 9V power directly.
  4. The four remaining unconnected legs are for the 5V control inputs from the Raspberry Pi. These should be the 2nd leg from each end, on each side of the chip. These will tell the motor controller when to enable the motor outputs and which way to drive them. As they are going to be connected to the Raspberry Pi, it's important to get it right to avoid damaging it. I've used 4 different coloured jumper cables (purple, blue, green and yellow). These are then connected to GPIO pins on the Raspberry Pi. See section below on testing the motors before connecting up the GPIO wires. I've used pins 17, 27, 22 and 10, as they are all close together on one side, but it doesn't matter as long as they are input/output pins. See robot arm project for more details on GPIO pins. You need to make note of the GPIO pin numbers used for programming the rover later.

Lego Wires

The Lego power functions motors and lights each have a 4 wire cable. This is so that they can be switched on and off and reversed by the power functions switches. As the motors are going to be switched by the motor controller here, I don't need all 4 wires. And so I didn't have to cut the Lego connectors off the motors and lights, I bought some extra power functions extension cables and cut those up instead (except for one of the motors where the connector was already damaged). I have cut the square Lego power connectors off the ends of the cables, taped up the unused outer two wires with electrical tape, and stripped the inner two wires for connecting to the terminal blocks on the breadboard.

Raspberry Pi power cable

For connecting the 5V power supply to the Raspberry Pi, I have used a cut down old micro USB power cable (not a data cable). I read that it is safer to use the USB power input on the Raspberry Pi, rather than connecting power directly to the GPIO pins, because it has additional protection built into it's circuit. It also makes it easy to connect the power without any danger of getting it wrong and breaking something. I cut down the cable to the required length and there are two little wires inside (there would be more in a USB data cable). I think mine were white for power and black for ground, but this may vary on other cables. I connected these to some red and black wires for connecting to the terminal block on the breadboard and taped it up. I tested my power cable on an old bluetooth speaker, as well as checking it with a multimeter, before trying it with the Raspberry Pi. The terminal block is connected to the 5V and ground power lines down the side of the breadboard, so it is powered via the 5V power regulator. This is important as 9V would damage the Raspberry Pi. See section below on testing the motors before connecting up the power to the Raspberry Pi.

Power indicator LED

I added an LED on the breadboard to check that the 5V power was connected up reliably before connecting the power to the Raspberry Pi. The LED is just connected to the 5V power down the side of the breadboard with a 330ohm resistor. When the battery pack is connected to the breadboard (before connecting the Raspberry Pi power), this LED should light up. If it doesn't light up check it is the right way round and everything is connected properly. If it's super bright and gets hot, disconnect power and check you have connected the power from the 5V regulator, not directly from the 9V batteries.

Testing the motors

Before connecting up the Raspberry Pi power and GPIO pins (step 4 above), I tested the motors and controller were connected up correctly and weren't going to damage the Raspberry Pi. Instead of connecting the 4 coloured jumper wires for the motor control inputs in step 4, carefully connect a wire between the 5V power (along the side of the breadboard) and one of the motor control inputs (2nd leg from either end, both sides of the chip). This should cause one of the motors to turn, so make sure to lift the rover up first, or take the tracks and wheels off, before it runs away! Check each of the control inputs, one by one (not all at once), by connecting to the 5V power (not 9V). You should see that each one causes one of the motors to run forward or backward. The program on the Raspberry Pi will turn these on and off to control the motors. If the motors don't work, go back through and check everything is connected properly and the power LED it on. Once the motors are working, connect up the GPIO wires to the Raspberry Pi (step 4). Then connect the USB power and check that you can connect to the Raspberry Pi via SSH after it has booted up. You can then move on to programming the rover.