All tutorials

2 · Inputs & sensors

Light sensor (LDR)

Intermediate15 min

Goal

Turn the LED on automatically when the room gets dark.

Why it matters

The voltage divider turns a variable resistance into a measurable voltage.

Steps

  1. 01Add a light sensor on A0.
  2. 02Pick a threshold (e.g. 400) and compare it to the reading.
  3. 03Light the LED below the threshold, switch it off above.
  4. 04Change the light level in the Scenario panel while running.

This tutorial runs inside the Circuitly simulator: your wiring and your code are checked automatically at every step.

Next tutorials