| Materials | Quantity |
| Arduino Elegoo Uno R3 | 1 |
| Breadboard | 1 |
| Jumper wires | 20 |
| Photocell | 1 |
| LEDs | 2 |
| 1k Resistor | 1 |
| 200 to 1k Resistor |
1 |
| 2k to 10k Resistor |
1 |





| Light Source | Light Intensity |
| Ambient lighting (Microcontroller lab) | 206 |
| Ambient lighting and the white LED | 798 |






// Test 3: Photocell PID with setPoint variableFigure 12: Arduino code for setPointChange() function
float Kp=0.1, Ki=0, Kd=0;
float error=0, previous_error=0;
float P=0, I=0, D=0;
float PID_value=0;
int setPoint=502;
float initialValue=10;
int buttonState;
int lastButtonState;
void setup() {
pinMode(3,OUTPUT);
pinMode(4,INPUT);
analogWrite(3,initialValue);
Serial.begin(9600);
}
void loop() {
setPointChange();
int photoSignal=analogRead(A0);
error=setPoint-photoSignal;
P=error;
I+=error;
D=error-previous_error;
previous_error=error;
PID_value=PID_value+Kp*P+Ki*I+Kd*D;
analogWrite(3,PID_value);
Serial.print(setPoint);
Serial.print(", ");
Serial.println(photoSignal);
}
void setPointChange(){ // state detection (edge detection) works.
buttonState=digitalRead(7);
if (buttonState!=lastButtonState){
delay(10);
if (buttonState==HIGH){
if (setPoint<900){
setPoint+=20;
}
}
}
lastButtonState=buttonState;
}

| import matplotlib.pyplot as plt import numpy x = [setpoint_data] x2 = [ambient_lighting_data] y = numpy.arange(len(x)) plt.plot(y, x, y, x2) plt.table("PID Photocell Resistor Plotter - setchange() function") plt.xlabel("Time (microseconds)", fontsize=14) plt.ylabel("Ambient Lighting", fontsize=14) |
