Connecting ESP-01 to Relay Module V4.0

In this blog post, we will explore how to connect the ESP-01 board to the Relay Module V4.0, address the issue of pin flickering, and implement a solution in the hardware. We will also delve into the inner workings of the relay module and provide a detailed explanation of the code used to activate the relay.

Prerequisites

  1. ESP-01 Module  (Affiliate) – https://s.click.aliexpress.com/e/_DETsfWR
  2. USB ESP-01 Programming Adapter with a CH340G chip (Affiliate) – https://s.click.aliexpress.com/e/_DETsfWR
  3. Relay Module V4 (Affiliate) – https://s.click.aliexpress.com/e/_DdEiLmR
  4. Breadboard and jumper wires (Affiliate) – https://s.click.aliexpress.com/e/_Dl5kuk1
  5. Computer with Arduino IDE installed

Understanding the Relay Module V4.0

The Relay Module V4.0 is a popular choice for controlling high voltage and high current devices using microcontrollers like the ESP-01. It consists of an Optocoupler, a Relay, and a Transistor. The optocoupler isolates the low voltage side (ESP-01) from the high voltage side (relay). The transistor acts as a switch, controlling the relay based on the input signal from the ESP-01.

 

 

 

 

Relay Module Parameters:

  • Product size: 37*25mm
  • Operating voltage: 5V DC
  • Communication: WIFI (ESP8266 module)
  • Transmission distance: the maximum transmission distance of 400m (open environment, mobile phone equipped with WIFI module)
  • Load: 10A/250VAC 10A/30VDC, relay pull 100,000 times

Schematic

From the schematic we can see the most important components that constitute the relay module:

 

 

  1. 5V to 3.3V Voltage Regulator to power the Esp-01
  2. Reset Button to reset the Esp-01
  3. Esp01 Connector Block
  4. Optocoupler
  5. Relay Activation Transistor
  6. Relay

How the Relay Module Work?

An electromagnetic relay module is a device that allows low-power microcontrollers, like the ESP-01 (an IoT module based on the ESP8266 microcontroller), to control high-power electrical devices. The relay module essentially acts as a switch that can be controlled electronically.

 

 

 

SOURCE (Changed by Me): https://ncd.io/blog/relay-logic/

 

 

 

Addressing Pin Flickering

Pin flickering occurs when the GPIO pin toggles between HIGH and LOW states rapidly and, in the case of the Esp01 relay module, when it is powered. To address this issue, we will implement a hardware debounce technique. Hardware Debouncing using a capacitor ensures that the relay only switches on or off when we want

 

 

 

Solving the Pin Flickering

  • During boot the relay turns on then off then on again for brief period of time.
  • I have module (ESP-01S-Relay-v4.0 ) , and I solved the problem by adding a capacitor parallel with the LED of the Optocoupler(PC817).

 

 

 

 

 

Using a Female Header Socket to fix the problem…

 

 

We can cut it leaving only two access pins…

 

 

Next, with patience, we solder it parallel to the optocoupler Led pins

 

 

 

Electrolytic capacitors are a type of capacitor that uses an electrolyte as one of its plates to achieve a higher capacitance per unit volume than other types of capacitors. The polarity of electrolytic capacitors is important, and they must be connected in the correct orientation in a circuit. Electrolytic capacitors have a positive (+) and a negative (-) lead, indicating the polarity.

  • Connecting an electrolytic capacitor in reverse polarity can lead to the capacitor becoming damaged, overheating, leaking electrolyte, or even exploding.

 

 

Finally connect slide the capacitor in the Header Socket…

 

 

Code to Test the Relay Module

 

Here’s a sample code to activate the relay using the ESP-01 board:
// Define a constant integer named ledPin and set its value to 0
const int ledPin = 0;

// The setup function is called once when the Arduino starts
void setup() {
  // Set the ledPin as an OUTPUT
  pinMode(ledPin, OUTPUT);
}

// The loop function runs over and over again indefinitely after setup
void loop() {
  // Turn the LED off by setting the voltage at ledPin to HIGH
  digitalWrite(ledPin, HIGH);

  // Pause the program for 3000 milliseconds (1 second)
  delay(3000);

  // Turn the LED on by setting the voltage at ledPin to LOW
  digitalWrite(ledPin, LOW);

  // Pause the program for another 3000 milliseconds (1 second)
  delay(3000);
}

 

The relay module uses a inverted logic to activate:

  • A LOW signal activates the Relay
  • A HIGH signal deactivates the Relay

 

Setting Up the Hardware

 

To put the CH340G adapter in programming mode, follow these steps:

  • Before plugging the USB ESP-01 programming adapter into a USB port on your computer, Press and hold the push button switch that you soldered in Step 2 for a few seconds.
  • Plug the USB ESP-01 programming adapter into a USB port on your computer with the button pressed.
  • Release the button: After a few seconds, release the push button switch. The Esp-01 should be in programming mode, and you can upload code to the ESP-01 module using your preferred development environment.

 

If having trouble, go to my previous blog post. It explains with images: https://www.edgemicrotech.com/preparing-the-usb-esp-01-programming-adapter-a-step-by-step-guide/

 

Wiring the Components for the Experiment

 

  • PAY ATENTION TO THE PIN INFO ON THE BOARD WHILE CONNECTING THE WIRES, SOME VERSIONS MAY BE DIFERENT
  • LONGER LED LEG TO THE NO SIDE AND SHORTER TO THE RESISTOR SIDE
  • USE A 220 OHM RESISTOR

 

 

Connecting ESP-01 to Relay Module V4.0

After programming the esp-01, place it in the position has it shows in the following image:

 

 

Step 5: Relay Module Pin Flickering Fixed

The solution provided above uses the hardware debounce technique by using a capacitor in parallel with the of the optocoupler LED to ensure that the relay only switches on when we want, even if the input signal fluctuates. This prevents the relay from flickering and ensures stable operation.

 

Conclusion:

In this blog post, we have covered how to connect the ESP-01 board to the Relay Module V4.0, addressed the issue of pin flickering, and implemented a solution in the code. We have also provided a detailed explanation of the relay module’s inner workings and the code used to activate the relay. With this knowledge, you can now confidently control high voltage and high current devicesusing the ESP-01 and Relay Module V4.0.