Ambient Light Control: Using Software PID Controller
Ambient Light Control: Using Software PID Controller
CONTROL
Using software PID controller
Group Members Presented to
■ Syed Muhammad Ali Ashja Dr. Bilal Qureshi
FA16-BEE-102
■ Uzair Bukhari
FA16-BEE-046
■ Muhammad Usama Tahir
FA16-BEE-154
Introduction to PID
■ We will use the Standard PID library for Arduino for this project.
■ The Code sets the default perimeters ,Read inputs from sensors and the compute PID value
with a sample time of 2mS.
■ The setpoint is adjusted using a POT.
■ The control Method has two Approaches to remove Error:
1. constant gains.
2. aggressive gains.
■ The Constant gains use lower gain value and if the gap between error and set point is High then
we use Aggressive gains.
■ Functional code Snippets are shown below:
Code
(contd)
Setpoint =150 ; Setpoint=255;
myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(0, 255); double gap = abs(Setpoint-Input);
myPID.SetSampleTime(2); if (gap < 10){ myPID.SetTunings(consKp, consKi, consKd);
} }else
void loop(){ {
digitalWrite(in1, LOW); myPID.SetTunings(aggKp, aggKi, aggKd);
digitalWrite(in2, HIGH); }
Input=map(analogRead(0),0,1023,0,255); myPID.Compute();
Setpoint=map(analogRead(A1),0,1023,0,255); analogWrite(enA,Output);
if(Setpoint>250)
Code
(contd)
bool PID::Compute() f(outputSum > outMax) outputSum= outMax;
{ if(!inAuto) return false; else if(outputSum < outMin) outputSum= outMin;
unsigned long now = millis(); double output; if(pOnE) output = kp * error;
unsigned long timeChange = (now - lastTime); else output = 0;
if(timeChange>=SampleTime) {double input = *myInput; output += outputSum - kd * dInput;
double error = *mySetpoint - input; if(output > outMax) output = outMax; else if(output < outMin)
output = outMin; *myOutput = outputlastInput = input;
double dInput = (input - lastInput);
lastTime = now;
outputSum+= (ki * error);
return true; }
if(!pOnE) outputSum-= kp * dInput;
else return false;}
PID tuning
■ Normal convention for tuning a PID controller is to Set the Kp gain first and adjust Ki
and Kd according to it.
■ In our system as we are using a light source directly and not sunlight, so the reaction is
very fast so we cant use proportional gain as it gives negative response which results in
oscillations.
■ Same is the case for Derivative gain Kd which is used for future errors.
■ So our system is tuned with and integral gain that uses the past values of error to
remove it.
Results
■ As we change the setpoint (from time 925-1124) the PID changes the output value accordingly to
achieve the require setpoint.
■ Once the Setpoint is fixed (from time 1225-1425 ) the output value is constant.
Conclusion