Example Code for Arduino-Shift Register Output Control
Last revision 2025/12/11
This article guides users through controlling shift register outputs with an Arduino, including wiring instructions and example code for easy implementation.
Wiring Diagram
- Input:
- D3 connect to Arduino Digital 3
- D8 connect to Arduino Digital 8
- D9 connect to Arduino Digital 9
- VCC connect to Arduino 5V
- GND connect to Arduino GND
- Output:
- D3 connect to the modules' input D3
- D8 connect to the modules' input D8
- D9 connect to the modules' input D9
- VCC connect to the modules' VCC
- GND connect to the modules' GND
Sample Code
//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 3;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 9;
byte Tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Serial.begin(9600);
Serial.println("reset");
}
void loop() {
if (Serial.available() > 0) {
// ASCII '0' through '9' characters are
// represented by the values 48 through 57.
// so if the user types a number from 0 through 9 in ASCII,
// you can subtract 48 to get the actual value:
int bitToSet = Serial.read() - 48;
// write to the shift register with the correct bit set high:
digitalWrite(latchPin, LOW);
// shift the bits out:
shiftOut(dataPin, clockPin, MSBFIRST, Tab[bitToSet]);
// turn on the output so the LEDs can light up:
digitalWrite(latchPin, HIGH);
}
}
This module supports multiple connections. However, connecting too many modules may affect performance. It is recommended to connect no more than 20 modules.
Was this article helpful?
