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 Runs in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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);
}
Parts list
20 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 8 × Resistor, 220 Ω
- 8 × LED, yellow
- 1 × Light sensor
- 1 × Resistor, 10k Ω
How it is wired
19 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; LED, yellow (1) pin C; LED, yellow (2) pin C; LED, yellow (3) pin C; LED, yellow (4) pin C; LED, yellow (5) pin C; LED, yellow (6) pin C; LED, yellow (7) pin C; LED, yellow (8) pin C; Resistor, 10k Ω pin 2
- Arduino Uno R3 pin 12; Resistor, 220 Ω (1) pin 1
- Arduino Uno R3 pin 11; Resistor, 220 Ω (2) pin 1
- Arduino Uno R3 pin 10; Resistor, 220 Ω (3) pin 1
- Arduino Uno R3 pin 9; Resistor, 220 Ω (4) pin 1
- Arduino Uno R3 pin 8; Resistor, 220 Ω (5) pin 1
- Arduino Uno R3 pin 7; Resistor, 220 Ω (6) pin 1
- Arduino Uno R3 pin 6; Resistor, 220 Ω (7) pin 1
- Arduino Uno R3 pin 5; Resistor, 220 Ω (8) pin 1
- Arduino Uno R3 pin 5V; Light sensor pin 2
- Arduino Uno R3 pin A0; Light sensor pin 1; Resistor, 10k Ω pin 1
- Resistor, 220 Ω (1) pin 2; LED, yellow (1) pin A
- Resistor, 220 Ω (2) pin 2; LED, yellow (2) pin A
- Resistor, 220 Ω (3) pin 2; LED, yellow (3) pin A
- Resistor, 220 Ω (4) pin 2; LED, yellow (4) pin A
- Resistor, 220 Ω (5) pin 2; LED, yellow (5) pin A
- Resistor, 220 Ω (6) pin 2; LED, yellow (6) pin A
- Resistor, 220 Ω (7) pin 2; LED, yellow (7) pin A
- Resistor, 220 Ω (8) pin 2; LED, yellow (8) pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.