Under this article, we will be controlling 8 LEDs using Shift Register IC 74HC595 (Quick Reference for 74HC595 here).
74HC595 is a Shift Register IC, that we can use it to expand limited Microcontroller output pin. We can use minimum of 3 output pin from Microcontroller to control the Shift Register IC (DS, SHCP and STCP) and we can have total of 8 output pin from from the Shift Register IC. In the other words, it is a serial (3 pin) to parallel (8 pin) converter that allows us to expand output pins from 3 pin to unlimited output pin. Connecting these Shift Register IC in series we can have more that 8 pin output, for example, using 5 x Shift Register IC, we can expand the output pin from 3 pin to 40 pin (5 x 8 pin). We will never be out of output pin anymore!
The basic operation to shift out 8-bit data to a 74HC595:
- Shift the 8-bit data to the 8-bit Storage Register through DS and SHCP pin (Serial Data and Serial Clock pin)
- Apply a pulse to STCP pin to shift the data from 8-bit Storage Register to 3-State Output Register (the 8 pin Parallel Output)
OE pin - the Output Enable pin that control the 3-State Output to either enable/activate the output (output = value of 3-State Output Register) or disable/deactivate the output (output = high impedance state).
MR pin - the Master Reset pin that clear the whole 8 Stage Shift Register Output.
Q7S pin - is the Serial Data Output that will be connected to the Serial Data Input pin of the next 74HC595 when they are connected in series (Daisy Chain)
Arduino Digital Pin2 will be connected to DS, Pin3 to SHCP and Pin4 to STCP pin on 74HC595. All LEDs will be connected to the Output Q0 - Q1 from 74HC595. OE pin connected to GND to always enable all output, MR pin connected to 5V to always enable the Shift Register.
You can download the full code at the end of this article.
The code for the project is quite simple, at the beginning of the coding, we will need to declare or define the pin use for Shift Register accordingly (DS=2, SHCP=3, and STCP=4). Under setup function, configure all the Shift Register pin as output, and set the output to LOW by default. Under loop function, we will create an int variable called counter, (use static keyword to make the value of the counter remain even after the loop function is exited). This counter variable will be increase each time the loop function is executed. Then, we will use shiftOut function to shift out the value of counter to 74HC595.
Remember that the shiftOut function will only shift out the data to 74HC595 8-bit Shift Register but the data will not shown on the output pins. We will need to send a pulse at STCP pin to channel or shift out the data we sent to output pin. Please take note that the minimum pulse width for STCP is around 22ns (for 4.5V VCC), which means that we don’t need to apply delay between setting and clearing the STCP pin. After sending pulse at STCP, we will perform a 50ms delay so that we can see the changes on the LEDs.