3.4. Motor control

Motor drivers

To be able to control motors we need specialized integrated circuits (motor drivers), which are capable to deliver enough current or voltage to spin the motor.

Motor drivers have been developed to supply motors with power and to isolate the other integrated circuits from electrical problems, like voltage spikes into the control lines when the speed or direction is changed.

Usually DC motors are controlled by an IC called H-bridge. It’s called that way because it looks like the capital letter H when viewed on a discrete schematic.


In the image above we can identify four switches:

  • HL, HR (high left/right)
  • LL, LR (low left/right)

To power the motor you turn on two switches that are diagonally opposed. Switches are turned on in pairs (HL with LR or HR with LL). If both switches on one side are turned on, a short circuit will be created between + and – terminals.

The current flow is illustrated with the green/red line.

Current flowing from from HL to LR
Current flowing from HR to LL

PWM in action

Refer back to the chapter on PWM - this is where it applies. In order to get a DC motor to move at a desired speed, we need to apply a PWM signal accordingly to the switches in the H-bridge. Suppose we send a PWM signal with a 50% duty cycle to the HL and LR switches. That will make the motor turn forward at a speed given by half its supply voltage. A PWM signal with a 25% duty cycle will result in a speed given by a quarter of the supply voltage, and so on. To turn the motor backward, we send a PWM signal to the other two switches, while leaving these ones open.

Now that we know how motors are controlled using hardware, let's see the software part. To drive the motors we will use the 3pi API (see the 3pi API 2.9):

set_motors(M1_speed, M2_speed);

Slow or fast?

An important question arises. Why go slow when you can go faster?

Let’s suppose that the track has a sharp right turn. If the robot is running at full speed, most certainly it will miss the turn and fall of the track. Of course, the solution isn’t to run very slow or very fast, but moderate. You won’t be able to break enough if the robot is driving too fast and detects too late a turn.

How can we go fast and reliable? Going fast and reliably usually implies other types of algorithms. We will discuss that type of algorithms in the Optimizations chapter.

Also, a robot which runs smoothly on a track doesn’t necessarily run that way on every track. So an important step in developing a line follower is testing the robot’s behavior on multiple tracks.

roboticsisfun/chapter3/ch3_4_motor_control.txt · Last modified: 2012/11/25 21:09 by liviu.radoi