Lesson 4 Photoresistor

Share for us

Introduction

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 darkness-activated switching circuits.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * Photoresistor

– 1 * Resistor (10KΩ)

– 8 * LED

– 8 * Resistor (220Ω)

– Jumper wires

-1 * Breadboard

Experimental Principle

The resistance of a photoresistor changes with incident light intensity. If the light intensity gets higher, the resistance decreases; if it gets lower, the resistance increases.

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

Experimental Procedures

Step 1: Build the circuit

The schematic diagram

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorials on our website)

Step 3: Compile the code

Step 4: Upload the sketch to the SunFounder Uno board

Now, shine some light on the photoresistor, and you will see several LEDs light up. Shine more light and you will see more LEDs light up. When you place it in a dark environment, all the LEDs will go out.

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: support@sunfounder.com
//Website: www.sunfounder.comconst 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 )    {
      digitalWrite(ledPins[led], HIGH); // turn on pins less than the level
    }
    else    {
      digitalWrite(ledPins[led],LOW); // turn off pins higher than
      // the level
    }
  }
}

Video