In this post you’ll learn how to power the ESP8266 (or ESP32) with mains voltage using the Hi-Link HLK-PM03 converter. As an example, we’ll use the ESP8266-01 to control a relay with a web server.
The ESP32 and ESP8266 are cheap Wi-Fi modules perfectly suited for DIY projects in the Internet of Things (IoT) and Home Automation fields. Using an ESP32 or ESP8266 with a relay allows you to control any AC electronics appliances over Wi-Fi (using a web-server, for example).
One of the biggest issues with these projects is to find a suitable power supply for the ESP32/ESP8266 with a small form factor at the same time (in a final application you don’t want to power the relay and the ESP32/ESP8266 using two different power supplies). One solution is to power the ESP8266 or ESP32 from mains voltage using the AC/DC converter Hi-Link HLK-PM03 (or HLK-PM01 model).
Introducing the Hi-Link HLK-PM03/01 Converter Modules
The Hi-Link HLK-PM03 is a small AC/DC converter module as shown in the figure below.
The HLK-PM03 AC/DC converter can supply 3.3V from either 110VAC or 220VAC. This makes it perfect for small projects that need a 3.3V supply from mains voltage. You can also get 5V output using the HLK-PM01 instead.
You can read more information about these modules’ specifications: HLK-PM03 and HLK-PM01. There’s also a really good article about the performance test of the HLK-PM01.
To power the ESP8266-01 from mains voltage, we’ll be using the HLK-PM03 to provide 3.3V to the VCC pin. If you need 5V to power other ESP8266 models or an ESP32 through the VIN pin, you can use the HLK-PM01 model that provides 5V output and works similarly.
Where to Buy?
You can check the HLK-PM03 or HLK-PM01 modules on Maker Advisor and find the best price.
Safety Warning
This project deals with mains voltage. Make sure you understand what you are doing. Please read the safety warning below carefully.
Powering the ESP8266 with AC using the Hi-Link HLK-PM03 module
You can use the HLK-PM03 without any circuitry and attach it directly to the ESP8266 VCC pin. However, I don’t recommend doing that. It is advisable to add a protection circuit with a thermal fuse and quick-blow fuses.
Adding capacitors to the HLK-PM03 output is also a good idea to smooth voltage peaks and prevent unexpected resets or unstable behavior powering the ESP8266. We’ve also added a varistor across the mains input to protect the circuit from voltage peaks.
Parts Required
Here’s a list of all the parts required to build the circuit for the project we’ll build:
- HLK-PM03
- ESP8266-01
- ESP8266-01 Serial Adapter or FTDI Programmer
- Relay Module (3.3V)
- Terminal blocks with 2 connectors
- Terminal blocks with 3 connectors
- Electrolytic capacitor 10uF
- Electrolytic capacitor 22uF
- Fuse Slow Blow (200mA)
- Thermal Fuse (72ºC)
- Fuse Quick Blow (630mA)
- Varistor
- Stripboard (Prototyping Circuit Board )
- Wires
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Circuit Diagram
The following figure shows the circuit diagram.
The J1 terminal block is where you should connect mains voltage.
The 3.3V and GND after the capacitors will power the ESP8266-01.
We’ve also added a three terminal block connected to the ESP8266 to get access to 3.3V, GND and GPIO 2 to control an output (relay module). Because we’re dealing with 3.3V you should use a 3.3V relay module like this, for example.
For testing purposes, we’ve soldered the circuit on a prototype board. We plant to build a PCB with this circuit in a future project.
Soldering Thermal Fuses
The thermal fuse used in the circuit is a 73ºC fuse. This means you need to be very careful when soldering it as the heat from the soldering iron can cause it to blow. To learn more about how to solder thermal fuses, we recommend taking a look at the following resources:
Code
In this example we’ve programmed the ESP8266 with Arduino IDE. To program the ESP8266 using Arduino IDE, you need to have the ESP8266 add-on installed. Follow the next tutorial to install the ESP8266 add-on, if you haven’t already.
The following code creates a web server that you can access to control the relay connected to the ESP8266 (GPIO 2) via Wi-Fi in your local network.
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
// Load Wi-Fi library
#include <ESP8266WiFi.h>
// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output2State = "off";
// Assign output variables to GPIO pins
const int output2 = 2;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output2, OUTPUT);
// Set outputs to LOW
digitalWrite(output2, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /2/on") >= 0) {
Serial.println("GPIO 2 on");
output2State = "on";
digitalWrite(output2, HIGH);
} else if (header.indexOf("GET /2/off") >= 0) {
Serial.println("GPIO 2 off");
output2State = "off";
digitalWrite(output2, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #77878A;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP8266 Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 5
client.println("<p>GPIO 2 - State " + output2State + "</p>");
// If the output5State is off, it displays the ON button
if (output2State=="off") {
client.println("<p><a href=\"/2/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/2/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
Setting Your Network Credentials
You need to modify the following lines with your network credentials: SSID and password before uploading the code. The code is well commented on where you should make the changes.
// Replace with your network credentials const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD";
For an explanation of the code, you can read the following tutorial: ESP8266 Web Server with Arduino IDE.
Note: if you’re using an ESP32 with the HLK-PM01 module, you can use the code in this tutorial.
Uploading the Code
Upload the code provided to the ESP8266-01 board using an FTDI programmer or a ESP8266-01 serial adapter.
If you’re using an FTDI programmer like this, you need to make a connection as shown in the following schematic diagram:
After uploading the code, open the Serial Monitor to get the ESP8266-01 IP address. You’ll need it to access the web server.
Demonstration
After uploading the code to your ESP8266 and getting the IP address, place it in the circuit and apply power to the circuit.
Warning: don’t touch the circuit while it is connected to mains voltage!
Open your browser, type the ESP IP address and the following page should load.
You should be able to control the relay remotely on your local network.
Note: we’re using the circuit on a prototype board for demonstration purposes. If you want to use the circuit in a final application, we recommend building a PCB and place it inside a proper project box enclosure.
We plan to build this circuit on a PCB in the upcoming weeks if there’s enough interest, and we’ll share all the files and resources you need to build your own PCB with the HLK-PM03 or HLK-PM01 modules.
Wrapping Up
The HLK-PM01 and HLK-PM03 are converter modules that provide 5V and 3.3V respectively, from mains voltage. This provides an easy way to power your ESP8266 or ESP32 in your IoT and Home Automation projects.
We hope you’ve found this project helpful! If you like these subjects, you may also like:
Dear What is the value of the varistor?
I would not trust these cheap components from China when dealing with high dangerous voltages. In the link you gave some of the HLK-PM03 do not seem to have CE Certification or ANY type of certification. The one you used does.
I think another idea might be to use a 5v phone charger and a 3.3v zener diode to get the required 3.3v. At least with the phone charger you know it’s safe if it’s from a reputable company. The other big advantage of using a phone charger is that the source 220v is far away from the circuit board so no creepage problems. Most homes have plenty of chargers lying around in drawers.
Yet another idea might be to use the 5v phone charger with two or three diode in series to provide the needed voltage drop. Each diode should give about 0.7v drop. Anyway, let’s be very careful out there with high voltages.
Great tip on the zener diodes!
You can have the power supply on one PCB and take the 3.3V output via wires to another PCB with your microcontroller board. Besides when you need to power 10s of such boards, using a phone charger for each is neither economical nor compact. In the end, the HLK-PM03 practically costs almost the same as a cheap Arduino nano or NodeMCU, so it’s not like you’ll lose a lot of money.
“I would not trust these cheap components from China” ??? Do you know that ALL electronics you have and buy come from China? I think your disparaging tone is totally inappropriate
“I think another idea might be to use a 5v phone charger”…. yeah great idea, they’re all made in China too, you know.
There are fine products that are made in China, then there are crap products that come out of China. It depends on who owns the plant and really how much you pay for the item. If you buy a good brand name that is designed in say Germany, and produced in China under German supervision and quality control then the product may well be good and reliable, however it if it made is some back alley garage using junk components that are culled from the bottom of the barrel, then they may well be more dangerous then a loaded handgun in the hands of Charlie Manson.
It comes down to do you trust the product and the vendor, not necessarily the country of origin. At the part CE certified, coming from China you can be certain that it almost assuredly is because because it means “conformité européenne”, what you probably want, but it also means “China Export”. The real CE trademark is expected to be written a certain way, but not all consumers may be aware of this or even check for it.
It comes down to whether you truest your vendor.
For more information see youtube.com/watch?v=xodLuR6C8N4
I use HLK-5M12 in my Arduino + HC-05 project and it worked fine so far… but I agree with you, I want to redesign my project to use phone charger instead of this module because it will be safer, and in case if it blows up due to main power surge or something it would be easy to replace without the need to solder.
Nice.
I have been looking for something like this.
im also a certified electrician so I know my way around high voltage
Excellent article qui va me faire progresser pour l’utilisation des ESP en domotique.
J’attends la suite avec impatience
Merci pour ce que vous faites
Super fine example- Nice to be able to build it all together, and with good security, it is all well protected.
But why two electrolyte capacitors just after each other?
Its to do with the reactance ( resistance) at various frequencies.
Good idea with a PCB, but then there will probably be room for a Wemos D1 Mini, so there are more input / output to work with, and more terminals to connect sensors, push bottons, LED’s with more …
Did something like that!
willem.aandewiel.nl/index.php/2018/12/03/donoff-wifi-enabled-light-dimmer/
Thank you for sharing your project. 😀
Hello, very nice tutorial! But please help me for this component: Fuse Quick Blow (630mA). Can you explain me the function and give me an example for this part? I’m not an electric-specialist. Thank you!
Hi Markus.
The fuse quick blow protects your circuit (the ESP8266) in case of voltage spikes. It can be a bit overkill adding that fuse, and the circuit works without it. However, for safety reasons, and to be sure that it will not burn the ESP8266, we add that fuse. You can get one from any electronics components store.
Regards,
Sara
Hi what is the value of the varistor?
Can this code be used for a SONOFF?
Yes.
Good, that way I can make my own SONOFF units. I have a box of them that need a home.
Interesting article, thank you. Unlike another commenter, I have no problems trusting the Hi-Link devices. CE-certification doesn’t actually mean that much as to a large extend it is self certification.
What I do not trust are the ‘best prices’ on Maker Advisor, a quick trip to Aliexpress already gave me cheaper prices than any mentioned by maker advisor.
Anyway, in spite of the article being interesting, ofcourse one would be better off buying a Sonoff basic: cheaper, tidier, in a case already
I have written at length about the HLK
https://skippy.org.uk/quick-look-at-the-hlk-pm01/
Hi.
Yes, very interesting article.
I’ve taken a look at your article when I was researching to write this tutorial.
Thank you 😀
Very nice tutorial and thank you for sharing! Only the fuse 200 mA keeps blowing up every time I connect the device to the mains. Can you help me out ?
The fuse can be 200mA, but you need to check the fuse voltage too… Is it properly rated for your outlet voltage?
Very interesting!
Can this circuit be used to power another device like a 12v light connected to tge relay?
Also the connections on the stripboard are nor clear? Can you please share the connections step by step for a beginner like me?
Your courses are also the best and to the point!
Hi. Useful article. I was just wondering, why do you use two capacitors? Wouldn’t you get the same effect if you use one capacitor on 33 uF? And how important is the capacitance?
Otto
Hi, What changes do I have to make in this circuit if I use HLK-PM01?
Nice! Was there enough interest to develop a pcb?
Hi.
Yes, but we ended up not building the PCB.
Regards
Sara
Why not?
I actually wanted to give it a try, but I’m too lazy to develop a pcb
Hi I got this setup but the varistor and fuse keep blowing and cant figure out why, i created my own pcb, as per the circuit diagram
Sorry forgot to mention its 240V 630mA slow blow fuse 14D241K varistor and HLK-5M05
What if I use LVR200 240V 2A Tyco Raychem PPTC Resettable Fuse instead of 72°C thermal fuse?
2023 now, isn’t there a safe, simple and compact solution yet?
Here’s something that makes a clean finished product:
tindie.com/products/shencentral/ac-wall-adapter-pcb-for-iot-us-2-pack/
Hi there,
Can I know the part numbers for the following pls?
Fuse Slow Blow (200mA)
Thermal Fuse (72ºC)
Fuse Quick Blow (630mA)
Varistor