In this tutorial, we will learn how to control a 74HC595N Shift Register. Understanding the basic working principles of the 74HC595 and its usefulness, we can now begin to apply it in our projects.
To build the global setup you must have the following parts:
- • ESP8266 NodeMCU v1.0;
- • Micro-USB to USB cable;
- • 74hc595n;
- • 8 leds;
- • 8 x 220 ohm rasistor;
- • Male to male jumper wires;
- • 1 Breadboard;
Note: You do not need to have a NodeMCU board nor an ESP8266 to follow this tutorial. I am using this board because it is very famous and also because, I will post future tutorials using wifi communications. In order to use other boards, you just have to change the pin number.
Shift Register 74HC595N
A shift register is a circuit you can use to control many outputs (8 here) at the same time while only using a few pins (3 here) of your board. It works on Serial IN Parallel OUT protocol. It receives data serially from the microcontroller and then sends out this data through parallel pins.
Thankfully Arduino provide a helper function specifically for shift registers called shiftOut(), which allows to simply shift the bits in one call. The shiftOut() function takes four parameters; the first two are the pins to use for Data and Clock respectively. The third parameter specifies which end of the data you want to start at. We are going to start with the right most bit, which is referred to as the ‘Least Significant Bit’ (LSB).
The last parameter is the actual data to be shifted into the shift register, which in this case is a byte variable that you will fill with the respective leds you to turn on or off.
As you can see in figure 2, pins marked as Q0-Q7 (15 and 1-7) are the output pins. Pin16 is connected to (VCC) 3.3V, whereas pins 8 and 13 are connected to ground and pin 10 to (VCC) 3.3V. Finally pins 14, 12 and 11 are data, latch and clock pins and are the ones used by NodeMCU to pass data to the shift register.
For further understanding about the logic and function tables of this circuit, you can visit the datasheet.
We will start by placing the shift register in our breadboard, ensuring that each of its sides are placed on a separate side of the breadboard.
Then, we will connect pins 16 (VCC) to the 3.3V pin on the NodeMCU and connect pins 8 (GND) to its Gnd pin. This should keep the shift register into the normal working mode.
Next, we have to connect the three pins needed to control the shift register, which, in this case, are the following:
- Pin 11 (SRCLK) of the shift register to pin D5 on the NodeMCU;
- Pin 12 (RCLK) of the shift register to pin D8 on the NodeMCU;
- Pin 14 (SER) of the shift register to pin D7 on the NodeMCU.
Finally, we just have to connect all the output pins (QA-QH) to each one of our LEDs, ensuring that a 220Ω resistor is placed between the output pin and the LEDs, in order to limit the current flowing through them, and that the cathodes of the LEDs are connected to ground.
The result of the previously described wiring process should be similar to the illustration shown figure 4.
int latchPin = 15; // pin D8 on NodeMCU boards
int clockPin = 14; // pin D5 on NodeMCU boards
int dataPin = 13; // pin D7 on NodeMCU
byte leds = 0; // Variable to hold the pattern of which LEDs are currently turned on or off
// Set all the pins of 74HC595 as OUTPUT
leds = 0; // Initially turns all the LEDs off, by giving the variable 'leds' the value 0
for (int i = 0; i < 8; i++) // Turn all the LEDs ON one by one.
bitSet(leds, i); // Set the bit that controls that LED in the variable 'leds'
* updateShiftRegister() - This function sets the latchPin to low, then calls the Arduino function
* 'shiftOut' to shift out contents of variable 'leds' in the shift register before putting the 'latchPin' high again.
shiftOut(dataPin, clockPin, LSBFIRST, leds);
To see a video of the setup working, you can visit our youtube channel and watch the video.