LED bar graph using shift register and potentiometer

Main Diagram

HARDWARE REQUIRED:

  • PICUNO Microcontroller board
  • 8 × LEDs
  • 8 × 220Ω resistors
  • 1 × SN74HC595 Shift Register
  • 1 × 10kΩ Potentiometer
  • Breadboard
  • Jumper wires
  • USB cable

DESCRIPTION:

This project reads an Analog voltage from a potentiometer and displays it as a bar graph on 8 LEDs using an SN74HC595 shift register. The Analog input (0--3.3V) is converted into a value from 0 to 8, and that number of LEDs are turned ON to visually represent the level.

CIRCUIT DIAGRAM:

Main Diagram
  • Connect the PICUNO board to the computer using a USB cable.
  • Connect the SN74HC595 Pins 1 -- 7, 15 to 8×220Ω resistors where each of them is connected anode of 8 LEDs.
  • Connect the cathode of all LEDs to GND.
  • Connect Pins 8 and 13 to GND.
  • Connect Pins 10 and 16 to 3.3 V.
  • Connect Pins 11, 12, 14 to GPIO 7, 8, 6 respectively.
  • Connect outer terminals of the potentiometer to VCC and GND, centre terminal to Analog pin A0 (Pin 26 in PICUNO)

SCHEMATIC:

SN74HC595 Pin 1--7, 15 (Q0--Q7) → 8x 220-ohm resistors → LEDs anode

LEDs cathode → GND

Pin 8 → GND

Pin 16 → VCC (3.3 V)

Pin 10 (MR) → Connect to VCC (keep shift register active)

Pin 13 (OE) → Connect to GND (output enable active)

Pin 11 (SRCLK/Clock) → Connect to GPIO 7

Pin 12 (RCLK/Latch) → Connect to GPIO 8

Pin 14 (SER/Data) → Connect to GPIO 6

Potentiometer Outer Terminals → VCC, GND

Potentiometer Centre Terminal → A0

CODE -- C:

int dataPin = 6;
int clockPin = 7;
int latchPin = 8;
int potPin = A0;

void setup() {
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int analogValue = analogRead(potPin);
  int level = map(analogValue, 0, 1023, 0, 9);
  byte barValue = 0;

  for (int i = 0; i < level; i++) {
    barValue |= (1 << i);
  }

  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, barValue);
  digitalWrite(latchPin, HIGH);

  Serial.print(\"Analog: \"); Serial.print(analogValue);
  Serial.print(\" Level: \"); Serial.println(level);

  delay(100);
}
analogRead(potPin) - Reads voltage from the potentiometer.

map(...) - Converts analog value (0--1023) to a level (0--8).

barValue |= (1 << i) - Sets each bit one by one.

shiftOut(...) - Sends the final LED pattern to the shift register.

CODE -- PYTHON:

from machine import Pin, ADC
import time

dataPin = Pin(6, Pin.OUT)
clockPin = Pin(7, Pin.OUT)
latchPin = Pin(8, Pin.OUT)
pot = ADC(0) # A0 pin on PicUNO

def shiftOut(value):
    for i in range(7, -1, -1):
        dataPin.value((value >> i) & 1)
        clockPin.value(1)
        time.sleep_us(1)
        clockPin.value(0)

while True:
    analog = pot.read_u16()
    level = analog * 9 // 65536
    bar = 0

    for i in range(level):
        bar |= (1 << i)

    latchPin.value(0)
    shiftOut(bar)
    latchPin.value(1)

    voltage = analog * 3.3 / 65535
    print(\"Analog:\", analog, \"Voltage:\", round(voltage, 2), \"V Level:\", level)

    time.sleep(0.1)
ADC(0) - Reads analog input from A0 pin.

read_u16() - Returns a 16-bit analog value (0--65535).

level = analog * 9 // 65536 - Scales the value to 0--8.

shiftOut() - Sends the LED pattern to the register.