Arduino or Node MCU has limited digital pins and if you are using multiple sensors then there is chance that you may exaughst out of the digital input/output pins. In order to solve this issue, thank god we have Shift Registers. These shift registers can be used to increase the number of digital input and output pins. In this blog I will show you two shift registers which are used to increase digital input and output pins.
CD4021: This is parallel to serial shift in register. Using this shift register you can use upto 8 digital input. Also, this collects the input information asynchronously and all at once. You can call it Input Shift Register. Basically you can connect different digital inputs and collect the information.
74HC595: This is serial to parallel shift out register. You can use 8 digital output.This works in synchronous serial communication.
![]() |
| Schematic for 74HC595, CD4021BE and Node MCU |
PIN Description is as below,
| CD4021 | Node MCU | 74HC595N |
| Parallel Serial Controller (P/SC) (PIN: 9) | D1 (GPIO5) | |
| CLOCK (PIN: 10) Clock PIN | D5 (GPIO14) | SCK/SRCLK/SH_CP (PIN: 11) CLOCK PIN |
| Q8 (PIN: 2) | D2 (GPIO4) | |
| D3 (GPIO0) | RCK/SH_CP/RCKL (PIN: 12) LATCH | |
| D7 (GPIO13) | SER/DS (PIN: 14) DATA | |
| GND | GND | G/OE |
| VCC | VCC | SCL/MR/SRCLR |
pinMode(LATCHOUT_PIN, OUTPUT);
pinMode(LATCHIN_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
dataIn = 0;
dataIn = This is variable which stores the out values. For exampple, out of 8 output pin, you can set any bit and send signal to output shift registers.
Once Initialization is complete, then next step is to record Input/output values.
Set the output to 74HC595 output pins as below,
digitalWrite(CLOCK_PIN, LOW);
digitalWrite(LATCHOUT_PIN, LOW);
shiftOut(DATAOUT_PIN, CLOCK_PIN, variableData);
digitalWrite(LATCHOUT_PIN, HIGH);
Intial data out value is set as below,
byte dataOut = 0xFF;
Change the bit as per requirement and set theDigital pin out value.
Below code value is used to read the value from Input Shift Register.
digitalWrite(CLOCK_PIN, HIGH);
digitalWrite(LATCHIN_PIN, HIGH);
delayMicroseconds(5);
digitalWrite(LATCHIN_PIN, LOW);
dataIn = shiftIn(DATAIN_PIN, CLOCK_PIN);
Both Shift IN and Shift out functions are standard provided at link,
https://www.arduino.cc/en/Tutorial/ShiftOut
https://www.arduino.cc/en/Tutorial/ShiftIn
Please find complete program as below,

No comments:
Post a Comment