Binary counter using SN74HC595 shift register

HARDWARE REQUIRED:
- PICUNO Microcontroller board
- 8 × LEDs
- 8 × 220Ω resistors
- 1 × SN74HC595 Shift Register
- Breadboard
- Jumper wires
- USB cable
DESCRIPTION:
The microcontroller counts from 0 to 255 in a loop. Each count value is an 8-bit binary number that gets sent to the SN74HC595. The shift register updates the state of 8 LEDs, visually showing the binary form of the count. Additionally, the value of the binary number is printed to the serial monitor on the laptop via USB.
CIRCUIT DIAGRAM:
[Fritzing image to be added here]
- 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.
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
CODE -- C:
int dataPin = 6;
int clockPin = 7;
int latchPin = 8;
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
for (int i = 0; i <= 255; i++) {
digitalWrite(latchPin, LOW); // Start data transmission
shiftOut(dataPin, clockPin, MSBFIRST, i); // Send 8-bit data
digitalWrite(latchPin, HIGH); // Latch data to outputs
Serial.print(\"Count: \");
Serial.println(i, BIN); //
delay(300); // Wait before next count
}
}
int clockPin = 7;
int latchPin = 8;
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
for (int i = 0; i <= 255; i++) {
digitalWrite(latchPin, LOW); // Start data transmission
shiftOut(dataPin, clockPin, MSBFIRST, i); // Send 8-bit data
digitalWrite(latchPin, HIGH); // Latch data to outputs
Serial.print(\"Count: \");
Serial.println(i, BIN); //
delay(300); // Wait before next count
}
}
Serial.begin(9600) - Initializes serial communication so the binary count can be viewed on the Serial Monitor.
digitalWrite(latchPin, LOW) - Prepares the shift register to receive data.
shiftOut(dataPin, clockPin, MSBFIRST, i) - Sends the current count (0 to 255) as an 8-bit binary number.
digitalWrite(latchPin, HIGH) - Updates the register's outputs with the new value.
Serial.println(i, BIN) - Displays the binary number currently being shown on LEDs.
delay(300) - Adds a short pause before the next count.
digitalWrite(latchPin, LOW) - Prepares the shift register to receive data.
shiftOut(dataPin, clockPin, MSBFIRST, i) - Sends the current count (0 to 255) as an 8-bit binary number.
digitalWrite(latchPin, HIGH) - Updates the register's outputs with the new value.
Serial.println(i, BIN) - Displays the binary number currently being shown on LEDs.
delay(300) - Adds a short pause before the next count.
CODE -- PYTHON:
from machine import Pin
import time
dataPin = Pin(6, Pin.OUT)
clockPin = Pin(7, Pin.OUT)
latchPin = Pin(8, Pin.OUT)
def shiftOut(value):
for i in range(7, -1, -1):
bit = (value >> i) & 1
dataPin.value(bit)
clockPin.value(1)
time.sleep_us(1)
clockPin.value(0)
while True:
for i in range(256):
latchPin.value(0)
shiftOut(i)
latchPin.value(1)
print(\"Count:\", bin(i)) # Print binary value
time.sleep(0.3)
import time
dataPin = Pin(6, Pin.OUT)
clockPin = Pin(7, Pin.OUT)
latchPin = Pin(8, Pin.OUT)
def shiftOut(value):
for i in range(7, -1, -1):
bit = (value >> i) & 1
dataPin.value(bit)
clockPin.value(1)
time.sleep_us(1)
clockPin.value(0)
while True:
for i in range(256):
latchPin.value(0)
shiftOut(i)
latchPin.value(1)
print(\"Count:\", bin(i)) # Print binary value
time.sleep(0.3)
shiftOut(i) - Breaks the 8-bit number into individual bits and shifts them to the register.
dataPin.value(bit) - Sets the data line to the current bit.
clockPin.value(1) → value(0) - Pulses the clock to shift in the bit.
latchPin.value(1) - Applies the shifted data to the output pins.
print("Count:", bin(i)) - Shows the binary value on the serial terminal (Thonny).
time.sleep(0.3) - Adds delay between counts.
dataPin.value(bit) - Sets the data line to the current bit.
clockPin.value(1) → value(0) - Pulses the clock to shift in the bit.
latchPin.value(1) - Applies the shifted data to the output pins.
print("Count:", bin(i)) - Shows the binary value on the serial terminal (Thonny).
time.sleep(0.3) - Adds delay between counts.