Connecting ESP-01 to Relay Module V4.0
Connecting ESP-01 to Relay Module V4.0
Prerequisites
- ESP-01 Module (Affiliate) – Buy on AliExpress
- USB ESP-01 Programming Adapter with a CH340G chip (Affiliate) – Buy on AliExpress
- Relay Module V4 (THE ONE WITH THE OPTOCOUPLER!)(Affiliate) – Buy on AliExpress or Buy on AliExpress
- Female and Male headers (Affiliate) –Buy on AliExpress
- 470 uF 25V Capacitor (Affiliate) – Buy on AliExpress
- 220 Ohm Resistor Kit (Affiliate) – Buy on AliExpress
- 5mm Led kit – Box (Affiliate) – Buy on AliExpress
- Breadboard and jumper wires (Affiliate) – Buy on AliExpress
- Computer with Arduino IDE installed
Understanding the Relay Module V4.0
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
- 5V to 3.3V Voltage Regulator to power the Esp-01
- Reset Button to reset the Esp-01
- Esp01 Connector Block
- Optocoupler
- Relay Activation Transistor
- Relay
How the Relay Module Work?
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-01-Relay-v4.0 ) , and I solved the problem by adding a capacitor parallel (470uF) with the LED of the Optocoupler(PC817).
Using a Female Header Socket to fix the problem…
We can cut it leaving only two 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
// 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.
- 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 On Start Up Fixed