## Introduction: Photoresistor Sensor With Arduino Uno R3

A photoresistor or photocell is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity; in other words, it exhibits photoconductivity. A photoresistor can be applied in light-sensitive detector circuits, and light- and dark-activated switching circuits.

## Step 2: Principle

The resistance of the photoresistor changes with incident light intensity. If the incident light intensity is high, the resistance reduces; if low, increases.

In this experiment, we will use eight LEDs to indicate light intensity. The higher the light intensity is, the more the LED is lit. When the light intensity is high enough, all the LEDs will be lit. When there is no light, all the LEDs will go out.

## Step 4: Procedures

Step1:

Build the Circuit

Step 2:

Download the code from https://github.com/primerobotics/Arduino

Step 3:

Upload the sketch to the Arduino Uno board

Click the Upload icon to upload the code to the control board. If "Done uploading" appears at the bottom of the window, it means the sketch has been successfully uploaded.

Now, if you shine the photoresistor with a certain light intensity, you will see several LEDs light up. If you increase the light intensity, you will see more LEDs light up. When you place it in dark environment, all the LEDs will go out.

## Step 5: Code

/************************************

* name:Photoresistor

* function: if you shine the photoresistor with a certain light intensity, you will see several LEDs light up.

* If you increase the light intensity, you will see more LEDs light up.

* When you place it in dark environment, all the LEDs will go out.

*********************************************

/Email: info@primerobotics.in

//Website: www.primerobotics.in

const int NbrLEDs = 8;//8 leds

const int ledPins[] = {5, 6, 7, 8, 9, 10, 11, 12};//8 leds attach to pin 5-12 respectively

const int photocellPin = A0; //photoresistor attach to A0

int sensorValue = 0; // value read from the sensor

int ledLevel = 0; // sensor value converted into LED 'bars'

void setup()

{

for (int led = 0; led < NbrLEDs; led++)

{

pinMode(ledPins[led], OUTPUT);// make all the LED pins outputs

}

}

void loop()

{

sensorValue = analogRead(photocellPin); //read the value of A0

ledLevel = map(sensorValue, 300, 1023, 0, NbrLEDs); // map to the number of LEDs

for (int led = 0; led < NbrLEDs; led++)//

{

if (led < ledLevel ) //When led is smaller than ledLevel, run the following code.

{

digitalWrite(ledPins[led], HIGH); // turn on pins less than the level

}

else

{

digitalWrite(ledPins[led],LOW); // turn off pins higher than

}

}

}