Controlling motors using an Arduino and a motor controller board.

There are many uses for motor controllers connected to Arduinos, but perhaps the most exciting is to make your robot move. However, getting an Arduino and motor controller set up can be daunting if you're a beginner, so this page is intended to give a brief overview of how it's done using an L298 motor controller kit. There are many shields, ready made boards and kits available for use with Arduinos, or you could make your own circuit, but for this project an L298 motor controller kit was used with an Arduino Mega board. An advantage of the L298 is that it allows for a higher current than the more common L293 chip, which is useful especially for four wheel drive robots where two motors are driven on each side. The kit used, which is shown in Figure 1, was bought on eBay (seller: rkeducation) and proved quite straightforward to solder together. As you can see, the kit was installed in a 4 wheel drive robot: if you do the same the important thing is to check your wiring before you solder leads to the motors, as you don't want them turning in opposite directions. You may wonder why the tape and straps on the four motors? Well, when the robot is bouncing about the wires could easily move around and the motor solder tags could easily break if that were to happen (which would most likely mean the time consuming job of installing a replacement motor).

The motor controller installed in the robot chassis.

Figure 1. The motor controller board installed in the robot chassis.

One of the nice features of the controller kit was that it included a 5V voltage regulator, which is useful as the Arduino boards may not be able to supply power to the myriad of systems and sensors included in many robots. A schematic of how it was connected up to the Arduino Mega pins and motors is shown in Figure 2. Your motor board may differ, but if it's based on the L298 then the connections are most likely marked in a similar way. Slightly confusingly, the ENA and ENB pins are connected to Arduino PWM pins to control the motor speed, IP1 to IP4 being used to determine the motor direction (i.e. forward and reverse). As you'll see in Figure 1, care was taken to ensure that bits of bare wire were avoided when connecting control, power and motor wires. That's to avoid short-circuiting, which can have a very nasty effect on batteries, electronics, and even robot builders!

Schematic of the motor controller board and connections.

Figure 2. Schematic of the controller board and connections.

To make the motors work, the first thing we need to do is add the pin definititions to the top of the Arduino code, as shown below. Remember though, no semicolons at the end of #define lines. If you used different pin numbers for the motor controller connection, simply change the numbers at the end of each line to correspond with what you've done. Be careful though, as when using PWM (pulse-width modulation) there are combinations of pins specific to each Arduino that may not work properly.

// Motor controller pin definitions

#define MR_enable 3

#define ML_enable 4

#define MR_one 5

#define MR_two 6

#define ML_one 7

#define ML_two 8

Having done that, to get your robot moving you just need to apply a PWM signal to the EN pins and high/low voltages to the IP pins. For example, the code below shows how the robot was made to move continually forward at about 40% of full speed (change 100 to a value between 0 and 255 to vary the speed). Of course, before doing that it's best to be able to stop! To do that, just set all four IP pins to LOW. To move backwards, just swap the HIGHs and LOWs in the code below. If you want to do something more fun that going in a straight line, you could use different speeds for each motor. For a dramatic spin, you could even set one motor to go forwards, and the other backwards. Hopefully you'll have a lot of fun trying out all the possibilities!

// Run both motors at 100/255 of full speed