Photoresistor bar graph on a 74HC595 (pins 9, 12 and 11), on an Arduino Uno
A photoresistor and a 1k resistor on A0 set how many of eight LEDs light, through a 74HC595 with data on pin 9, clock on 12 and latch on 11. 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
- Light sensing with a voltage divider
- Using a 74HC595 to add output pins
- Dividing a reading into bands
- Building a bit pattern with
bitSet
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 21
How it works
A photoresistor and a 1k resistor split 5V and feed A0. In bright light the photoresistor's resistance drops, so the reading rises. The sketch divides the reading by 57 to get a count of lit LEDs.
A 74HC595 shift register drives eight LEDs from three pins: data on 9, latch on 11 and clock on 12. The count lights LEDs from Q0 upward.
When the photoresistor is down to 1k, A0 reads about half of 1023, and all eight LEDs are on. Click the photoresistor in the simulator and drag its light level to try it.
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, 1k Ω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, 1k Ω pin 2 | Arduino Uno R3 pin GND |
| 74HC595 shift register pin SHCP | Arduino Uno R3 pin 12 |
| 74HC595 shift register pin STCP | Arduino Uno R3 pin 11 |
| 74HC595 shift register pin DS | Arduino Uno R3 pin 9 |
| 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, 1k Ω 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.
// Photoresistor light meter: the brighter it is, the more LEDs light.
//
// 74HC595 DS (pin 14) -> Uno pin 9 (data)
// 74HC595 STCP (pin 12) -> Uno pin 11 (latch)
// 74HC595 SHCP (pin 11) -> Uno pin 12 (clock)
// 74HC595 VCC and MR -> 5V GND and OE -> GND
// Q0 to Q7 -> eight LEDs, each through 220 ohm to GND
// 5V -> photoresistor -> A0 -> 1k resistor -> GND
//
// A photoresistor's resistance falls as light lands on it. Sitting on top of
// a 1k resistor, it makes A0 read more in bright light. When the cell is down
// to 1k, A0 reads half of 1023, and that is where all eight LEDs light: the
// reading is cut into nine bands of 57, from none lit to all eight.
//
// Click the photoresistor and drag its light level to see the bar move.
const int LIGHT_PIN = A0;
const int DATA_PIN = 9;
const int LATCH_PIN = 11;
const int CLOCK_PIN = 12;
int shown = -1;
void show(byte value) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, value); // bit 0 lands on Q0
digitalWrite(LATCH_PIN, HIGH);
}
void setup() {
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: photoresistor on A0, 74HC595 on pins 9, 11 and 12");
}
void loop() {
int reading = analogRead(LIGHT_PIN);
int lit = reading / 57;
if (lit > 8) lit = 8;
byte leds = 0;
for (int i = 0; i < lit; i++) bitSet(leds, i);
show(leds);
if (lit != shown) {
shown = lit;
Serial.print("reading ");
Serial.print(reading);
Serial.print(", LEDs lit ");
Serial.println(lit);
}
delay(50);
}
Code walkthrough
The key functions of the sketch, and what each one does.
show sketch.ino
Sets LATCH_PIN LOW, clocks the byte out with shiftOut, then sets the latch HIGH so the outputs update together. Bit 0 lands on Q0.
void show(byte value) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, value); // bit 0 lands on Q0
digitalWrite(LATCH_PIN, HIGH);
}setup sketch.ino
Makes the data, latch and clock pins outputs and opens serial at 9600. Then it prints a line naming the pins in use.
void setup() {
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: photoresistor on A0, 74HC595 on pins 9, 11 and 12");
}loop sketch.ino
Reads A0 and computes lit as reading / 57, capped at 8. That cuts the range into nine bands, from none lit to all eight. It builds the byte with bitSet, sends it with show, and prints when the count changes.
void loop() {
int reading = analogRead(LIGHT_PIN);
int lit = reading / 57;
if (lit > 8) lit = 8;
byte leds = 0;
for (int i = 0; i < lit; i++) bitSet(leds, i);
show(leds);
if (lit != shown) {
shown = lit;
Serial.print("reading ");
Serial.print(reading);
Serial.print(", LEDs lit ");
Serial.println(lit);
}
delay(50);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LEDs do not respond to light This project
Check that A0 connects to the junction between the photoresistor and the 1k resistor, and that the other ends go to 5V and ground. - The LED bar looks scrambled This project
This build uses data on pin 9 and clock on pin 12. Copying wiring from another project with those two swapped gives wrong patterns. - All eight LEDs light too easily This project
That is by design. Once A0 reads about 456, all eight are on. Change the 57 inloopto spread the range differently. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.