The part 1 of this topic was about direction control of your DC motor. Today it’s time to have a look to speed control. Everyone who’s curious about robotic motorization basics can read this post, but I suggest you to begin with the guide to motors in robotics, as well as the part 1 of this post first. There is also a small post I wrote to summarize the different topics where I discuss about motors. In the following, all the words “motor” refer to “brushed DC motor”.

This is it. You are able to choose the rotating direction of a motor whenever you want. What a sweet sensation. You feel like you almost have the full control… but you want more already.

Ok. Let’s see how you can control the speed of your motor.

Voltage

An electric motor works with electric power — i.e. voltage and current — provided at the input through wires. While inverting these wires results in changing the rotation direction, we also can adjust the voltage and the current to see changes at the motor’s output.

The voltage at the terminals, in particular, is proportional to the speed of the motor. That means the more voltage you provide at the input, the faster the motor will rotate at the output.

Note: If you try to increase the voltage at high ranges without checking its datasheet, you’ll likely burn it down. Each motor has a specific voltage — and current — it can bear according to its parts, and going below these values is not a good idea.

We will see later what the current does.

So here you are, you can now control the rotating speed of the motor. Of course you will have to change the voltage at the input. Manually.

We could use a serie variable resistor (R1), so that we can adjust the voltage, i.e. the motor’s speed.

This technically works, but…

But:

This is not automated — this is not robotics!

A resistor dissipates power when current flows through it, and generates heat. That means less power, and lower efficiency.

As current varies with motor’s load (we will see that later), and as voltage drops vary with current into the resistor, the speed control wouldn't be linear if the load of the motor varies.

Is that really control? No it’s not.

As for the changes of direction, we want to control the speed, i.e. the voltage, through a microcontroller programmed with our own code.

But first let’s dig into a new concept:

PWM — What is it?

PWM (Pulse Width Modulation) is a way of providing voltage, but instead of being a flat value many small high-low steps of voltage.

Flat voltage vs. PWM voltage

Applying to a motor, it’s like switching it ON and OFF a lot of times and very fast.

The ON phases are called pulses .

. The short time of one pulse of HIGH is called the duty cycle (in seconds — for now).

(in seconds — for now). One pulse of HIGH and one fraction of time of LOW is called the period (also in seconds).

(also in seconds). The high value of the pulse is called the amplitude (in Volt).

(in Volt). The frequency (f) in Hertz, is the inverse of the period (T).

Great. Mathematics now. What else next?

Let’s have a look at the illustration of a PWM signal:

As for the scale of time, a PWM frequency chosen for controlling a motor can vary (very) roughly between some Hertz to some kilo-Hertz. Applying the last formula, it means that for a PWM frequency of f = 10 kHz , we have a period of T = 1/10,000 = 0.0001 s . So in that example, every pulse lasts less than 0.1 milliseconds. That’s not much.

When generating a PWM signal, it will only be defined by its frequency, in Hertz and its duty cycle, in % of the period.

How PWM is created?

You can use various electronic components to make a circuit that provides a PWM signal, but you will find it easier with a microcontroller. The microcontroller carries a tiny internal clock that basically paces the system according to its frequency. It’s called a clock. How original. And still inside, they are some other clocks — this time they are called timers — which can be used for generating the PWM signal.

While writing a program for your microcontroller, you can choose the specs of your PWM: frequency and duty cycle. The amplitude is defined by the voltage inside the microproc, it’s an HIGH value.

At the end, the PWM will get out from an output pin of your microcontroller, and, if you are using a developing board from one of its output pin.

How does PWM works on motors?

As we don’t really choose the amplitude, let me remind you that PWM is not used for powering the motor. It’s a command signal, not a power signal. In order to use a PWM signal, you must send it through a transistor (the little door we talked about in the part 1) that will let the power signal pass:

Yes but no. Stop. That’s too much electronics.

So, what happens in this picture:

The power supply is Vcc (the positive wire of your battery).

Q1 is a transistor (a “door”).

When the PWM signal is down (LOW value), Q1 is opened. No current flows through the motor, it doesn’t turn.

When the PWM signal is up (HIGH value), Q1 is closed, the current can flow through the motor, from Vcc to the ground, making it turn.

The resistor R2 prevents too much current to flow inside the transistor Q1.

When the transistor opens, the motor is not powered during a short time before the transistor closes again. During this time, it can be in generator mode and the generated current could damage the transistor. That’s why D1 is here: the current flows into D1 instead of Q1. The diode D1 is called a free-wheeling diode.

Note: Saying that a transistor or a push-button is closed is the same than saying that a door is opened; Because an opened door lets the passage to whatever wants to go through, a closed transistor or push-button lets the passage to current

Of course the motor does not stop after each pulse, when the PWM signal is LOW. Everything happens so fast that the free rotor, pushed by its inertia, doesn’t have time to stop before it’s powered on again with the next pulse.

This is how you use PWM. But maybe you can sleep on it first before to finish reading this post, that’s a lot to process. See you tomorrow then. Sleep tight.