An introduction to PID control with DC motor

PART 1 — How to be a good autonomous and environment-aware robot

Simon BDY
Simon BDY
Feb 27, 2018 · 8 min read

In this post I will introduce to you the basics of PID control, and why it comes in handy for robotic applications. You don’t need much of background to read this, but knowing the basics of motors is always a good idea: my previous posts on this subject are listed here: Welcome to the jungle.

Now it’s time for serious business.

You already theoretically know how to change the spinning speed and direction of a DC brushed motor — if not, you just have to read a few posts (see above) to get quickly updated.

Yet, you may find out that some improvement is needed if you want to give your motors — your — some behaviors.

Robotic behaviors… what are they? Basically, it can be the fact of being autonomous: no remote control. Also, it’s how a robot react with its direct environment.

How to be autonomous and get along with environment?

Getting autonomous (for a robot, that is) basically involves you writing a kick-ass programming code that will cover all the single actions you want your robot to do, but also all the possibilities of bad turn of events that could happen in its life. This code could as well include some deep-learning functions so that your robot has its own evolving intelligence. The goal being that it can act accordingly with what it is supposed to do, along with its external environment.

If I summarized, you need to give a purpose to your robot, and put a brain inside it so that it can manage to fulfill it.

But let’s not forget I’m discussing the subject of DC brushed motors in robotics, not every parts and every aspects of a robot. So I must narrow the subject to this question:

How to get a motor being autonomous?

We can now talk about control loop feedback system.

Let’s first define the system as:

  • An actuator (for example a motor) and its control board (e.g. this board)
  • A sensor (speed, torque, position, etc.)
  • A micro-controller board

All of them must be supplied with proper electric energy. As a user, you want the actuator to reach a value, that is called a set-point.

A control loop feedback system is a system that runs in a close loop, with a set-point to reach. The command given to the actuator to reach the set-point depends on the feedback. This feedback consists in the actuator’s value measured by the sensor and compared to the set-point value. The resulting error is computed and re-injected into the initial order as a command that automatically corrects and adjusts the value of the actuator, in order to reach the set-point.

Applied to the motor-sensor-system I defined above, it follows a simple recipe (please note it’s highly simplified for now):

  • An initial order is given to the motor
  • The sensor retrieves the status of the motor
  • The error is calculated
  • The error is re-injected and computed into the initial order
  • And so on.

This illustration will help you understand:

Example of a control loop feedback system, for the speed control of pencil-drawn motor.

Now you see through this example how the speed is controlled: the system will try to reach Wanted speed (set-point) by computing the error and re-injecting it into the initial command. If someone has the brillant idea to add some load on the output of the motor, the system will detect the change of speed due to that load, the error will rise, and the correction will be higher.

This is an adaptation to environment, and it’s called a feedback loop.

There are different ways of controlling a DC motor. Speed-control and position-control are two common examples, but torque-control is possible as well. You can even make a temperature- or a noise-control if you wish. Logically enough, an appropriate sensor will be needed in each case — i.e a speed control will work well with a speed sensor.

How can you apply that control loop feedback to your motor?
You will use a PID controller.

PID control

A PID controller is a good example of motor loop control (though it can be used with various different things, like a kitchen oven or a space-exploration rocket), and widely used in robotic applications.

First, let’s emphasize on something: how does it look? If I run into a PID in the street, will I recognize it as one? Actually PID can be hardware (with resistances, doors, wires, etc.) or virtually anything but a code. Today the second case is the most common, because PID can be implemented in a micro-controller.

Now, that code you’re writing with obvious delight is part of the program uploaded in the micro-controller. The micro-controller then gives orders to the motor control board, and this order is applied as a power signal into the motor. You already knew that, right?

But what really is a PID programming code? The term PID stands for proportional, integral, derivative. They are the three main components of the code (associated to three respective constants that I will talk about later).

These three components will be calculated in function of the error given by the sensor. To simplify things, let’s call these components functions: e.g. is the integral function in function of the error . The original target is called set-point.

Highly simplified expression of a PID command, with e as the error between the set-point and the actual measured value.

Note that this kind of command can be sometimes simplified into a PI, PD, or even a P command. It depends on the system you’re working with, as some of them don’t really need the full PID command to work properly:

Here is a new example:

You want your motor to stop fooling around, to get its life on track, and to spin at a rate of 50. No matter the unit, it can be 50 ETR/(Elephant-tail rotations per micro-century), I don’t even care. Just 50.

-Your desired speed: setPoint = 50
-The actual speed of the motor: sensorValue
-The speed command with the PID functions in function of the error: speed = Fp(error) + Fi(error) + Fd(error)

  • 1. In the very first loop, the motor is not running:
    The first command is speed = Fp(0) + Fi(0) + Fd(0) = 0
    >> sensorValue = 0
    >> error = setPoint-sensorValue = 50–0 = 50
    Error is 50. Big.
  • 2. The associated command changes in the second loop, and so the motor has began to spin, it’s rotating at 5, that’s why the error is a bit lower.
    speed = Fp(50) + Fi(50) + Fd(50)
    >>sensorValue = 5
    >>error = setPoint-sensorValue = 50–5 = 45
  • 3. On the third loop the motor has accelerated a lot.
    speed = Fp(45) + Fi(45) + Fd(45)
    >>sensorValue = 60
    >>error = setPoint-sensorValue = 50 — 60 = -10
    The error is negative because the motor is spinning too fast now.
  • 4. The command to correct that on the fourth loop:
    speed = Fp(-10) + Fi(-10) + Fd(-10)
    >>sensorValue = 50
    >>error = setPoint-sensorValue = 50–50 = 0

On the fifth loop, the micro-controller is quite happy because the motor is obeying. Good.

So it will provide a null command speed = Fp(0) + Fi(0) + Fd(0) = 0, because the error is null. But too bad: the motor, receiving a null order, is slowing down. This will results in a rising error, and in a new positive command, and so on. In the end, the motor is getting orders to keep to the set-point value whatever happened.

Viewed on a curve, it will look like this:


We see that the curve — which is the motor speed versus the time — is trying to reach the set-point value 1. Don’t worry, we will discuss more about that in the next following posts.

That’s all for now. To summarize, keep in mind that a PID controller is a widely used control loop feedback system — written as a code most of the time — that allows your motor to reach a set-point value (of position, speed, etc.) and to keep it that way as long as you don’t change this value. It uses a maximum of three main functions to correct the command — Proportional, Integrate, Derivative — which values change in function of the error between the set-point and the actual measure.

On the next posts, we will try to dig in these proportional, integral and derivative components.

Thank you for reading.

— If you liked what you read, please and follow us on Medium!


Embedded open architecture to turn electronics into…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade
A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store