Arduino Photoresistor Tutorial

Learn how to use photoresistor with Arduino and how to write program

A photoresistor is a sensor that changes the its resistance to current when amount of light falling on it changes. Photoresistor are also called photocell and LDR(Light Dependent Resistor). Shown below is a typical photoresistor that you can buy anywhere in the electronics component stores.

In this arduino photocell tutorial, you will learn how to use a photocell as a sensor to turn on and off a LED. You will also learn how to write A code for doing the same.

Circuit Diagram

Connect the circuit as shown in the following diagram:

Arduino Photocell Tutorial

The Photoresistor is connected at one end to the +5V pin of the arduino uno and the other end is connected to 10KOhm resistor. The other end of the 10KOhm resistor is connected to the digital pin 2 of the Arduino UNO. A polarized 100uF capacitor is connected to the junction of the 10KOhm resistor and digital pin 2 wire. The other negative terminal of the capacitor is grounded. Two LEDs red connected to digital pin 12 via 330Ohm resistor and green LED connected to pin 13 are used to indicate that there is no light or insufficient light and that there is light. Hence this is a small sensor project in which the LEDs indicate the presence or absence of light. The actual physical hardware setup is shown below.

Arduino Photocell Program code

Below is the arduino program code that detects the light amount and turns on or off the LEDs accordingly.

In the first few lines of the code we have assigned identifiable names to the pins we are going to be using. Then we have set the pin 2 which is our sensor pin to 0 to initialize to a known state. In the setup() function we have just configured the pins as input or output.

In the loop() function we create the logic to read the light amount state and turn on or off LED. We have used the digitalRead() function to read the state of the lightsensor pin to which the photoresistor(LDR) is connected to and saved it in variable sensorState. Then depending on the state of the variable sensorState, we have turned on or off the Red and Green light.

Compile and upload the above program code to Arduino. Then you put your hand over the photocell for a moment and the red LED should turns on. After a few seconds, the red LED will turn off, and the green LED will turn on.

See also LED blinking C program for Arduino.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

Dot Vs Arrow Operator and Char pointer in Structure C programming tutorial

Arduino UNO I2C Example Project

Arduino UNO I2C Example Project