Introduction: Arduino 16 LEDs Using Two 74HC595 Shift Registers (unlimited Pins)

This is my first instructable, hope you will like it. I have been playing around with LEDs for a while and I realized that I am pretty much limited with Arduino pins and can not make huge projects which requires a lot of pins. This is small project with main focus to drive LEDs by using Arduino pins as less as possible and it demonstrates how to add infinite number of pins. In this particular project the 16 LEDs are driven with just only 3 Arduino pins. The key element is shift register. Each 74HC595 shift register can drive up to 8 LEDs and by daisy chaining registers it is possible to extend Arduino 3 pins to infinite number (for great number of registers, there could be problem with clock which is required for shift registers).

Step 1: Components

- Arduino UNO (x1)

- 74HC595 shift register (x2)

- LEDs (x16)

- 220omh resistors (x16)

- A lot of wires

- Two breadborads (one with 400 pins and other with 830)

- Potentiometer, to control the brightness (optional)

Step 2: Circuit

Make the circuit (see above) and connect the first shift register as following:

- GND (pin 8) to ground

- Vcc (pin 16) to 5V

- OE (pin 13) to ground

- MR (pin 10) to 5V

- DS (pin 14) to Arduino pin 11

- SH_CP (pin 11) to Arduino pin 12

- ST_CP (pin 12) to Arduino pin 8

Connect the second shift register exactly the same, but connect the DS (pin 14) to first register pin 9. After that connect pins: 1, 2, 3, 4, 5, 6, 7 and15 from both registers to LEDs. This connection makes all the pins always active and addressable, however when the Arduino is powered up some of the LEDs may be turned on. Solution for this is to connect MR (pin 10) and OE (pin 13) to Arduino directly, but it this way you have to sacrifice 2 Aurduino pins.

To add more shift registers connect them like the second register. Always connect MR and OE pins directly to Arduino and DS pin to previous register.

If you want to regulate the brightness of LEDs then connect potentiometer as shown in picture above to control resistance for all LEDs. However it is optional and you can get along without it.

Step 3: The Code

The existing codes are designed to limited number of shift registers and there is no universal function/method to do it. As a software developer it is unacceptable for me and I am used to make everything as dynamic as possible with no limitations. I redesigned the existing code samples to allow you to use unlimited number of shift registers. See code below:

int numOfRegisters = 2;
byte* registerState;

void setup() {
//Initialize array

registerState = new byte[numOfRegisters];

for (size_t i = 0; i < numOfRegisters; i++) {

registerState[i] = 0;

}

//... setup

}

void regWrite(int pin, bool state) {
//Determines register int reg = pin / 8;

//Determines pin for actual register

int actualPin = pin - (8 * reg);

//Begin session

digitalWrite(latchPin, LOW);

for (int i = 0; i < numOfRegisters; i++) {

//Get actual states for register

byte* states = ®isterState[i];

//Update state

if (i == reg) {

bitWrite(*states, actualPin, state);

}

//Write

shiftOut(dataPin, clockPin, MSBFIRST, *states);

}

//End session

digitalWrite(latchPin, HIGH);

}

Step 4: The Final Code

I posted the full source code on github, because here it would be unreadable:

https://github.com/jr2002008/Arduino-74HC595-shift-registers/blob/master/ArduinoLEDsWithShiftRegisters/ArduinoLEDsWithShiftRegisters.ino

In the final code I added several effects for those 16 LEDs. The effects are demonstrated in the video above. If you want to add more LEDs, connect more shit registers as described previously and change the value of numOfRegisters in the code (Also adjust logic for effects).

You can also use this code not for just LEDs only, if you simply want more pins for your Arduino use the regWrite(int pin, bool state) function to write state for any pin and there is no limit how much shift registers you are using, just change the value of numOfRegisters and every thing else is automated.

Comments

author
alexkirlian made it!(author)2017-07-01

Fantastic effects and a great opportunity to study the 74HC595.

Arduino_74HC595_Seq_03.jpg
author
ArmandoC26 made it!(author)2016-11-03

hi! great job, specially with this dynamic code! it looks very good though i don't get these 2 lines, can you please help me?

int reg = pin / 8

int actualPin = pin - (8 * reg);

isn't actualPin always 0 despite of "pin" value?

Reg=10/8=1.25

acrualPin = 10-(8*1.25) = 10-10

Thanks!

Armando


author
JimC247 made it!(author)2017-05-25

Think ints. pin=10. reg = 10/8 = (int)1.25 = 1. actual = 10-8=2, so third pin on second register (0 numbered).

author
ArmandoC26 made it!(author)2017-05-26

yes, got it after a while.... :)

author
ArmandoC26 made it!(author)2016-11-03

Yup! got it! int( is doing the trik... sorry ! :)

author
JimC247 made it!(author)2017-05-25

Thank you! This was my intro to the Shift registers. It took a little
over an hour to hook up the wires & run the program! (Admitably I
had a board with 12 LEDs & resistors pre made from a previous
project, also did not use potentiometer). Thanks for sharing!

author
LightBlueHue made it!(author)2017-05-15

Should it work as soon as you upload or does something in the code need changing? The strange numbering of the switch cases are confusing me also (0, 1, 3, etc). Any help would be much appreciated. Thanks!

author
S%C3%A9bastienA8 made it!(author)2017-04-17

Hello, I like the idea, bravo. I try to do the same principle with the LEDs that light up one by one and extinguish at once to make the electronic side of the door of the atlantis stars. Could you help me?

https://www.youtube.com/watch?v=Pp-huP2-EX4

author
Jules1050 made it!(author)2017-01-17

Hi,

this could be interesting for you:

https://www.sparkfun.com/products/10680

SMD Version of the shift register with breakout board!

author
Prabash made it!(author)2016-12-09

How ai get this code sampale to the arduino software interface

author
ArifSae made it!(author)2016-03-06

I made my project with 16 RGB leds using shift register.

https://www.instructables.com/id/Sparrow-My-Assista...

220 Ohm in my opinion is a not good choice as you are going to blow your IC if you use all leds lit at same time (7ma approx X8 leds = 56ma, very close to maximum to IC limit) . I used 1k Ohms and now leds are only slight less brighter. I think tutorials should mention this point as new comers might blow ICs and get dishearted.

author
DIY+Hacks+and+How+Tos made it!(author)2015-11-29

Great project.

author
J%C4%81nisR4 made it!(author)2015-11-30

Thank you