Your Arduino Uno has 14 digital pins. That seems like a lot until you try to build an LED cube or a digital clock, at which point you realize you are hopelessly out of pins and completely stuck.
Instead of throwing money at a bigger Arduino, you can cheat the system using a magical little IC called a Shift Register. It takes three pins and turns them into eight. Itâs like a power strip for your microcontroller outputs.
A shift register acts like an expansion module, turning a few control pins into many output pins.
The 74HC595 is one of the most famous shift registers. It takes serial data (data sent one bit at a time, sequentially down a single wire) and converts it into parallel data (multiple bits output simultaneously across multiple wires).
In practical terms: you use just three pins on your Arduino to talk to the 74HC595, and the 74HC595 gives you eight new digital output pins! Even better, you can âdaisy-chainâ multiple shift registers together. Connect two 74HC595 chips, and you get 16 outputs, still using those same original three Arduino pins.
The 74HC595 has 16 pins. The three critical pins connected to the Arduino are:
Wiring 8 LEDs can be messy, so take your time!
Here is the code to make the LEDs light up in a sequence using the shiftOut() function built into Arduino.
// Define the pins connected to the 74HC595
const int latchPin = 8; // STCP
const int clockPin = 12; // SHCP
const int dataPin = 11; // DS
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
// Loop through all 8 LEDs
for (int i = 0; i < 8; i++) {
// Ground the latch pin so the LEDs don't flicker while transmitting
digitalWrite(latchPin, LOW);
// Shift out a byte (8 bits).
// bit(i) creates a byte with only the i-th bit set to 1.
shiftOut(dataPin, clockPin, MSBFIRST, bit(i));
// Pulse the latch pin high to push the data to the outputs
digitalWrite(latchPin, HIGH);
delay(200); // Wait so we can see the LED
}
}
By mastering the shift register, you unlock the ability to build massive LED displays and complex indicators without worrying about running out of pins!