Design a site like this with
Get started

Power ESP8266 with Mains Voltage using Hi-Link HLK-PM03 Converter

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.

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.

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

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:

  • Careful when you solder thermal fuses
  • Youtube video – How to solder thermal fuses


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.


// 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() {
  // 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 ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  // Print local IP address and start web server
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");

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 =;             // 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("Connection: close");
            // 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>");
            // The HTTP response ends with another blank line
            // Break out of the while loop
          } 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
    Serial.println("Client disconnected.");

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.


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:


Published by Gnd_To_Vcc

Here to spread my knowledge . Knowledge should always be spread not stored.

One thought on “Power ESP8266 with Mains Voltage using Hi-Link HLK-PM03 Converter

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: