DC Motor Speed Control with PID

In this project it is aimed to speed control a standard Pololu DC motor with encoder using software based PID controller.

Similar projects worth following
PID control methods are widely used in the industry. In this project we will be using PID for speed control of a DC motor. For this purpose encoder feedback is used to compare the target speed and actual speed and the difference between these two value (error) is used to drive the motor.

DC Motor Speed Control with PID

Encoder Library

Zip Archive - 12.76 kB - 05/23/2016 at 14:23


View file

  • 1 × Arduino Mega2560 The Mega 2560 is a microcontroller board based on the ATmega2560. It has 54 digital input/output pins (of which 15 can be used as PWM outputs), 16 analog inputs, 4 UARTs (hardware serial ports), a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started. The Mega 2560 board is compatible with most shields designed for the Uno and the former boards Duemilanove or Diecimila.
  • 1 × Pololu DC Motor with Encoder (64 CPR) This gearmotor is a powerful 12V brushed DC motor with a 30:1 metal gearbox and an integrated quadrature encoder that provides a resolution of 64 counts per revolution of the motor shaft, which corresponds to 1920 counts per revolution of the gearbox’s output shaft. These units have a 0.61"-long, 6 mm-diameter D-shaped output shaft.
  • 1 × Breadboard A breadboard is a construction base for prototyping of electronics. Originally it was literally a bread board, a polished piece of wood used for slicing bread. In the 1970s the solderless breadboard (AKA plugboard, a terminal array board) became available and nowadays the term "breadboard" is commonly used to refer to these. "Breadboard" is also a synonym for "prototype". Because the solderless breadboard does not require soldering, it is reusable. This makes it easy to use for creating temporary prototypes and experimenting with circuit design. For this reason, solderless breadboards are also extremely popular with students and in technological education. Older breadboard types did not have this property. A stripboard (veroboard) and similar prototyping printed circuit boards, which are used to build semi-permanent soldered prototypes or one-offs, cannot easily be reused. A variety of electronic systems may be prototyped by using breadboards, from small analog and digital circuits to complete central processing units (CPUs).

  • Step 1 - Encoder

    Ozan Enginoglu05/23/2016 at 12:43 0 comments

    In this step, it is aimed to connect the encoder (just encoder not the dc motor) to the Arduino and check if it is functioning properly. We know that the encoder output for the gearbox shaft's only one turn should return 1920 so we will be checking that manually by rotating the shaft of the motor by hand.


    Luckily, pin diagram of the encoder is given on the pololu web site as shown in the table below.

    Redmotor power (connects to one motor terminal)
    Blackmotor power (connects to the other motor terminal)
    Greenencoder GND
    Blueencoder Vcc (3.5 - 20 V)
    Yellowencoder A output
    Whiteencoder B output

    Motor power pins won't be used in this step but the other four pins are necessary for the encoder to function properly. Encoder GND and Vcc pins are used to power encoder circuit and encoder A and B pins are used to get feedback from the encoder. Green encoder GND will be connected to Arduino's ground pin and Vcc pin will be connected to Arduino's +5V regulated voltage output which is within the range (3.5 V - 20 V).

    While connecting Encoder Pins A and B, it is important to select the pins that can be used for interrups. The reason for using interrupt pins is that Arduino should be able to get the feedback data from the encoder as soon as it changes. If the interrups are used Arduino will be able to stop whatever it's doing at that moment and get the encoder feedback first and continue. After a short search, i was able to find the table shown below.

    BoardDigital Pins Usable For Interrups
    Uno, Nano, Mini, other 328-based2, 3
    Mega, Mega2560, MegaADK2, 3, 18, 19, 20, 21
    Micro, Leonardo, other 32u4-based0, 1, 2, 3, 7
    Zeroall digital pins, except 4
    MKR1000 Rev.10, 1, 4, 5, 6, 7, 8, 9, A1, A2
    Dueall digital pins

    In this project I'm using Arduino Mega2560 so the pins i can use are 2, 3, 18, 19, 20, 21. Among these I preferred 18 and 19. So I connected encoder output A to digital pin 18 and output B to digital pin 19.

    Pin connections of the encoder are shown in the Figure given below. As can be seen in the figure, motor terminal connections are intentionally left blank.

    Encoder Connections

  • Fundementals

    Ozan Enginoglu05/23/2016 at 12:41 0 comments

      Standard DC motors without encoders have just two cables (terminals) to drive the motor. These terminals are used for creating a voltage difference which is directly related to motor speed. The shaft rotation speed is increased if the voltage difference between terminals are increased or vice versa.

      By inverting the applied voltage to these connections, it is possible to drive the motor in the opposite direction. But unfortunately, the relation between the applied voltage and the speed is not linear and the output of the shaft rotation is unpredictable due to various reasons such as a torque applied to the shaft. In such case shaft rotation speed is reduced even though the voltage stays same. So it is clear that the speed of the motor doesn't only depend on the voltage but also to the environmental conditions.

      If speed control of a DC motor is desired, another sensor called encoder is required to get the actual position and thereby speed of the shaft rotation. These information is then used to compare and if necessary correct the speed of the dc motor.

      Pololu DC motor with Encoder that i've used in this project is as shown in the picture below.

      Full Name: 30:1 Metal Gearmotor 37Dx52L mm with 64 CPR Encoder

      Pololu DC motor with Encoder

      This motor has a metal gearbox with a ratio of 30:1 and a shaft with a diameter of 37 mm. Resolution of the encoder that is assembled to the motor shaft is 64 Cycles Per Revolution (CPR). This means that if the motor shaft turns one round the output of the encoder counts up to 64. But it is important to notice that the gearbox shaft is different than the motor shaft and as mentioned before, the ratio of rotation between these shafts is 30:1. So if motor shaft turns 30 times, gearbox shaft turns just 1 time. If this relation is considered; the cycles per revolution of the gearbox shaft can be calculated with the following formula:

      and it is equal to 30 * 64 = 1920 CPR.

      The gearbox output resolution (1920 CPR) is of great importance in this project because it is the only feedback we get from the DC motor and using this feedback we shall acquire position and speed variables to be used in PID control which will be explained in detail later.

      Just like almost all complex projects, we should always prefer going through the project step by step to prevent any unknown behaviour output from the system. For this project, the steps i am planning to follow are listed below.

      1. Encoder
      2. DC Motor + Motor Driver + Encoder
      3. PID + DC Motor + Motor Driver + Encoder

View all 2 project logs

Enjoy this project?



Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates