Introduction: Arduino PID Library - Brightness Control

Picture of Arduino PID Library - Brightness Control

PID Definitions:

The Proportional/Integral/Derivative controller or PID controller or just PID, is a proccess' control techinics that join Proportional integral and Derivative actions, thus causing the error signal is minimized by proportional action, integral action and cleared by a speed obtained with the preemptive derivative action.
It is based on mathematical modeling of the response of a loop process to be controlled.

In practice the PID controllers are found inside electronic controllers called "single-loop", often with microprocessors, and also through software on programmable logic controllers (PLC) and other control equipment.

In this project, we will simulate a brightness control with Arduino.
The Arduino will be running the PID library which can be found at:

http://playground.arduino.cc/Code/PIDLibrary

In addition to the PID library for Arduino, will also need the PID Front End for processing, which is a GUI in order to configure the control parameters, such as P, I, D, Set Point and ways of working, etc. .. .; performing a user-friendly interface with Arduino.
You can also download the PID_FrontEnd from the link above.

As  we will work with Arduino and Processing, we need to have the Arduino's IDE and Processing 1.0.1 2.0b8 installed in our computer.
To the Processing software, run properly with PID_FrontEnd, we also need the library "ContorlP5" for Processing installed in our computer.
You can Download the library "ControlP5" directly from the related link below:

http://www.sojamo.de/libraries/controlP5/#installation

For details on PID control processes, I advise you to visiting the PID_V1 Library's creator website.

http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/

Also access  the "Control Guru" website for more details about automation and control.

http://www.controlguru.com/pages/table.html

Details about "Control Systems" can be found in the website:

http://www.facstaff.bucknell.edu/mastascu/econtrolhtml/CourseIndex.html









Step 1: Constructive Aspects - Part One

Picture of Constructive Aspects - Part One

Let's go now turn to the constructive aspects of our project:

We will do two tests, one with the simulated environment by a dark tube (this environment will not allow interference from outside), and another test with the open environment, where we can interfere externally and thus, verify the system response.


