The Light Sensor

Learn to read a light sensor and light an LED in the dark using a threshold.

LESSON COMPASS

What will you use this page for?

Core idea

A light sensor measures how much light falls on it and turns that into an electrical value; by reading this value we can decide "is it dark or light?"

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Getting the logic backwards An LDR usually gives a small value in the dark, not a large one, in most circuits (because as resistance rises the voltage read can drop). If your code does not work, first actually measure your bright and dark values and write them down, then decide whether you need < or > . Choosing the…

Next connection

The Temperature Sensor

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteDigital and Analog Signals
ContentStandard lesson · 1,501 words
Last updated

One-sentence summary

A light sensor measures how much light falls on it and turns that into an electrical value; by reading this value we can decide "is it dark or light?"

Why does it matter?

Many devices around us actually "sense" light. Street lamps switch on by themselves when it gets dark. Your phone screen gets brighter in sunlight and dims in a dark room. Garden sprinklers, automatic doors and toy robots also change their behaviour based on the light around them.

In the previous lesson we saw that signals can be digital (only on/off) or analog (changing gradually). A light sensor is a perfect example of this: light does not disappear all at once, it fades or grows step by step. That is why we usually read it as an analog value. By the end of this lesson you will know how to read a value from a sensor and how to set a threshold (a limit) to make a decision. This is the first step in teaching your robotics projects to "sense" their surroundings.

What is a light sensor and how does it work?

LDR: a resistance that changes with light

The most common light sensor is the LDR. It stands for "Light Dependent Resistor". Sometimes it is called a "photoresistor". It is a small, flat part with a wavy pattern on top.

The LDR's job rests on a single idea:

Resistance is like how hard it is for electricity to pass through. When resistance is high, current struggles to flow; when it is low, current flows easily. Because the LDR changes its resistance with light, the electrical value in the circuit it is connected to changes too. Our board reads this change.

Turning the value into a number

Boards (like the micro:bit or Arduino) do not read resistance directly in "ohms". Instead, we connect the LDR into a circuit and read a number from an analog input pin on the board. This number is usually in a range:

What matters is not the exact number but the logic: we read one extreme value in bright light and another extreme value in the dark. When the value grows or shrinks, we know the light is increasing or decreasing.

Reading the sensor: the analog value

Everyday example: a night light

Think of a night light. When there is light in the room it stays off, and when the room gets dark it turns on. Inside it there is a small light sensor. It keeps measuring the surroundings and checks "is it dark enough yet?"

We can write the logic as pseudocode like this:

Start
Repeat forever:
  light_value = read the light sensor
  If light_value < threshold
    turn on the LED
  Otherwise
    turn off the LED

Here threshold is a limit number that we choose. Values below it mean "dark", values above it mean "light".

How do you choose the threshold?

The threshold is not a ready-made number; we find it by testing. The method is simple:

  1. First read the sensor in a bright room and write the value down.
  2. Then cover the sensor with your hand and write down the value in the dark.
  3. Pick a number close to the middle of the two. That becomes your threshold.

For example, if you read 800 in bright light and 200 when you cover it, you might choose a threshold around 450–500.

micro:bit style reading

The micro:bit has a built-in way to measure light on its own display (it uses its LEDs like a sensor). Instead of blocks, we can see the logic in text code:

from microbit import *

while True:
    light = display.read_light_level()   # a value between 0 and 255
    if light < 50:                        # 50 is the threshold we chose
        display.show(Image.HEART)         # show a heart in the dark
    else:
        display.clear()                   # clear the screen in the light
    sleep(200)

Arduino style reading

On Arduino we connect the LDR to an analog pin and read it with analogRead:

threshold = 500

Repeat forever:
  light_value = read pin A0       // between 0 and 1023
  If light_value < threshold
    turn on LED number 13         // dark: switch the light on
  Otherwise
    turn off LED number 13        // light: switch the light off
  wait 200 milliseconds

Notice that the logic is the same in the micro:bit and Arduino versions. Only the number range and pin names change. The thing you need to learn is the logic: read, compare, decide.

Mini activity

Design a "smart street lamp" that works on paper. It needs no parts; just write the logic.

  1. Assume your sensor reads 900 in bright light and 150 in the dark.
  2. Choose a threshold value and write one sentence about why you picked it.
  3. Complete the pseudocode by filling in the blanks:
threshold = ____

Repeat forever:
  value = read the light sensor
  If value < threshold
    ____ the lamp
  Otherwise
    ____ the lamp
  1. Now reverse the rule: how would you change the code for a warning light that is on in the light and off in the dark? (Hint: think about the < sign.)

You can also try a real LDR together with an adult. Print the sensor's value to the screen, move your hand over it and watch how the number changes.

Common mistakes

Getting the logic backwards

An LDR usually gives a small value in the dark, not a large one, in most circuits (because as resistance rises the voltage read can drop). If your code does not work, first actually measure your bright and dark values and write them down, then decide whether you need < or >.

Choosing the threshold randomly

Instead of saying "500 is probably fine", take a measurement in your own room. Light conditions are different everywhere; your threshold is specific to your room.

Forgetting to cover the sensor

When testing, if you do not block the light with your finger, you will see the value never changes. To see the change, always try both a bright and a dark state.

Not adding a wait

If you read and print the value thousands of times per second, the numbers will scroll by unreadably. Adding a small wait (for example 200 ms) makes the reading calmer.

Safety note

Lesson summary

Check questions

  1. What does LDR stand for and what does it measure?
  2. In the dark, does an LDR's resistance increase or decrease?
  3. Why do we read a light sensor's value as analog rather than digital?
  4. What is a "threshold" and how do we decide on it?
  5. How would you change the comparison in the pseudocode for a light that is on in bright light and off in the dark?

Answers

  1. LDR stands for "Light Dependent Resistor". It measures how much light falls on it (by changing its resistance according to the light).
  2. In the dark its resistance increases; in bright light it decreases.
  3. Because light does not change all at once but gradually. An analog reading shows the difference between "a little dark" and "very dark" as a range of numbers.
  4. A threshold is the limit number we choose between "dark" and "light". We decide on it by measuring the bright and dark values and picking a number close to the middle.
  5. We reverse the comparison: instead of value < threshold we write value > threshold. That way the lamp turns on when the light increases.

Source and verification note

For “The Light Sensor”, verification focuses on whether the relationship between What is a light sensor and how does it work? and Turning the value into a number remains consistent across examples. Sensor readings can change with the model, supply voltage and environment. Thresholds in the lessons are therefore examples; a real project should use a measurement table and calibration.

Next lesson

The Temperature Sensor

Start QuizBack to Sensors and Actuators
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.