Chapter 10 – Experiment 03 – LIGHT SENSOR

In a famous film (for whom is not a geek, the film is Star Wars) they said “Luke use the source”, in this experiment we will use the source “light” to have some nice results. For the first time we will use a sensor that allows us to interact with the external environment, a small component called photo-resistor based on a light resistance, it will sense the light and will allow the microcontroller to react and change the intensity of a small LED. The photo-resistor creates a different resistance based on the intensity of the light. Changing the resistance it changes the result voltage. The microcontroller reads the different voltage and will light up the LED with more or less intensity. A low resistance value will occur when the sensor is well lit and a high resistance value will occur when it is in darkness.

The schema

int photocellPin = 7;
int photocellOutput;

int led = 12;

void setup(void)
{
Serial.begin(9600);
pinMode(led, OUTPUT);
}

void loop(void)
{
// digitalWrite(led, LOW);   // turn the LED on (HIGH is the voltage level)

photocellOutput = analogRead(photocellPin);

Serial.print(“Raw data = “);
Serial.println(photocellOutput); // raw data from the photocell

if (photocellOutput < 1020) {
digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
} else {
digitalWrite(led, LOW);   // turn the LED on (HIGH is the voltage level)
}

delay(1);
}

NOT WORKING? (3 things to try)

LED Remains Dark
This is a mistake we continue to make time and time again, if only they could make an LED that worked both ways. Pull it up and give it a twist.

It Isn’t Responding to Changes in Light.
Given that the spacing of the wires on the photo-resistor is not standard, it is easy to misplace it. Double check if it is its in the right place.

Still not really quite working?
You may be in a room which is either too bright or too dark. Try turning the lights on or off to see if this helps. Or if you have a flashlight nearby give that a try.

MAKING IT BETTER

Reverse the response:
Perhaps you would like the opposite response. Don’t worry we can easily reverse this response just change: digitalWrite(led, HIGH); to LOW and digitalWrite(led, LOW); to HIGH

Upload and watch the response change

2 comments

  1. [...] Chapter 08 – Experiment 01 – BLINKING LED Chapter 09 – Experiment 02 – TRAFFIC LIGHTS Chapter 10 – Experiment 03 – LIGHT SENSOR Chapter 11 – Experiment 04 – TEMPERATURE SENSOR Chapter 12 – Experiment 05 – PUSHBUTTON [...]

  2. int lightPin = 7; //PhotoResistor Pin
    int ledPin = 12; // Led Pin

    void setup()
    {
    pinMode(ledPin, OUTPUT); //sets the led pin to output
    digitalWrite(ledPin, HIGH); //turns on led
    }

    void loop()
    {
    int threshold = 3;
    if (analogRead(lightPin) > threshold) {
    digitalWrite(ledPin, LOW);
    }
    else {
    digitalWrite(ledPin, HIGH);
    }
    delay(50);
    }

Leave a Reply to Dashamir Hoxha Cancel reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


istanbul evden eve nakliyat istanbul evden eve nakliyat istanbul evden eve nakliyat sarıyer evden eve nakliyat beylikdüzü evden eve nakliyat şişli evden eve nakliyat