Test - 1
The environment is simulated by a dark tube (black).
At one end of the tube, we will have a white LED with high-brightness (it will be used to illuminate the environment). At the other end of the tube, we will have a LDR (the LDR will be used to manage the amount of light in the environment, it will be our system's  feed back).

The connection with the Arduino is very simple:
The pin D3 (digital 3) of the  Arduino will be used as output and is connected to the LED's anode. The LED has a resistor of 220 Ohms in series with the anode (for the LED's  polarization); the LED's cathode is directly connected to GND.
The pin A0 (analog 0) of the Arduino will be connected to the LDR and used as analog input. The LDR has one leg connected directly to the +5 V supply, and the another leg of the LDR is connected to a pull-down resistor value of 10K (the resistor will be connected in series with the LDR and connected to the GND. At the intersection of the LDR + resistor we will have the output signal of the light sensor that will be connected to  the Arduino's pin A0.

Look at the pictures to know how to do the circuit interconnection of sensors and actuators with Arduino, the circuit was designed with Fritzing.












Step 2: Constructive Aspects - Part Two

Picture of Constructive Aspects - Part Two

Test - 2
Now the system may suffer external actions which are called disturbances such as shading or excessive light in the sensoror in the environment or even blocking the sensor.

The disturbances are automatically controlled by the Arduino_PID, ie:
When we have a decrease of the incident light on the LDR (simulated by a shadow on LDR), the system will automatically increase the brightness of the LED trying to maintaining a constant ambient lighting.
When we have an increase of the incident light on the LDR (a flashlight may increase the brightness for simulation purposes), the system will reduce the brightness of the LED trying to maintaining a constant ambient lighting.

Another detail of this particular test is that the Setpoint value, is controlled by a potentiometer, is no longer needed to using the Setpoint configuration via PID_FrontEnd.

In the pictures, we have the circuit of sensors and actuator's  interconnection with Arduino.


Step 3: Programming Aspects

Picture of Programming Aspects



1 - Download and unzip the libraries:
PID_V1 to the libraries' directory of the Arduino IDE;
Front End PID to a folder on your computer;
ControlP5 to the Processing Libraries' directory.

2 - Start the Arduino IDE and open the file "PID_FrontEnd_ArduinoSampleCode" from the "PID_FrontEnd" folder.
Send this scketch for arduino.

3 - Start the "Processing" and open the file "PID_FrontEnd_v03" from the  PID_FrontEnd folder.
Run the application.

Look at the some pictures related to the process described above:



For the second test, in particular;  we must send to the Arduino, the sketch below:
(this sketch will be used in place of the file "PID_FrontEnd_ArduinoSampleCode")
You can download this sketch from the link below.

http://www.4shared.com/file/Y5BIbUdR/LED_PIDcontroller_2.html

Remembering  that the Setpoint control value  is done by a potentiometer.
As a disturbance source, we can use a flashlight to focus more light in the LDR or a little screen to shade the LDR.
When we have a disturbance in the process, the controller will increase / decrease the brightness of the LED trying  to keeping the output under control and at the level settled by the setpoint (in the case of making a "shadow", the controller will increase light intensity of the LED), we can see these changes in PID_FrontEnd graphically.

Step 4: PID FrontEnd GUI

Picture of PID FrontEnd GUI

When running the application, the PID_FrontEnd window will open.

In this window, we can note the following functions:
TOGGLE_AM - changes the PID mode to automatic or manual;
SETPOINT - desired amount of light in the environment(from 0 to 1024);
INPUT - the actual value of the ambient brightness (measured by LDR and returned as feedback);
OUTPUT - the control value returned by the Arduino as a PID controller (brightness control of the LED attached on pin D3);
Kp - Proportional control's constant;
Ki - Integral control's constant;
Kd - Derivative control's constant;
TOOGLE_DR - changes the PID's direction (if output grows as grows the entry or the reverse of it);
SEND_TO_ARDUINO - send data to the Arduino;

PID Input / Setpoint - Graphical Window for the setpoint (desired value for brightness) and input (measure of brightness LDR) curves.
PID Output - Graphical window for the control output of the LED.


In the pictures, we can notice the setpoint settled to 950 (green line), the input variable (red line) and the PID control output variable (blue line) to control brightness.


We can understand the information described acimas through a block diagram (look at the pictures).
Where we note that the Error = SP - PV and the error is applied to the PID controller that generates the MV for process control.
PV = Proccess Variable;
SP = Set Point;
MV = Manipulated Variable.

Each controller type: P, I and D can be used independently or jointly as:
P controller (very used)
PI controller (very used)
PD controller (rarely used)
PID controller (most used)

Look at the pictures and you can observe a change in the setpoint value (from 950 to 700 and then to 950 again) and you can see the performance of PID control on the input variable.
You can observe that has a bit of oscillation in control (characteristic of each controlled system, whether it is first or second order, etc.).







Step 5: Videos

Picture of Videos

Comments

darzo18 (author)2016-03-16

Hello, is there any way you can replace the potentiometer to set the setpoint with some other chip or resistor?

joliveira2 (author)2015-07-30

Excelente explicação sobre PID!

Muito obrigado

MayankR (author)2015-05-28

Dear BIGDOG1971,

Thank you for posting this Instructable! I was really confused as to how to start working on PID. This has been really helpful.

I am stuck on How to start "process" and do the rest.

There is one more problem when I include the PID Library, nothing happens. I have installed and the libraries appear in the drop down menu, But the #include does not appear on IDE when I select them. Could you tell me where I could be going wrong?

Waiting for your reply! Thanks!

Mayank

abdullahcakan (author)2015-05-14

what is this error ?

COM19

ControlP5 2.0.4 infos, comments, questions at http://www.sojamo.de/libraries/controlP5

java.lang.ArrayIndexOutOfBoundsException: 304

at controlP5.BitFont.getGlyph(Unknown Source)

at processing.core.PGraphics.textCharImpl(PGraphics.java:4681)

at processing.core.PGraphics.textLineImpl(PGraphics.java:4669)

at processing.core.PGraphicsJava2D.textLineImpl(PGraphicsJava2D.java:1787)

at processing.core.PGraphics.textLineAlignImpl(PGraphics.java:4659)

at processing.core.PGraphics.text(PGraphics.java:4356)

at processing.core.PGraphics.text(PGraphics.java:4307)

at processing.core.PApplet.text(PApplet.java:13183)

at controlP5.ControlFont.draw(Unknown Source)

at controlP5.Label.draw(Unknown Source)

at controlP5.Label$SinglelineLabel.draw(Unknown Source)

at controlP5.Label.draw(Unknown Source)

at controlP5.Textfield.draw(Unknown Source)

at controlP5.ControllerGroup.drawControllers(Unknown Source)

at controlP5.ControllerGroup.draw(Unknown Source)

at controlP5.ControlWindow.draw(Unknown Source)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1236)

