Built-in project · Arduino Uno R3

Photocell bar graph on a 74HC595, on an Arduino Uno

A photocell and a 1k resistor on A0 set how many of eight LEDs a 74HC595 lights, with data on pin 12, clock on 9 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 Runs in your browser. Free, and no account needed.

Photocell bar graph on a 74HC595 · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

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
// Photocell light meter: the brighter it is, the more LEDs light.
//
//   74HC595 DS (pin 14)    -> Uno pin 12   (data)
//   74HC595 SHCP (pin 11)  -> Uno pin 9    (clock)
//   74HC595 STCP (pin 12)  -> Uno pin 11   (latch)
//   74HC595 VCC and MR -> 5V      GND and OE -> GND
//   Q0 to Q7 -> eight LEDs, each through 220 ohm to GND
//   5V -> photocell -> A0 -> 1k resistor -> GND
//
// A photocell's resistance falls as light lands on it. With it on top of a
// 1k resistor, A0 reads more in bright light. The sketch maps the reading,
// 0 to 1023, onto a count of LEDs, 0 to 8, and lights that many.
//
// Click the photocell and drag its light level to see the bar move.

const int LIGHT_PIN = A0;
const int DATA_PIN = 12;
const int CLOCK_PIN = 9;
const int LATCH_PIN = 11;

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(CLOCK_PIN, OUTPUT);
  pinMode(LATCH_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: photocell on A0, eight LEDs on a 74HC595");
}

void loop() {
  int reading = analogRead(LIGHT_PIN);
  int count = map(reading, 0, 1023, 0, 9);
  if (count > 8) count = 8;
  byte bar = 0;
  for (int i = 0; i < count; i++) bitSet(bar, i);
  show(bar);
  if (count != shown) {
    shown = count;
    Serial.print("light ");
    Serial.print(reading);
    Serial.print(": ");
    Serial.print(count);
    Serial.println(" LEDs");
  }
  delay(100);
}

Parts list

21 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

22 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; 74HC595 shift register pin GND; 74HC595 shift register pin OE; 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, 1k Ω pin 2
  • Arduino Uno R3 pin 12; 74HC595 shift register pin DS
  • Arduino Uno R3 pin 11; 74HC595 shift register pin STCP
  • Arduino Uno R3 pin 9; 74HC595 shift register pin SHCP
  • Arduino Uno R3 pin 5V; 74HC595 shift register pin MR; 74HC595 shift register pin VCC; Light sensor pin 1
  • Arduino Uno R3 pin A0; Light sensor pin 2; Resistor, 1k Ω pin 1
  • 74HC595 shift register pin Q1; Resistor, 220 Ω (2) pin 1
  • 74HC595 shift register pin Q2; Resistor, 220 Ω (3) pin 1
  • 74HC595 shift register pin Q3; Resistor, 220 Ω (4) pin 1
  • 74HC595 shift register pin Q4; Resistor, 220 Ω (5) pin 1
  • 74HC595 shift register pin Q5; Resistor, 220 Ω (6) pin 1
  • 74HC595 shift register pin Q6; Resistor, 220 Ω (7) pin 1
  • 74HC595 shift register pin Q7; Resistor, 220 Ω (8) pin 1
  • 74HC595 shift register pin Q0; Resistor, 220 Ω (1) 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.