B4R Code Snippet Shift Register 74HC595 - Controlling 8+ LED's with only 3 pins

SubName: Controlling 8 LED's with only 3 pins from your Arduino through a shift register.
Description: Here is some basic code for controlling 8 LED's through a 74HC595 shift register, you could in fact control a lot more LED's using multiple shift registers and still only use 3 pins from your Arduino device. You can use shift registers to control anything from LED's to multiple 8 channel relay modules etc.

I know that @derez has already created posts controlling shift registers with shiftOut, my code snippet does not use shiftOut.

Some more information:
Simply putting it, one single shift register IC allows you to control up to 8 outputs using only 3 input pins (in this example 3 digital pins from an Arduino UNO based microcontroller). You shift either 1's or 0's (high or low) into one or more bits of the shift register using the Data and Clock lines, using the Data and Clock lines is the only way to write 1's or 0's (high or low) to the shift registers storage. Once you have finished writing to storage you just latch the stored data to the output pins of the shift register, you can write up to 8 bits of data into the shift register at any one time before latching the stored data to output pins (Q0 to Q7) of the shift register.

I'm not going to attempt to write an in-depth description on shift registers, but if you really want to know then I highly suggest that you watch a couple of YouTube videos on the subject, you will find it a lot more interesting than reading a few paragraphs from me.

Please note:
Multiple shift registers can be connected together relatively easily in series (daisy chained) to control 8, 16, 24, 32, 40 etc LED's, Relays etc. The more devices that you connect to your Arduino the more power that will be needed to switch all your devices on at the same time, so I suggest that you use an external power source for larger projects.
'WIRE LEGEND for 74HC595 shift register
'Vcc = 5V
'MR = 5V
'DS = 8
'STCP = 11
'SHCP = 12
'Q0 To Q7 = Goes through 8 x 220Ω resistors to 8 LED's

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.

    Public Serial1 As Serial
    Private DS, STCP, SHCP As Pin
    Private Registers(8) As Boolean
End Sub

Private Sub AppStart

    DS.Initialize(8, DS.MODE_OUTPUT)
    STCP.Initialize(11, STCP.MODE_OUTPUT)
    SHCP.Initialize(12, SHCP.MODE_OUTPUT)

    'Loopey is for example purposes only
End Sub

Sub Loopey
    Dim i As Int = 0
    Do While i < 8
        Registers(i) = True
        i = i + 1

    Dim i As Int = 7
    Do While  i > 0
        Registers(i) = False
        i = i - 1
End Sub

Private Sub WriteToReg()

    Dim i As Int = 7
    Do While i >= 0
        i = i - 1

End Sub

Tags: 8, LED, Shift, Register, Arduino

Wiring diagram
The 1µF capacitor is only needed if you are having latching issues


Breadboard layout with Arduino

8 LED's connected through a shift register to an UNO based microcontroller board

Last edited: