Using 74HC595 Shift Registers with Arduino

- Using 74HC595 Shift Registers with Arduino
- Daisy chaining 74HC595 shift registers
- Different ways (binary, decimal, hex) to hold the data using an array
- Using bitshift, bitwrite operators
- Direct port access for faster manipulation of the IO pins

const byte COL_COUNT = 8; //array to hold the data unsigned char sequence[COL_COUNT] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000}; //unsigned char sequence[COL_COUNT] = {1, 2, 4, 8, 16, 32, 64, 128}; //unsigned char sequence[COL_COUNT] = {0x01, 0x02, 0x04, 0x8, 0x10, 0x20, 0x40, 0x80}; //Define which pins will be used for the shift register control //can be any digital pin on the Arduino int latchPin = 8; //Pin connected to ST_CP(pin 12) of 74HC595 int clockPin = 12; //Pin connected to SH_CP(pin 11) of 74HC595 int dataPin = 11; //Pin connected to DS(pin 14) of 74HC595 void setup() { pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); } void loop() { for (int col = 0; col < COL_COUNT; col++) { digitalWrite(latchPin, LOW); //Pull latch LOW to send data shiftOut(dataPin, clockPin, MSBFIRST, sequence[col]); //Send the data digitalWrite(latchPin, HIGH); // Pull latch HIGH to stop sending data delay(200); } }

