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 them in our projects.
Parts Required
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 provides 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 rightmost 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 leads 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 of the logic and function tables of this circuit, you can visit the datasheet.
Circuit
We will start by placing the shift register in our breadboard, ensuring that each of its sides is 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 in 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.
Coding
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
void setup()
{
// Set all the pins of 74HC595 as OUTPUT
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
leds = 0; // Initially turns all the LEDs off, by giving the variable 'leds' the value 0
updateShiftRegister();
delay(500);
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();
delay(500);
}
}
/*
* 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.
*/
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
Way cool! Some very valid points! I appreciate you penning this write-up and also the rest of the site is very good. Julia Zane Kuster
I am looking for help for a DIY clock based on an ESP32 Devkit C with a 74HC595N. There are lots of examples using strips. I have 300 individual 3mm white LEDs. It’s easy enough to solder a bunch together to make up 4 x 7 segments. It is difficult to find information on power and resistors requirements. Some say to use 220 ohm on each LED and some say on each segment. Some say ESP32 5.5v will power it, some say it will need 12v. I hope ESP32 will power the 96 required LEDs.