# What is a PID controller and how does it work with an Arduino?

Hello, welcome back. In this tutorial, we will learn what is a PID controller and how does it work with an Arduino platform. It also explains how the PID controller works using a simple example. For that, described how to make a ball balancing PID controller system and all of these are explained through this project. You can use this knowledge mainly to build self-balancing robots, line follower robots, drones and model air crafts, etc. Keep reading.

## What is the PID (Proportional Integral Derivative )?

PID is a more stable control system that is primarily used to control factors such as pressure, temperature, velocity and flow. Also, this PID consists of three main controllers. That is,

• P (Proportional) — This Proportional controller gives a proportional output to the error. For that, the error must be multiplied by a constant called kp. The correct value should be found by entering different values for the Kp constant. We can get the error by subtracting the actual value from the set point. Through this, we can stabilize the system to some extent.
• I (Integral) — The steady-state errors can be detected through this controller. For that, the error must be integrated. Then, must multiply that value by a constant called ki. This correct ki value should be found using different values.
• D (Derivative) — This derivative controller eliminates the problem of overshoot caused by the proportional controller. For that, the error must be differentiated. Then, that value must be multiplied by a constant kd. This correct kd value should be found using different values

Also, we can get the PID value by adding the above three controller values.

Also, obtaining the three constants kp, ki and kd are called tuning a PID control system. The correct three values must be found by entering different values. Go to Wikipedia for more information.

Okay, let’s learn how to create a PID control system with Arduino step by step. The required components are as follows.

### Step 1

Firstly, identify these components.

### Step 2

Second, cut the two foam board pieces as follows.

### Step 3

Thirdly, glue them together using the pictures below.

### Step 4

Next, glue a piece of foam board to the corner.

### Step 5

Then, cut rigifoam or foam board pieces as following sizes. After, glue them.

### Step 6

Then, take the ball holder and glue a small piece of pipe with a cut in the middle.

### Step 7

Next, attach the ball holder to the supporter using an iron stick. For that, use the pictures below.

### Step 8

Afterwards, glue the small pipe piece and servo motor as follows.

### Step 9

OK, now take the iron rod and set it as follows and connect it to the ball holder and the servo motor.

### Step 10

Next, glue the ultrasonic sensor to the other corner of the ball holder.

### Step 11

Then, glue two pieces of foam board to block the ball. After, glue the Arduino UNO board.

### Step 12

OK, now connect these components to the Arduino board. For that, use the circuit diagram below.

### Step 13

Next, connect this project to the computer. OK, let’s creates the program for this project. It is as follows.

• The complete program of this project – Download
``````/*PID controller system with Arduino.
* http://srituhobby.com
*/

#include <Servo.h>
Servo servo;

#define trig 2
#define echo 3

#define kp 0
#define ki 0
#define kd 0

double priError = 0;
double toError = 0;

void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
servo.attach(5);
Serial.begin(9600);
servo.write(50);

}
void loop() {
PID();
//  int a = distance();
//  Serial.println(a);
}

long distance () {
digitalWrite(trig, LOW);
delayMicroseconds(4);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);

long t = pulseIn(echo, HIGH);
long cm = t / 29 / 2;
return cm;
}

void PID() {
int dis = distance ();

int setP = 15;
double error = setP - dis;

double Pvalue = error * kp;
double Ivalue = toError * ki;
double Dvalue = (error - priError) * kd;

double PIDvalue = Pvalue + Ivalue + Dvalue;
priError = error;
toError += error;
Serial.println(PIDvalue);
int Fvalue = (int)PIDvalue;

Fvalue = map(Fvalue, -135, 135, 135, 0);

if (Fvalue < 0) {
Fvalue = 0;
}
if (Fvalue > 135) {
Fvalue = 135;
}

servo.write(Fvalue);
}``````

#### Code explanation

Firstly, the servo motor library is included.

``````#include <Servo.h>
Servo servo;``````

Secondly, the ultrasonic sensor pins are defined.

``````#define trig 2
#define echo 3``````

Thirdly, let’s includes the kp,ki and kd values. To do this, enter different values for these three constants and obtain the correct values.

``````#define kp 15
#define ki 0.02
#define kd 20``````

In the void setup,

``````void setup() {
//The ultrasonic sensor Trig pin set as output pin and Echo pin set as an input pin
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
//The servo motor pin is set
servo.attach(5);
//The serial monitor is beginning
Serial.begin(9600);
//The servo motor rotates 50 degrees
servo.write(50);

}``````

In the loop function, the main function is called.

``````void loop() {
PID();
//  int a = distance();
//  Serial.println(a);
}``````

This code gets the values through the ultrasonic sensors.

``````long distance () {
digitalWrite(trig, LOW);
delayMicroseconds(4);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);

long t = pulseIn(echo, HIGH);
long cm = t / 29 / 2;
return cm;
}``````

This is the main function of this program,

``````void PID() {
//Gets the distance
int dis = distance ();

//Includes the set point
int setP = 15;
//Gets the error value
double error = setP - dis;

//This code calculates the P term
double Pvalue = error * kp;
//This code calculates the I term
double Ivalue = toError * ki;
//This code calculates the D term
double Dvalue = (error - priError) * kd;
//This code gets the PID value
double PIDvalue = Pvalue + Ivalue + Dvalue;
priError = error;
toError += error;
Serial.println(PIDvalue);
int Fvalue = (int)PIDvalue;

Fvalue = map(Fvalue, -135, 135, 135, 0);

if (Fvalue < 0) {
Fvalue = 0;
}
if (Fvalue > 135) {
Fvalue = 135;
}
//The servo motor rotates
servo.write(Fvalue);
}``````

### Step 14

Ok, now select board and port. After, upload this code to the Arduino board.

### Step 15

Finally, take the ball and place it on the holder. OK, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial.

What is a PID controller and how does it work with an Arduino?