- Line 4 assign variables using binary notation (0 and 1), simply prepend the symbols B to the front of the binary number.
- Line 5 assign variables using decimal notation (0 to 9).
- Line 6 assign variables using hexadecimal notation (0 to 9 and A to F), simply prepend the symbols 0x to the front of the hexadecimal number.
for (int col = 0; col < COL_COUNT; col++) { digitalWrite(latchPin, LOW); //Pull latch LOW to send data shiftOut(dataPin, clockPin, MSBFIRST, sequence[col]); //Send the data digitalWrite(latchPin, HIGH); //// Pull latch HIGH to stop sending data delay(200); }
shiftOut(11, 12, MSBFIRST, sequence[7])
or
shiftOut(11, 12, MSBFIRST, B10000000) //binary
or
shiftOut(11, 12, MSBFIRST, 128) //decimal
or
shiftOut(11, 12, MSBFIRST, 0x80) //hex
|
const byte COL_COUNT = 8; const byte ROW_COUNT = 3; //array to hold the data unsigned char sequence[24] = { B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000, B00000001, B00000011, B00000111, B00001111, B00011111, B00111111, B01111111, B11111111, B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111 }; //Define which pins will be used for the shift register control //can be any digital pin on the Arduino int latchPin = 8; //Pin connected to ST_CP(pin 12) of 74HC595 int clockPin = 12; //Pin connected to SH_CP(pin 11) of 74HC595 int dataPin = 11; //Pin connected to DS(pin 14) of 74HC595 void setup() { pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); } void loop() { for (int row = 0; row < ROW_COUNT; row++) { for (int col = 0; col < COL_COUNT; col++) { digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, sequence[col + row*COL_COUNT]); digitalWrite(latchPin, HIGH); delay(200); } } }
const byte COL_COUNT = 8; const byte ROW_COUNT = 3; //array to hold the data unsigned char sequence[ROW_COUNT][COL_COUNT] = { B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000, B00000001, B00000011, B00000111, B00001111, B00011111, B00111111, B01111111, B11111111, B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111 }; //Define which pins will be used for the shift register control //can be any digital pin on the Arduino int latchPin = 8; //Pin connected to ST_CP(pin 12) of 74HC595 int clockPin = 12; //Pin connected to SH_CP(pin 11) of 74HC595 int dataPin = 11; //Pin connected to DS(pin 14) of 74HC595 void setup() { pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); } void loop() { for (int row = 0; row < ROW_COUNT; row++) { for (int col = 0; col < COL_COUNT; col++) { digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, sequence[row][col]); digitalWrite(latchPin, HIGH); delay(200); } } }
const byte COL_COUNT = 8; const byte ROW_COUNT = 3; //array to hold the data unsigned char sequence[ROW_COUNT][COL_COUNT] = { B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000, B00000001, B00000011, B00000111, B00001111, B00011111, B00111111, B01111111, B11111111, B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111 }; //Define which pins will be used for the shift register control //can be any digital pin on the Arduino int latchPin = 8; //Pin connected to ST_CP(pin 12) of 74HC595 int clockPin = 12; //Pin connected to SH_CP(pin 11) of 74HC595 int dataPin = 11; //Pin connected to DS(pin 14) of 74HC595 int row; void setup() { Serial.begin(9600); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); } void loop() { row = 0; col_display(); row = 1; col_display(); row = 2; col_display(); row = 1; col_display(); row = 0; col_display(); } void col_display() { for (int col = 0; col < COL_COUNT; col++) { digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, sequence[row][col]); digitalWrite(latchPin, HIGH); delay(200); } }
const byte COL_COUNT = 8; int latchPin = 8; //Pin connected to ST_CP(pin 12) of 74HC595 int clockPin = 12; //Pin connected to SH_CP(pin 11) of 74HC595 int dataPin = 11; //Pin connected to DS(pin 14) of 74HC595 void setup() { pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); } void loop() { for (int col = 0; col < COL_COUNT; col++) { int col_data = 1 << col; //bitshift left digitalWrite(latchPin, LOW); //Pull latch LOW to send data shiftOut(dataPin, clockPin, MSBFIRST, col_data); //Send the data digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data delay(200); } }
const byte COL_COUNT = 8; int latchPin = 8; //Pin connected to ST_CP(pin 12) of 74HC595 int clockPin = 12; //Pin connected to SH_CP(pin 11) of 74HC595 int dataPin = 11; //Pin connected to DS(pin 14) of 74HC595 void setup() { pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); } void loop() { int col_data = 0; for (int col = 0; col < COL_COUNT; col++) { bitWrite(col_data, col, HIGH); digitalWrite(latchPin, LOW); //Pull latch LOW to send data shiftOut(dataPin, clockPin, MSBFIRST, col_data); //Send the data digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data delay(200); } for (int col = 0; col < COL_COUNT; col++) { bitWrite(col_data, col, LOW); digitalWrite(latchPin, LOW); //Pull latch LOW to send data shiftOut(dataPin, clockPin, MSBFIRST, col_data); //Send the data digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data delay(200); } }
const byte COL_COUNT = 8; unsigned int sequence[COL_COUNT] = {0B0000000100000001, 0B0000001000000011, 0B0000010000000111, 0B0000100000001111, 0B0001000000011111, 0B10000000111111, 0B0100000001111111, 0B1000000011111111}; //unsigned int sequence[COL_COUNT] = {257, 515, 1031, 2063, 4127, 8255, 16511, 33023}; int latchPin = 8; //Pin connected to ST_CP(pin 12) of 74HC595 int clockPin = 12; //Pin connected to SH_CP(pin 11) of 74HC595 int dataPin = 11; //Pin connected to DS(pin 14) of 74HC595 void setup() { pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); } void loop() { for (int col = 0; col < COL_COUNT; col++) { digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, (sequence[col] >> 8)); //shift out highbyte shiftOut(dataPin, clockPin, MSBFIRST,sequence[col]); //shift out lowbyte digitalWrite(latchPin, HIGH); delay(200); } }
- Change unsigned char sequence[COL_COUNT] to unsigned int sequence[COL_COUNT] for 16 bits serial data
- Prepend the symbols 0B to the front of the binary number for 16 bit data
Seq. | First 74HC595 | Second 74HC595 |
1 | 00000001 | 00000001 |
2 | 00000011 | 00000010 |
3 | 00000111 | 00000100 |
4 | 00001111 | 00001000 |
5 | 00011111 | 00010000 |
6 | 00111111 | 00100000 |
7 | 01111111 | 01000000 |
8 | 11111111 | 10000000 |

- The serial data for first shift register is getting from a two dimension array, and therefore its output is exactly sames as sequence[][] array
- The serial data for second shift register is gemerated using a left shift (<<) operator
const byte COL_COUNT = 8; const byte ROW_COUNT = 3; //array to hold the data unsigned char sequence[ROW_COUNT][COL_COUNT] = { B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000, B00000001, B00000011, B00000111, B00001111, B00011111, B00111111, B01111111, B11111111, B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111 }; //Define which pins will be used for the shift register control //can be any digital pin on the Arduino int latchPin = 8; //Pin connected to ST_CP(pin 12) of 74HC595 int clockPin = 12; //Pin connected to SH_CP(pin 11) of 74HC595 int dataPin = 11; //Pin connected to DS(pin 14) of 74HC595 int row; void setup() { pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); } void loop() { row = 0; col_display(); //display first row data row = 1; col_display(); //display second row data row = 2; col_display(); //display third row data } void col_display() { for (int col = 0; col < COL_COUNT; col++) { int second74HC595_data = 1 << col+8; //serial data for 2nd 74HC595 int data = sequence[row][col] + second74HC595_data; //1st 74HC595 serial data + 2nd 74HC595r serial data digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, (data >> 8)); //shift out highbyte (2nd 74HC595) shiftOut(dataPin, clockPin, MSBFIRST,data); //shift out lowbyte (1st 74HC595) digitalWrite(latchPin, HIGH); delay(200); } }
const byte BIT_COUNT = 8; void setup() {
} // generate a pulse for SERIAL CLOCKPORTB &= ~(1 << 4); // Set the clockPin low PORTB |= (1 << 4); // Set the clockPin high // it can also use the code below to generate SERIAL CLOCK // ^= toggle bit, doing this twice to generate a high low pulse // PORTB ^= 1 << 4; //toogle bit 3, all other pin unchange // PORTB ^= 1 << 4; } }
|
- If you want to control two shift registers (16 bits), chang the BIT_COUNT to 16
- If you want MSBFIRST, change if (data & (1<<i)) { to code below
if (data & (0B1000000000000000>>i)) //most significant bit is sent first. |