ESP32: mains power with the HLK-PM01 isolated supply
Use the HLK-PM01 isolated mains-to-5V module to power an ESP32 from a wall outlet. The safer alternative to wiring a 5V phone charger inline.
The HLK-PM01 is a small ($3-5) isolated mains-to-5V module that converts 120V or 240V AC to 5V DC at up to 600 mA. It is the safest way to power an ESP32 from a wall outlet for a permanent installation.
The alternative (cutting open a USB phone charger) works but is not isolated, not certified, and not safe. The HLK-PM01 is.
This is ESP32 + Arduino territory. Mains wiring is dangerous; you must know what you are doing.
What you need
- HLK-PM01 module (3W, 5V output, 600 mA; about $3-5)
- ESP32 dev board
- Wire nuts or Wago connectors (for mains wiring)
- Enclosure (a junction box or project box)
- Mains cable (with ground wire if you use 3-prong outlets)
The HLK-PM01
The HLK-PM01 is a small switching power supply module. It accepts 100-240V AC input and outputs 5V DC. The input and output are galvanically isolated (no direct electrical connection), which makes it much safer than a non-isolated supply.
The module has 4 pins:
- AC input (L and N, the two AC pins)
- DC output (V+ and V-)
Wiring is straightforward:
Mains L (black/brown) -- HLK-PM01 AC L
Mains N (white/blue) -- HLK-PM01 AC N
HLK-PM01 V+ -- ESP32 5V (or USB 5V pin)
HLK-PM01 V- -- ESP32 GND
The HLK-PM01’s V- (DC ground) is isolated from mains earth/ground. This is the safety feature: even if the mains wiring has a fault, the ESP32’s ground is not at mains voltage.
The “safer than a phone charger” claim
Phone chargers are cheap, small, and have the same voltage output. Why not just use one?
- Isolation: Most phone chargers are isolated, but some cheap ones are not. The HLK-PM01 is documented as isolated.
- Certification: Phone chargers (genuine ones) are UL/CE listed. Knock-offs may not be. The HLK-PM01 is UL/CE listed.
- Repairability: A failed phone charger is replaced, not repaired. The HLK-PM01 is a component you can swap.
- Form factor: The HLK-PM01 has screw terminals for mains, which is safer than splicing a charger cable.
For permanent installations (anything you mount to a wall or leave plugged in for months), use a proper supply like the HLK-PM01. For prototypes and short-term projects, a phone charger is fine.
The power budget
The HLK-PM01 outputs 5V at up to 600 mA (3W). An ESP32 with Wi-Fi active draws up to 500 mA peaks. For a typical sensor project, the average draw is 50-100 mA, well within the budget.
For projects that need more power (e.g. an ESP32 with an attached camera or a 4G modem), use a larger supply like the HLK-PM12 (12W, 5V, 2.4A).
The ground loop problem
If you connect the ESP32’s USB cable (for programming) while the HLK-PM01 is powering it, you create a ground loop. The USB ground and the HLK-PM01 ground are at slightly different potentials, which can cause:
- Random resets when connecting USB
- Damaged USB port on your computer
- Random behavior in the ESP32
The fix: power the ESP32 from the HLK-PM01 only when programming is done. Or add an isolation barrier (a USB isolator, $20) between the computer and the ESP32.
For most projects, the workflow is:
- Program the ESP32 from USB.
- Disconnect USB.
- Connect the HLK-PM01.
- Seal the project in its enclosure.
The enclosure
Mains wiring needs to be inside an enclosure. Options:
- Junction box (PVC or metal): the standard for electrical work. Available at any hardware store. Has knockouts for cable glands.
- Project box (ABS plastic): easier to drill and modify. Less solid than a junction box.
- 3D-printed enclosure: custom fit, but ABS or PETG only (PLA melts if the supply gets hot).
The enclosure must:
- Be rated for the environment (indoor, outdoor, etc.)
- Have proper cable glands (not just holes)
- Allow ventilation (heat dissipation)
- Keep mains and low-voltage wiring physically separated (double insulation or a divider)
The safety rules
- Unplug before wiring. Always.
- Use the correct wire gauge. 14 AWG for 15A circuits, 12 AWG for 20A.
- Use wire nuts or Wago connectors. Not solder, not electrical tape.
- Ground the chassis. If using a metal enclosure, ground it to mains earth.
- Test with a GFCI outlet first. Plug your project into a GFCI outlet; if there is a fault, the GFCI will trip and you will know.
- Get an electrician to check your work. If you have not worked with mains before, pay an electrician to verify your wiring.
The HLK-PM01 has a 5V output that is isolated from mains. The ESP32 running at 5V is safe to touch (no mains voltage present). The unsafe part is the AC input side. Keep that sealed.
What you learned
- The HLK-PM01 is an isolated mains-to-5V module.
- The ESP32 is safe to touch when powered from the HLK-PM01.
- The enclosure must keep mains and low-voltage wiring separated.
- A GFCI outlet is your friend.
When something breaks
- ESP32 does not power up. HLK-PM01 wiring is wrong, or output is shorted.
- HLK-PM01 gets hot. Normal up to about 50°C. Hotter means overloaded; check load current.
- Random resets when USB connected. Ground loop. Disconnect USB.
- GFCI trips. Short circuit in the wiring. Find and fix before powering on again.
What to build next
- The 18650 + TP4056 tutorial covers the battery alternative for portable projects.
- The solar + battery tutorial covers the perpetual-power alternative for outdoor projects.
- The book ESP32 Smart Home covers mains-powered installations with proper safety and code compliance.