Controlling LEDs with Arduino

Using a Photo Resistor

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This segment goes thru how a photo resistor works, the process of building a circuit with a photo resistor and how to use it to control an LED. Data is also written to the Serial Plotter in the Arduino IDE to graph data.

Keywords

  • photo resistor
  • light sensitivity
  • analog
  • pwm
  • led
  • arduino

About this video

Author(s)
Liz Clark
First online
09 April 2019
DOI
https://doi.org/10.1007/978-1-4842-4883-6_6
Online ISBN
978-1-4842-4883-6
Publisher
Apress
Copyright information
© Liz Clark 2019

Video Transcript

In this section, we’ll continue looking at analog input– this time, of the photoresistor. To follow along, you’ll need the supplies we’ve used so far shown here, as well as at least one photo resistor and a 10k Ohm resistor. Let’s get started.

We can use another simple component to explore analog conceptual Arduino called a photo resistor. A photo resistor, also commonly referred to as a light-dependent resistor, or LDR, is a type of resistor whose resistance is variable depending on the amount of light that it’s exposed to. This means that you can build circuits whose behavior can vary depending on the amount of light available.

Let’s use a photo resistor to control the brightness of an LED. First, let’s build the circuit. We’re going to build an LED circuit as we have in previous projects, attaching the anode to digital pin 9. The photoresistor portion of the circuit will require a 10k resistor, which can be identified with the color code brown, black, orange. This is the same value resistor that we used in the push button segment.

First, insert the photoresistor into the breadboard and attach one leg to the 5 volt rail. Next, attach the remaining leg to the ground rail with a 10k resistor. Then, connect the same leg that is connected to ground to analog pin 0. Basically, one side of the photoresistor is connected to 5 volt power, and the other side is connected to both ground and the analog pin with a 10k resistor.

Adding this 10k resistor creates what is called a voltage divider. A voltage divider does exactly what it sounds like– divides the voltage in the circuit. In this case, it’s dividing the 5 volt signal between the 10k Ohm resistor and the photoresistor. Since the photo resistor’s resistance is variable, the division of the 5 volt signal between both the photoresistor and 10k Ohm resistor will vary as well. The difference between the two resistances is what is sent to the Arduino as an analog value, and what, in turn, is affecting the LED’s brightness.

Now, we’re going to move to the Arduino IDE to write our code. First, we’ll set up our pins to be integers, with integer LED equals 9 and integer photo equals A0. Then, moving onto the setup, we’ll set the pin mode for the LED to be in output with pin mode, LED, output. We can now move to the loop and basically repeat our first piece of code for when we’re working with potentiometers in our last segment.

First, we’re going to create another integer called Val, and have it equal analog read photo. Then, as before, we’re going to map the analog value range to the digital PWN value range with the line Val equals map, Val, 0, 1023, 0, 255. We can finish the loop by sending an analog value to the LED with analog right LED Val, just like we did with our potentiometer code. We can then compile and upload our code to the Arduino.

Take your hand and experiment with blocking different amounts of light over the photoresistor. You should see the LED’s brightness be affected. If you want to really eliminate the amount of light that’s reaching the photoresistor, use something dark in color and dense, like a piece of plastic.

Just as we’ve done in previous segments, we’re going to log our components’ data via serial to double-check our code. However, this time, there will be a bit of a twist. First, in our code setup, we’re going to open serial communication with serial.begin 9600. And then in our loop, we’re going to insert serial.println Val, just as we did in our potentiometer example. This time, though, instead of opening the serial monitor, we’re going to open the serial plotter.

Compile and upload the code to the Arduino, and then go to tools, serial plotter, located directly underneath the serial monitor. As you can see, the plotter has a much different look than the monitor. It allows for real-time data visualization. In this case, it’s showing you the amount of light being absorbed by the photoresistor that is then translated to LED brightness. The plotter can also plot multiple lines of data in a single graph, which we’ll look at in our next segment

In our current example, though, you should see the plotter’s data correspond to both the amount of light you’re allowing to reach the photo resistor and the LED’s brightness. The plotter tends to present analog data in a much more pleasant way, and should be useful to you in future projects.