# How to stop being controlled by your DC motor: reverse the roles! Part 2

· 6 min read

Part 1 of this topic was about direction control of your DC motor. Today it's time to have a look at speed control. Everyone curious about robotic motorization basics can read this post, but I suggest you begin with the guide to motors in robotics, as well as part 1 of this post first.

I also wrote a small post to summarize the different topics where I discuss motors.

All the words "motor" refer to "brushed DC motor" in the following.

This is it. You can choose the rotating direction whenever you want—what a sweet sensation. You feel like you almost have complete 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 present to see changes in 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 series variable resistor (R1) to adjust the voltage, i.e., the motor's speed.

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 the motor's load (we will see that later), and as voltage drops run with the current into the resistor, the speed control wouldn't be linear if the motor's load varies.

Is that control? No, it's not.

As for the direction changes, we want to control the speed, i.e., the voltage, through a microcontroller programmed with our 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.

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).
• One pulse of HIGH and one fraction of time of LOW is called the period (also in seconds).
• The high value of the pulse is called the amplitude (in Volt).
• Hertz's frequency (f) is the inverse of the period (T).

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

As for the time scale, a PWM frequency chosen for controlling a motor can vary (very) roughly between some Hertz and 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.

Continue to learn more about DC motors and many other subjects on our documentation! Several tutorials and videos are waiting for you.

### How is PWM 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 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 generate the PWM signal.

While writing a program for your microcontroller, you can choose the specs of your PWM: frequency and duty cycle. The voltage inside the microprocessor defines the amplitude. It's a HIGH value.

In 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 pins.

### How does PWM works on motors?

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

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 from flowing inside the transistor Q1.
• When the transistor opens, the motor is not powered quickly before the transistor closes again. It can be in generator mode during this time, 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 as 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 enables 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 finishing reading this post. That's a lot to process. See you tomorrow then. Sleep tight.

Hi. I know that you didn't sleep because you still don't know when the speed is actually controlled. Since the beginning of this post, we only replaced a simple enough flat voltage circuit with a too complex with Qs and Ds and Rs PWM voltage circuit. But still no speed control? Come on!

Right, this is how you control the speed:

## Speed control. Finally.

Viewed from the motor, it's another kind of PWM incoming at its terminals. The transistor closes and opens fast, alternatively letting the current flow and stopping it, so it receives steps of Vcc (which is the value of your battery). This is the power signal.

Let's go back to the command signal. As I said earlier, PWM is defined by its frequency and duty cycle. For example, this duty cycle is originally a time, in seconds, but it is mostly expressed as a percentage of the total period.

This percentage value of the duty cycle will define the speed of the motor: the higher the value, the faster the rotor.
The power signal will look like the command signal — same duty cycle — but with more amplitude (Vcc). Again, how does the motor see this? If we take several pulses, we can deduce an average value from this power signal. For example, a PWM signal with a 24 V amplitude and a 33 % duty cycle has a mean voltage of 8 V. Because 24 x 33% = 8.

Let's have a look at the power signal and the average voltage:

Regarding the evolution of the speed, it looks roughly like that:

If I summarize this whole PWM thing:

• You can create a PWM signal from a microcontroller (e.g., an Arduino board) that drives the speed of your motor in a small circuit.
• Its frequency and its duty cycle define this PWM signal.
• The PWM signal opens and closes a transistor, allowing a current to flow or not through the motor, thus creating a power signal that looks like the command signal but with more amplitude.
• The higher the duty cycle, the faster the motor's rotational speed.
• The motor reacts to that power signal like a mean voltage, calculated from its duty cycles.

Pros of using PWM signal for your motor:

• It's easy to use.
• Good efficiency (few losses of power).
• It is good for the motor's coils — better than flat voltage.
• Good robustness.

Cons:

• It can make noises (according to the human-audible frequency used).
• EMI noises can be generated.
• The frequency must be chosen wisely (this will be the subject of a future post).

That was a tough one. But look at you now. You can control both the direction and the speed of your DC motor. This is freakin' robotics!

And we haven't finished yet. This is only the beginning. Have you heard about torque? No? You will.

Thank you for reading.

— If you liked what you read, please clap the hell out of it and follow us on Reddit!

By the way, if you want to discover some exciting projects created with Luos and exchange with different people every day, don't hesitate to join our community: Join us on Discord

Get Started with Luos

Tags: