PID made simple

This project is about a simple and low cost way to implement a PID controller to a DC motor with feedback from an incremental encoder. No prior experience in programming or electronics is required; it is a plug and play setup. An Arduino board is used to run the controller, and a user interface built in Matlab to help the user communicate with the hardware (give commands, change parameters, see plots etc.)

By the time one has gathered the necessary parts, it is a matter of 30 minutes to get the whole thing running.

Hardware

You will need:

  • A DC motor with an incremental encoder. I used this phidget motor (3263E_0 - 12V/3.0Kg-cm/78RPM 51:1 DC Gear Motor w/Encoder).
  • An Arduino board to run the controller. Here, I used the Arduino Leonardo, but other Arduino boards can also be used without any changes.
  • A motor controller to drive the motor. I used this one. A simple h-bridge can also be used.
  • A power supply. I used the HY3005-3 DC Power Supply.

1. Program the Arduino board

If you haven't used an Arduino before, first follow the guide found here. After installation, run the first example blinking the on-board LED to make sure you did everything right.

In this project, I simply combined two existing libraries which have to be downloaded for the project to work. The first one, the Encoder Library, is used for reading the encoder signal using the Arduino interrupts. Follow the steps described in the link, and you should be able to read the encoder. The second one, the Arduino PID Library, is used for implementing the PID controller. Again, follow the steps described in the link and you should be ready for what follows.

I wrote a program that combines these libraries. Download the program at the end of this page. Except for controlling the motor, the program communicates with a Matlab program running in an external computer. More on this in a next step. After downloading the program, upload it to the Arduino board.

2. Connect the parts

The encoder should be connected to the Arduino as described here, and the motor shield as shown here.

3. Download the Matlab program

The Matlab program can be downloaded at the end of this page, and it is used to interface with the hardware. See the experiments below.

4. Drive the motor

To drive the motor follow these steps:

  1. Connect the Arduino to your computer.
  2. Find out which serial port your Arduino is connected to. Go to the Matlab code and change the serial port number to your port number. (arduino=serial('COM17','BaudRate',115200);)
  3. Run the Matlab script. A Graphical User Interface (GUI) should appear in your screen.
  4. Turn on the Power Supply. Be careful not to burn your motor. Stay inside the motor and drive voltage limits.
  5. Give proper values to the PID gains (see the video for this).
  6. Finally, for the motor to move, give a setpoint command and hit enter. The motor should move towards the commanded value.

5. Experiments

P Controller

The gains were set to: kp = 0.55 , ki = kd = 0, and the motor was commanded to go to 5000, 13000, and 3000 counts.

Although this was a P controller, no oscillations were observed due to the high friction of the geared motor.

PD Controller

The gains were set to: kp = 0.55 , kd = 0.3, ki = 0, and the motor was commanded to go to 5000, 13000, and 3000 counts.

PID Controller

The gains were set to: kp = 0.55 , kd = 0.3, ki = 1.3, and the motor was commanded to go to 5000, 13000, and 3000 counts.