Using an Arduino to Control an LED Using a Photoresistor
1. Introduction
In
this lecture, we used an Arduino Uno R3 and a photoresistor to sense
the change in ambient light. We used this control structure to change
the brightness of an LED near the photoresistor. Using a feedback loop,
we were able to change the amount of ambient light near the sensor to a
specified value.
2. The Code and the Results
In Figure 1 we can see an example of using the photoresistor and one LED to maintain a steady amount of ambient light near the photoresistor. The "setPoint" is the target amount of ambient light as read through the arduinos analog pin.
Figure 1. An example of using a photoresistor to control an LED
In Figure 2 we can see an example of adding an extra push button controlled LED to the system. When the button LED is lit, the photoresistor controlled LED dims to maintain the "setPoint" of ambient light.
Figure 2. An example of using a photoresistor to control an LED
In Figure 3 we can the resulting plot from running the code above. Python was used to capture and plot this data. Here you can see how the LED dims to maintain the "setPoint". The oscilations is the program reacting to the current ambient light and overcompensating in each direction to maintain an average ambient light. Those oscilations get smaller when the push button LED is activated because the photoresister controlled LED is providing less of the ambient light.
Figure 3. An example of using a photoresistor to control an LED
In
Figure 4 we can see an example of how the system reacts to changing the
'setPoint'. Now the push button is being used to add twenty to the
'setPoint' value. The
system reacts by supplying more power to the LED, which in turn
supplies more ambient light. By checking that the push button is 'LOW'
before adding the twenty, we ensure that the button does not"bounce" and run the function more than once per press.
Figure 4. An example of using a push button to control the 'setPoint' of the feedback loop
In Figure 5 we can see the resulting plot from running the code shown above. Python was used to capture and plot this data. You can see how the LED is being supplied more power on average so the ambient light oscilates around the new 'setPoint'.
Figure 5. The Python code and plot to collect and display data from the code in Task 3
2. Discussion
This
lab was a great look into using basic sensors, and using other programs
to communicate via the serial port with microcontrollers. Using Python
to plot data in a comprehensible way quickly by directly interfacing
with the Arduino was something I'd never considered until this project.
The feedback loop used was also interesting, and an awesome learning
experience to set up.