Built-in project · Arduino Uno R3

Light meter: photoresistor on A0, LEDs on pins 5 to 12, on an Arduino Uno

A photoresistor and a 10k resistor on A0 set how many of eight LEDs on pins 5 to 12 light: none in the dark, all of them in bright light. The whole build, an Arduino Uno and 18 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • A voltage divider with a light sensor
  • Mapping a reading to a bar of LEDs
  • Lighting a number of LEDs with a loop
  • Printing only when something changes
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 20

How it works

A light sensor (a photoresistor) from 5V to A0 and a 10k resistor from A0 to ground make a voltage divider. More light lowers the photoresistor's resistance, so A0 reads higher in bright light.

The sketch uses map to turn the reading, 0 to 1023, into a number of LEDs from 0 to 8. It lights that many yellow LEDs, starting at pin 5 and going up to pin 12, each through a 220 ohm resistor.

The serial monitor prints a line only when the count changes. Drag the light level on the sensor to see the bar grow and shrink.

Parts list

20 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
LED, yellow (1) pin CArduino Uno R3 pin GND
LED, yellow (2) pin CArduino Uno R3 pin GND
LED, yellow (3) pin CArduino Uno R3 pin GND
LED, yellow (4) pin CArduino Uno R3 pin GND
LED, yellow (5) pin CArduino Uno R3 pin GND
LED, yellow (6) pin CArduino Uno R3 pin GND
LED, yellow (7) pin CArduino Uno R3 pin GND
LED, yellow (8) pin CArduino Uno R3 pin GND
Resistor, 10k Ω pin 2Arduino Uno R3 pin GND
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 12
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 11
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 10
Resistor, 220 Ω (4) pin 1Arduino Uno R3 pin 9
Resistor, 220 Ω (5) pin 1Arduino Uno R3 pin 8
Resistor, 220 Ω (6) pin 1Arduino Uno R3 pin 7
Resistor, 220 Ω (7) pin 1Arduino Uno R3 pin 6
Resistor, 220 Ω (8) pin 1Arduino Uno R3 pin 5
Light sensor pin 2Arduino Uno R3 pin 5V
Light sensor pin 1Arduino Uno R3 pin A0
Resistor, 10k Ω pin 1Arduino Uno R3 pin A0
LED, yellow (1) pin AResistor, 220 Ω (1) pin 2
LED, yellow (2) pin AResistor, 220 Ω (2) pin 2
LED, yellow (3) pin AResistor, 220 Ω (3) pin 2
LED, yellow (4) pin AResistor, 220 Ω (4) pin 2
LED, yellow (5) pin AResistor, 220 Ω (5) pin 2
LED, yellow (6) pin AResistor, 220 Ω (6) pin 2
LED, yellow (7) pin AResistor, 220 Ω (7) pin 2
LED, yellow (8) pin AResistor, 220 Ω (8) pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A light meter: the more light, the more of the eight LEDs light.
//
//   5V -> photoresistor -> A0 -> 10k resistor -> GND
//   pins 5 to 12 -> one 220 ohm resistor and one LED each -> GND
//
// The photoresistor's resistance falls as light lands on it, so A0 reads
// more in bright light. map() turns the reading, 0 to 1023, into a count of
// LEDs, 0 to 8, and the loop lights that many from pin 5 up.
//
// Drag the photoresistor's light level and watch the bar.

const int SENSOR = A0;
const int LEDS[] = {5, 6, 7, 8, 9, 10, 11, 12};
const int COUNT = 8;

int shown = -1;

void setup() {
  for (int i = 0; i < COUNT; i++) pinMode(LEDS[i], OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: light meter on A0");
}

void loop() {
  int reading = analogRead(SENSOR);
  int level = map(reading, 0, 1023, 0, COUNT + 1);
  if (level > COUNT) level = COUNT;
  for (int i = 0; i < COUNT; i++) digitalWrite(LEDS[i], i < level ? HIGH : LOW);
  if (level != shown) {
    shown = level;
    Serial.print("light ");
    Serial.print(reading);
    Serial.print(": ");
    Serial.print(level);
    Serial.println(" LEDs");
  }
  delay(50);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Sets all eight pins in LEDS as outputs with a loop, then starts serial at 9600 baud and prints a startup line.

void setup() {
  for (int i = 0; i < COUNT; i++) pinMode(LEDS[i], OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: light meter on A0");
}

loop sketch.ino

Reads A0 and maps it to level using COUNT + 1 as the top, then caps it at COUNT. A loop turns on every LED whose index is below level and turns off the rest. If level changed, it prints the reading and the number of LEDs. It waits 50 ms between passes.

void loop() {
  int reading = analogRead(SENSOR);
  int level = map(reading, 0, 1023, 0, COUNT + 1);
  if (level > COUNT) level = COUNT;
  for (int i = 0; i < COUNT; i++) digitalWrite(LEDS[i], i < level ? HIGH : LOW);
  if (level != shown) {
    shown = level;
    Serial.print("light ");
    Serial.print(reading);
    Serial.print(": ");
    Serial.print(level);
    Serial.println(" LEDs");
  }
  delay(50);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The bar is always full or always empty This project
    Check the divider. The photoresistor goes from 5V to A0 and the 10k resistor from A0 to GND.
  • Bright light lights fewer LEDs This project
    If you swap the photoresistor and the 10k resistor, the reading reverses. Put the sensor on the 5V side.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
  • The reading barely changes, or sits stuck near 0 or the top of the range. Light sensor
    An LDR is a resistor, so use it in a divider with a fixed resistor, about 10k, and read the middle node. Without the divider the analog pin sees nothing useful.
  • Readings go the wrong way: higher in the dark. Light sensor
    Which side the LDR sits on decides the direction. LDR on the supply side gives a higher reading in the light. LDR on the ground side gives a lower reading in the light. Swap them if needed.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.