Tags: ,

In this series of tutorials, we will see how to drive a multitude of relays with a NodeMCU32S microcontroller and 74HC595 shift registers. We will then see how to address each relay individually. Finally, we will create a web interface to control each relay via the internet. This project is a good basis to develop a home automation system for your home.

This tutorial can be applied to any microcontroller. You will need to be careful to modify the connection diagram and code to suit your use.


  • NodeMCU 32S
  • Breadboard
  • Jumper cable
  • 8 relay module
  • Shift register 74hc595


To control the 8 relay module, 8 digital outputs of the microcontroller must be reserved. In order to save the number of inputs/outputs of the microcontroller, different integrated circuits can be used. In particular, the shift register. The shift register 74HC595 has 8 programmable outputs, which can be set to 0 or 5V and only requires 3 digital inputs. This solution is therefore ideal to allow us to control the relays and keep the microcontroller outputs for other functions.


To control the shift register, the NodeMCU is connected as follows:

  • GND integrated circuit ground
  • Vcc power supply pin. Usually connected to 5V
  • SH_CP or RCLK connected to pin 33
  • ST_CP or SRCLK connected to pin 32
  • DS or SER connected to pin 25

The outputs of the integrated circuit 74HC595 (Q0-Q7) are directly connected to the inputs of the 8 relay module. In this tutorial we do not use the Enable (OE) and Reset (MR) pins.

  • OE Output enable, activates LOW. Pin connected to GND to activate the outputs.
  • MR Master reset, active LOW. Reset pin. Connected to 5V

Attention: The outputs of a microcontroller are current limited. To drive so many relays, it is strongly recommended to use an external voltage source.

NodeMCU32S ESP32, shift register 74HC595 and 8 relays module schematics


In the following code, we will first define the basic functions to manage the shift register. Then we will operate the relays one by one using a for loop.

#define number_of_74hc595s 1
#define numOfRegisterPins number_of_74hc595s * 8
#define SER_Pin 25
#define RCLK_Pin 33
#define SRCLK_Pin 32

boolean registers [numOfRegisterPins];

void setup(){
//Init Serial USB
Serial.println(F("Initialize System"));
//Init register
pinMode(SER_Pin, OUTPUT);
pinMode(RCLK_Pin, OUTPUT);
pinMode(SRCLK_Pin, OUTPUT);

void loop(){

void clearRegisters(){/* function clearRegisters */ 
//// Clear registers variables 
for(int i = numOfRegisterPins-1; i >=  0; i--){
  registers[i] = HIGH;//LOW;

void writeRegisters(){/* function writeRegisters */ 
//// Write register after being set 
digitalWrite(RCLK_Pin, LOW);
 for(int i = numOfRegisterPins-1; i >=  0; i--){
  digitalWrite(SRCLK_Pin, LOW); int val = registers[i];
  digitalWrite(SER_Pin, val);
  digitalWrite(SRCLK_Pin, HIGH);
  digitalWrite(RCLK_Pin, HIGH);

void setRegisterPin(int index,int value){/* function setRegisterPin */ 
////Set register variable to HIGH or LOW
registers[index] = value;

void writeGrpRelay(){/* function writeGrpRelay */ 
for(int i = numOfRegisterPins-1; i >=  0; i--){
   Serial.print(F("Relay "));Serial.print(i);Serial.println(F(" HIGH"));
   setRegisterPin(i, LOW);
   Serial.print(F("Relay "));Serial.print(i);Serial.println(F(" LOW"));
   setRegisterPin(i, HIGH);


Once the components have been correctly connected and the code loaded into the microcontroller, the relays should activate one after the other.

Bonus: Control 16 relays with two shift registers.


NodeMCU32S ESP32, shift registers 74HC595 and 16 relays module schematics


The beauty of this code is that to make it work with a second shift register you only need to change the parameter:

#define number_of_74hc595s 2


Next Steps


How useful was this post?

Click on a star to rate it!

Average rating 4.4 / 5. Vote count: 14

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?