at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1229)

at processing.core.PApplet.handleMethods(PApplet.java:1423)

at processing.core.PApplet.handleDraw(PApplet.java:2401)

at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)

at processing.core.PApplet.run(PApplet.java:2256)

at java.lang.Thread.run(Unknown Source)

MariaB1 made it! (author)2014-10-29

Thank's for your reply .

it was my operating system problem.

Now i have compiled the program and it is working well.

Exactly i want to use this example to control a dc motor (rpm) and control P , I and D using variable resistors. i have measured rpm using INTERRUPT AND TIMER 1

I have connected variable resistors as name of P , I and D and measured these in variables . Now i want to put these variable value in Kp, Ki and Kd.

what is maximum value of P , I and D which i should round of for KP,KI,KD.

for example p= 1 to 100 , i= 0.00 to .90 d =

i am stuck on this stage . i need your advice.

MariaB1 (author)2014-10-20

hi ..

i want to make a pid controller . i have downloaded and installed library .
when i open any example and want to write into my arduino it gives this error .

"pid does not name a type"
can anybody please suggest me any idea to solve this issue.
i will be very than full .
pleaseeeeeeeeeeeeeeeeeeeeeeeeee

BIGDOG1971 (author)MariaB12014-10-21

Hi Maria,

This kind of issue is due to libraries installation failure.
To solve the problem, proceed as shown below.

Remove all project libraries from the "libraries" folder.
To install the library(ies), follow the steps below:
1 - take sure that the Arduino's IDE is closed;
2 - unzip the library(ies) in the "libraries" folder of the Arduino's IDE;
3 - open the .ino file and compile it..

90% of people with this kind of problem, have solved it by following the procedure above.
If not, please re-install the arduino's IDE.


I hope I have helped you.

Feel free to send me your questions.


Best Regards


Marcelo Moraes
arduinobymyself.blogspot.com.br
arduinobymyself@gmail.com
Skype: marcelo.moraes

theinlinaung2010 (author)2014-08-14

I tried adjusting P, I, and D constants but there was a small oscillation. It seemed the disturbance is too large.

susurluk8 (author)2014-05-06

hi its great application. can you send to me transfer function ? aykutdurgut@gmail.com

BIGDOG1971 (author)susurluk82014-05-08

Sorry
I do not have the transfer function for the system

Use this website to learn how to getting the transfer function from a real system.

http://www.facstaff.bucknell.edu/mastascu/econtrolhtml/CourseIndex.html

BIGDOG1971 (author)sylD2014-04-13

Hi,
Send me an email requesting the code and libraries and I will share the files with you.

arduinobymyself@gmail.com

ealkhateeb (author)2013-12-07

hi ,

good job bro , but
how I can download Processing v2.0 ? or where ?

its website don't work !!

BIGDOG1971 (author)ealkhateeb2013-12-10

Hi Ealkhateeb
You will find processing to downloading on: https://processing.org/download/

Ensure to download the correctly version of "processing" compatible with your Windows version and compatible with your hardware 32/64 bits.

regards

pantelis111 (author)2013-11-18

hi!! you made a great job.i would like to ask you which is the model of the led,sensor system.

i want to find the Differential Equation of the system and find the transfer function.

thank you

About This Instructable

95,907views

94favorites

License:

Bio: I am 43 years old and I live in Brazil. I am a Telecommunication / Electronics Engineer. Sixteen years acting in the industry of "Telecommunication and ... More »
More by BIGDOG1971:Arduino Propeller Message Display (POV)ARDUINO By Myself Mini-CNC (Plotter)ROVER Bot - The Arduino Bluetooth Controlled Tank
Add instructable to: