Final Project Report: PID Control Using Arduino UNO Microcontroller
Final Project Report: PID Control Using Arduino UNO Microcontroller
Team Members:
SanketRairker
661485906 srairk2@uic.edu
(Leader)
Table of Contents
Section Title Page No.
1 Summary and Objectives 2
2 Description of the Project 3
3 List of Components and Measurement Equipment Used 6
4 Circuit Diagram and Code 7
5 Procedure 10
6 Results 11
7 Conclusions 18
8 References 18
7 Table 3: Oscilloscope visual of the input ramp waveform (blue), PID filtered 16
output wave (yellow), and PID unfiltered output wave (purple)
8 Table 4: Oscilloscope visual of the input ramp waveform (blue), PID filtered 18
output wave (yellow), and PID unfiltered output wave (purple)
𝑃 = 𝐾𝑝 ∗ 𝑒𝑟𝑟𝑜𝑟(𝑡)
A high proportional gain results in a large change in the output for a given change in the error.
If the proportional gain is very high, the system can become unstable. In contrast, a small gain
results in a small output response to a large input error. If the proportional gain is very low, the
control action may be too small when responding to system disturbances. Consequently, a
proportional controller (Kp) will have the effect of reducing the rise time and will reduce, but
never eliminate, the steady-state error.
𝐼 = 𝐾𝑖 ∗ 𝑒𝑟𝑟𝑜𝑟(𝑡)
𝑑
𝐷 = 𝐾𝑑 ∗ (𝑒𝑟𝑟𝑜𝑟 𝑡 )
𝑑𝑡
A PID controller is a summation of each of the threecontroller types listed above. Their
combination provides improved control over their individual functionalities. A diagram of a PID
controller can be seen below.
4
Figure 1 shows how the error (E(s)) in a system is evaluated by a combination of proportional,
integral, and derivative control to provide a single output (U(s)). The output of a PID controller can
be described by the following transfer function.
𝑈(𝑠)
𝐺 𝑠 =
𝐸(𝑠)
𝐾𝑖 𝐾𝑑 𝑆 2 + 𝐾𝑃 𝑆 + 𝐾𝐼
𝐺 𝑠 = 𝐾𝑝 + + 𝐾𝑑 𝑆 =
𝑆 𝑆
For this project, a microcontroller will be used to build a digital PID controller. A microcontroller
refers to a chip that integrates a microprocessor, input/output interfaces, and a communication bus.
The microprocessor refers to the central processing unit and its memory. Programming a
microcontroller involves defining logic between the inputs and outputs based on desired
functionality. An Arduino UNO microcontroller was selected for this project. See a diagram of the
Arduino UNO in Figure XX below.
USB
Plug
Power
Pins
In order to implement a PID controller using a microcontroller, the error in the system is connected
to the analog input pins. The analog signal is processed according to microcontroller’s programmed
commands, then a resulting output voltage is fed to the output pins. For this experiment, the Pulse
Width Modulation (PWM) ~3 pin was selected for digital output. From the Arduino learning site:
Digital control is used to create a square wave, a signal switched between on and off.
This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by
changing the portion of the time the signal spends on versus the time that the signal
spends off. The duration of "on time" is called the pulse width. To get varying analog
values, you change, or modulate, that pulse width.
The portion of time spent ON in the PWM cycle is called the “Duty Cycle”. For example, a 25%
Duty Cycle means that the square wave is ON for the first quarter of time for each iteration. In
order to call this PWM pin on the Arduino UNO board, the function, “analogWrite()” is used.
analogWrite()writes a value between 0 and 255 to an output pin and is directly proportional to the
value of the Duty Cycle. See equation below for an explanation.
If
𝑎𝑛𝑎𝑙𝑜𝑔𝑊𝑟𝑖𝑡𝑒(191)
Then,
191
= .75
255
Therefore,
𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒 𝑃𝑒𝑟𝑐𝑒𝑛𝑡𝑎𝑔𝑒 = 75%
It is in this manner that the output of the PID controller is measured and provided as an output to
the PWM pin.
In order to convert the PWM signal from a digital to true analog signal, the implementation of a
low pass filter to condition the output is required. This serves to remove most of the high frequency
components, leaving the desired analog signal. The cutoff frequency must be low enough to
remove unwanted spikes from the PWM output, but allow the desired low frequency signal to pass.
See the following relationship to describe the cutoff frequency (fc) of a passive low pass filter.
1
𝑓𝑐 =
2𝜋𝑅𝐶
6
Arduino UNO 1
Computer 1
USB Cable 1
IDE Software 1
Breadboard 1
Alligator Clips 3
Jumper Wires 6
Capacitors 1 126 nF
Oscilloscope 1
Digital Multimeter
1
(DMM)
Power Supply 1
Function
Generator
Power Supply
Breadboard
C
R
Vout
Analog
Error Input
Figure 3: Circuit Diagram including the Arduino UNO and RC Circuit (Low Pass Filter) on a
Breadboard, sharing a common ground.
In order to remove noise from the PWM output and to convert the signal from digital to analog, a low
pass filter was implemented. The desired cutoff frequency is very low for the purposes of this project,
requiring large resistance (R) and capacitance (C) values. This was determined by analyzing the
repetitive signal outputted by the PWM pin. Spikes were occurring every 10 milliseconds, or 100 Hz.
A resistance value of about 40 kΩ and a capacitance value of 126 nF were selected for the low pass
filter. The following calculation was used to determine the resulting cutoff frequency.
1
𝑓𝑐 =
2𝜋𝑅𝐶
1
𝑓𝑐 =
2𝜋(40 kΩ)(126nF)
𝑓𝑐 = 32.2 𝐻𝑧
8
The cutoff frequency of 32.2 Hz is lower than that of the higher frequency noise produced by the
PWM output of 100 Hz. Therefore, the low pass filter design is appropriate. See below for a
photograph of the completed circuit used for experimentation.
The following is the code used to program the Arduino UNO to behave as a PID controller.
#include <TimerOne.h> Calls the TimerOne Library
//setting the gain values
float Kp = 1; //proportional gain constant
float Ki = 0.08; //integral gain constant This portion of the code creates variables to be
float Kd = 0.04; //derivative gain constant used later on in the code.
float prv_e = 0; //previous error
float input; //input from function generator read Assigns values for Kp, Ki, and Kd,
from pin A3
float e; //error Initializes prv_e, out, and intgl values to 0
float d_err; //derivative error
float out = 0; //output of PID function
float intgl = 0; //integral
void setup() { This portion of the code sets up the timing
// pin modes declaration interrupt for the PID loop. The interrupt runs
noInterrupts(); //disable interrupts every 10000 microseconds (100 Hz).
pinMode(A3, INPUT); //A3 analog pin as input
Serial.begin(9600); //sets the data rate in bits per This means that the PID loop is run every time the
9
second (baud) for serial data transmission interrupt occurs to calculate a new value for error
pinMode(6, OUTPUT); //pin 6 as output adjustment.
Timer1.initialize(10000); //initilize timer1 and
sets period in microseconds
Timer1.attachInterrupt(PID); //// attaches PID()
as a timer overflow interrupt
interrupts(); //re-enable interrupts
}
5. PROCEDURE:
1. We wrote an appropriate code within the Arduino IDE software environment to convert
input (error) to a PID output with adjustable Kp, Ki, and Kd values. The timer was set to
generate interrupts at 100Hz.
2. A circuit was built with Arduino I/O pins connected and with the output passed through a
low pass filter as shown in Figure 3. The filter had a total resistance of 36 kΩ and
capacitance of 127nF. Cutoff frequency for the circuit was 31.96 Hz.
3. We uploaded the code on the Arduino and then connected the output positive lead to the
A3 analog input pin and the negative lead to ground.
4. The output was compared with the input signal on the oscilloscope. The output waveform
was observed for square, sine and ramp inputs.
5. Gain tuning-
We manually tuned the gains Kp, Ki and Kd by observing the changes in the waveform. We
started by keeping Kp=1 and other gains zero. The value was increased until for a particular
value the waveform began oscillating. This value was halved and set as Kp. Then Ki was
varied by observing offset changes in the output. The Ki value with minimum possible
offset was finalized. Lastly, the derivative gain was changed to decrease the response time
of the output.
6. The outputs of the circuit with the low pass filter and without the filter were obtained and
compared. We also compared the outputs with only proportional, only derivative and only
integral gains.
11
6. RESULTS:
The Arduino PID controller was built as specified in previous sections and tested experimentally.
Multiple waveform types were applied to the PID controller, including a square waveform, a sine
waveform, and a ramp waveform.
The K values were tuned to the following with the best results:
Kp =1.2
Ki=1
Kd =0.05
The following tables show the various oscilloscope responses for each of the waveforms tested
with the PID controller for this project. Blue lines indicate the input function, purple lines indicate
the unfiltered output function, and yellow lines indicate the low pass filtered output function.
12
Oscilloscope Output
Oscilloscope Output
Oscilloscope Output
As the previous three results tables show, the filtered signal is much clearer than the unfiltered signal
in each case. Each of the upright unfiltered signal lines represents a repetitive noise output from the
PWM pin. They are spaced 1 millisecondapart. The removal of this noise produces the yellow curve in
the tables above, a much clearer representation of the PID’s functionality. The PID controller does a
good job of following the input path for each waveform type with the selected K values.
18
7. CONCLUSIONS:
In this experiment, we successfully implemented digital PID control using Arduino UNO
microcontroller. The effects of varying the proportional, integral and derivative gains have been
summarized below:
Steady-State
Parameter Rise Time Overshoot Settling Time Stability
Error
No effect in Improve if Kd
Kd Minor Change Decrease Decrease
theory is small
Table 5: Effect of increasing dynamics parameters on the response of the system
We also observed the effect of adding a low pass filter at the output of the Arduino; the filter
not only removes the noise of the output PWM signal, it also helps in obtaining an analog
output from the PWM output provided by the Arduino UNO. By limiting the cutoff frequency
to 31.96 Hz, we were able to obtain a much clearer waveform.
8. PROBLEMS FACED:
Initially there was a problem with the output waveform obtained on the oscilloscope as the
waveform was distorted even when the circuit connections had been properly made. It was
later found that the oscilloscope was faulty and the problem was solved by switching to a
different oscilloscope.
The cutoff frequency was initially set to about 120Hz and the obtained output was not
satisfactory. So it was reduced to 31.96 Hz, which resulted in a much clearer output.
We also faced problems while implementing the code while mapping the input to a suitable
range. We initially mapped the nput from 0-1023 to 0-5 and then from 0-5 to 0-255 once PID
calculations were completed. This was providing incorrect results. We then removed the
second mapping function and mapped the input function directly from 0-1023 to 0-255. This
provided a much more accurate output and a higher resolution signal for processing.
9. REFERENCES:
1. Department of Mechanical and Industrial Engineering (2009), Mechatronics Lab Manual,
Chicago Illinois: Cetinkunt, S.
2. http://www.atmel.com/Images/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-
328P_datasheet.pdf
3. http://www.ni.com/white-paper/3782/en/#toc3