Photocell light meter on an Arduino Uno
A photocell and a 10k resistor on A0 set how many of eight LEDs a 74HC595 lights: none in the dark, all eight in bright light. The whole build, an Arduino Uno and 19 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.
Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- A light sensor in a voltage divider
- Turning an analog reading into a bar graph
- Driving eight LEDs from three pins with a 74HC595
- Building a bit pattern with shifts
- Updating outputs only when something changes
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 21
How it works
A light sensor and a 10k resistor form a voltage divider on A0. More light lowers the sensor's resistance, so the voltage at A0 goes up. The reading rises with the light.
The sketch divides the reading by 114 to get a number of LEDs from 0 to 8. It sends that bar to a 74HC595 shift register, whose eight outputs each drive a yellow LED through a 220 Ω resistor.
The sketch only updates the LEDs and prints when the number of lit LEDs changes.
Parts list
21 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × 74HC595 shift registerIn LED Cube Kit.
- 8 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 8 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × Light sensor
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
Kits that can build it
These 10 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Adafruit MetroX Classic Kit
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 pin | Connects to |
|---|---|
| 74HC595 shift register pin GND | Arduino Uno R3 pin GND |
| 74HC595 shift register pin OE | Arduino Uno R3 pin GND |
| LED, yellow (1) pin C | Arduino Uno R3 pin GND |
| LED, yellow (2) pin C | Arduino Uno R3 pin GND |
| LED, yellow (3) pin C | Arduino Uno R3 pin GND |
| LED, yellow (4) pin C | Arduino Uno R3 pin GND |
| LED, yellow (5) pin C | Arduino Uno R3 pin GND |
| LED, yellow (6) pin C | Arduino Uno R3 pin GND |
| LED, yellow (7) pin C | Arduino Uno R3 pin GND |
| LED, yellow (8) pin C | Arduino Uno R3 pin GND |
| Resistor, 10k Ω pin 2 | Arduino Uno R3 pin GND |
| 74HC595 shift register pin STCP | Arduino Uno R3 pin 6 |
| 74HC595 shift register pin SHCP | Arduino Uno R3 pin 5 |
| 74HC595 shift register pin DS | Arduino Uno R3 pin 4 |
| 74HC595 shift register pin MR | Arduino Uno R3 pin 5V |
| 74HC595 shift register pin VCC | Arduino Uno R3 pin 5V |
| Light sensor pin 1 | Arduino Uno R3 pin 5V |
| Light sensor pin 2 | Arduino Uno R3 pin A0 |
| Resistor, 10k Ω pin 1 | Arduino Uno R3 pin A0 |
| Resistor, 220 Ω (2) pin 1 | 74HC595 shift register pin Q1 |
| Resistor, 220 Ω (3) pin 1 | 74HC595 shift register pin Q2 |
| Resistor, 220 Ω (4) pin 1 | 74HC595 shift register pin Q3 |
| Resistor, 220 Ω (5) pin 1 | 74HC595 shift register pin Q4 |
| Resistor, 220 Ω (6) pin 1 | 74HC595 shift register pin Q5 |
| Resistor, 220 Ω (7) pin 1 | 74HC595 shift register pin Q6 |
| Resistor, 220 Ω (8) pin 1 | 74HC595 shift register pin Q7 |
| Resistor, 220 Ω (1) pin 1 | 74HC595 shift register pin Q0 |
| LED, yellow (1) pin A | Resistor, 220 Ω (1) pin 2 |
| LED, yellow (2) pin A | Resistor, 220 Ω (2) pin 2 |
| LED, yellow (3) pin A | Resistor, 220 Ω (3) pin 2 |
| LED, yellow (4) pin A | Resistor, 220 Ω (4) pin 2 |
| LED, yellow (5) pin A | Resistor, 220 Ω (5) pin 2 |
| LED, yellow (6) pin A | Resistor, 220 Ω (6) pin 2 |
| LED, yellow (7) pin A | Resistor, 220 Ω (7) pin 2 |
| LED, yellow (8) pin A | Resistor, 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.
// Light meter: a photocell on A0 and a bar of eight LEDs on a 74HC595.
//
// photocell 5V -> A0
// 10k resistor A0 -> GND
// 74HC595 DS -> pin 4, SHCP (clock) -> pin 5, STCP (latch) -> pin 6
// Q0 to Q7 -> the eight LEDs, each through 220 ohm to GND
//
// A photocell's resistance falls as light falls on it. With the 10k under it,
// more light pulls A0 up toward 5 V, so the reading rises with the light.
// The sketch turns the reading into a number of LEDs, from none in the dark
// to all eight in bright light, and shifts that bar out to the 74HC595.
const int DATA = 4;
const int CLOCK = 5;
const int LATCH = 6;
int shownBars = -1;
void showBar(int bars) {
// bars ones from the bottom: 0 -> 00000000, 3 -> 00000111, 8 -> 11111111
byte pattern = (bars >= 8) ? 0xFF : (byte)((1 << bars) - 1);
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, MSBFIRST, pattern);
digitalWrite(LATCH, HIGH);
}
void setup() {
pinMode(DATA, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(LATCH, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: photocell light meter");
}
void loop() {
int reading = analogRead(A0);
int bars = reading / 114; // 0..1023 into 0..8
if (bars > 8) bars = 8;
if (bars != shownBars) {
shownBars = bars;
showBar(bars);
Serial.print("reading ");
Serial.print(reading);
Serial.print(", LEDs lit ");
Serial.println(bars);
}
delay(50);
}
Code walkthrough
The key functions of the sketch, and what each one does.
showBar sketch.ino
Turns a count of bars into a byte with that many low bits set. For example, 3 becomes 00000111. It handles 8 separately as 0xFF. It then drops the latch pin, shifts the byte out and raises the latch so all eight LEDs change together.
void showBar(int bars) {
// bars ones from the bottom: 0 -> 00000000, 3 -> 00000111, 8 -> 11111111
byte pattern = (bars >= 8) ? 0xFF : (byte)((1 << bars) - 1);
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, MSBFIRST, pattern);
digitalWrite(LATCH, HIGH);
}setup sketch.ino
Sets the data, clock and latch pins as outputs and opens serial at 9600 with a hello.
void setup() {
pinMode(DATA, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(LATCH, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: photocell light meter");
}loop sketch.ino
Reads A0, divides by 114 and caps the result at 8. If the result differs from shownBars, it updates the bar and prints the reading and how many LEDs are lit. It then waits 50 ms.
void loop() {
int reading = analogRead(A0);
int bars = reading / 114; // 0..1023 into 0..8
if (bars > 8) bars = 8;
if (bars != shownBars) {
shownBars = bars;
showBar(bars);
Serial.print("reading ");
Serial.print(reading);
Serial.print(", LEDs lit ");
Serial.println(bars);
}
delay(50);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The bar never reaches all eight LEDs This project
Your light is not bright enough to push the reading over about 912. Try more light, or change the divisor 114. - No LEDs light at all This project
Check the 74HC595: pin 4 to DS, 5 to SHCP and 6 to STCP, with MR on 5 V and OE on ground. - Outputs show garbage, or the register never clears. 74HC595 shift register
Tie OE low and SRCLR (pin 10) high. Left floating, these pins make the chip behave randomly. Then pulse the latch pin after shifting the byte. - The bits come out in the wrong order, or LEDs are dim. 74HC595 shift register
Choose MSBFIRST or LSBFIRST in shiftOut to flip the order. Give each LED its own resistor, since each output can source only about 6 mA safely. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.