Introduction

In this lesson, we will show how to use the photoresistor with an Osoyoo UNO, we will monitor the output of a photoresistor, allow the Arduino to know how light or dark it is. When the light falls below a certain level, the Arduino turns on an LED.

Preparations

HARDWARE

About Photoresistor

Photocells are sensors that allow you to detect light. They are small, inexpensive, low-power, easy to use and don’t wear out. For that reason they often appear in toys, gadgets and appliances. They are often referred to as CdS cells (they are made of Cadmium-Sulfide), light-dependent resistors (LDR), and photoresistors.

Photocells are basically a resistor that changes its resistive value (in ohms Ω) depending on how much light is shining onto the squiggly face.When it is dark, the resistance of a photoresistor may be as high as a few MΩ. When it is light, however, the resistance of a photoresistor may be as low as a few hundred ohms.  They are very low cost, easy to get in many sizes and specifications, but are very innacurate. Each photocell sensor will act a little differently than the other, even if they are from the same batch. The variations can be really large, 50% or higher! For this reason, they shouldn’t be used to try to determine precise light levels in lux or millicandela. Instead, you can expect to only be able to determine basic light changes.

This graph indicates approximately the resistance of the sensor at different light levels:

Connection

You connect the components as shown in the diagram below. Connect the LED to pin 9 of the Arduino. The 200 ohm resistor is current limiting resistor. One lead of the photo resistor is connected to 5V, the other to one lead of the 10k ohm resistor. The other lead of the 10k ohm resistor is connected to ground. This forms a voltage divider, whose output is connected to pin A0 of the Arduino.

As the light impinging on the photoresistor gets stronger, the resistance decreases, and the voltage output of the divider increase. The reverse happens, when the impinging light gets weaker.

CODE PROGRAM

After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Graphical Programming software Mixly and follow the next operations:

Connect the light sensor with the A0 analog port of the UNO board. Users can change the analog port to A1 ~ A5 on your own.

In this experiment, we will connect a photoresistor to an Arduino analog input and read the value with the analogRead() function. Depending on the value the Arduino reads, the program will then set pin 9 HIGH or LOW to turn on or turn off the LED night lights. The threshold value is 500. When the analog value read is less than 500, the Arduino will turn the LEDs on. When the analog value it reads is more than 500, the Arduino will turn the LEDs off.

Click Save aftogramming is done. Select the board type and serial port before uploading. For instause a Uno board, just select Arduino/Genuino Uno: if you use a Mega2560, select Arduino/Genuino Mega or Mega2560. 

Select the serial device of the Arduino board from the COM menu. This is likely to be COM3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). To find out, you can disconnect your Arduino board and re-open the menu; the entry that disappears should be the Arduino board. Reconnect the board and select that serial port.

Next,upload the code. If the uploading fails, check and correct the code according to the prompts

Finally, the staus will change to ‘Upload success!’.

Running Result

If the room is lighted, the LEDs should not light. Try getting them to turn on it by covering the photoresistor with your hand. Remove your hand and observe that they turn off again.

In the same time, open the Serial Monitor and you will get the output data as below :

Note:

When you are using the Serial Monitor, please make sure the baudrate setting is same as your sketch definition.