If you work with microcontrollers like the ATmega328 (Arduino Uno) or ESP8266 (Wemos D1 mini) and want to control many lines at the same time, it can become a problem that there are not enough outputs are available.
For example: to control an HD44780-compatible LCD at least 6 lines are required if it is operated in 4-bit mode. However with a Wemos D1 mini there are only 11 outputs available or 9 if you want to use the serial data lines. If 6 lines are only used for the display, only 5 or 3 lines are available for other purposes. This can still be sufficient in some cases – but if you also want to connect sensors or buttons to control the unit, this may not be enough.
Shift registers are one solution to this problem. With these you can operate almost any number of outputs with just three lines. The only limit is the time it takes to fill the registers.
Many outputs with a 74HC595
A shift register with serial input and parallel output, “SIPO” for short for “serial input, parallel output”, can be used to control many outputs. A component often used for this purpose is the 74HC595 – an 8-bit shift register. According to the data sheet, this IC can be used at a voltage range from 2 to 6 volts and with a clock frequency of up to 25 MHz (at 5 volts around 20 MHz).
In the following description 0 and 1 are used for “low” (voltage below 0.8 volts) and “high” (voltage above 2 volts).
The pinout for a DIP 16 package is as following:
|2||QC||Output 3||15||QA||Output 1|
|3||QD||Output 4||14||SER||Serial Input|
|4||QE||Output 5||13||OE||Output Enable|
|5||QF||Output 6||12||RCLK||Register Clock|
|6||QG||Output 7||11||SRCLK||Store Register Clock|
|7||QH||Output 8||10||SRCLR||Store Register Clear|
* This output can be used to control a second shift register.
The circuitry for operating a single shift register needs just three outputs to control SER, RCLK and SRCLK:
The power supply uses GND and VCC. GND will be connected to ground, VCC with +5V or +3.3V.
With OE you enable the output of the shift register. Therefore this input will be connected to GND.
Data input is done by using SER, RCLK and SRCLK in the following order:
- Set SRCLK to 0.
- Set RCLK to 0.
- Set SER to 0 or 1 depending on which information you want so store.
- Set RCLK to 1 – this shifts the register content up one bit and puts the data from SER to the lowest bit of register.
- Repeat step 2-4 for the remaining bits.
- Set SRCLK to 1 – this will copy the written 8 bits to the outputs.
Using multiple shift registers
If 8 bit are not enough you can also connect multiple shift registers in series. For this you connec the output QH* (pin 9) to the input SER (pin 14) of the next chip. SRCLK and RCLK are connected together:
Further chips can also be connected according to the same scheme. You just have to remember that with each additional shift register a further 8 clock cycles are required for the output of the data, which means that the maximum possible speed decreases with an increasing number of outputs.
An alternative to using shift registers is the universal port expander MCP23017. This is more expensive but provides great flexibility and the possibility to use ports as input as well.