ESP32 MicroPython Relay
by DIYables in Circuits > Microcontrollers
10 Views, 0 Favorites, 0 Comments
ESP32 MicroPython Relay
This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-relay
This build uses an ESP32 running MicroPython to switch a relay on and off, letting the low-voltage ESP32 control a high-voltage device such as an LED strip powered from a 12V adapter. The relay acts as the go-between, isolating the ESP32's GPIO signal from the higher voltage and current on the output side.
Supplies
Everything below is what the build uses. The links go to the parts we make and test.
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- ESP32 Uno-form board
- ESP32 S3 Uno-form board
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Relay
- LED Strip
- DC Power Jack
- Breadboard
- Jumper Wires
- Screw Terminal Expansion Board for ESP32
- Breakout Expansion Board for ESP32
- Power Splitter for ESP32
- DIYables ESP32 Starter Kit (ESP32 included)
- DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
The relay has an input side and an output side. On the input side, wire DC- to GND, DC+ to VCC (5V), and IN to a GPIO pin on the ESP32. On the output side, which is typically a screw terminal, wire COM and NO (normally open) to the high-voltage device, such as the LED strip powered by the 12V adapter. You can build this on a breadboard with jumper wires, or use a screw terminal expansion board for the ESP32 for a cleaner connection. Power the ESP32 either through its USB cable or through the Vin pin.
How It Works
The relay is a switch triggered by the signal on its IN pin. Depending on the module, it may use LOW level trigger or HIGH level trigger mode, meaning the relay activates when IN is pulled to 0V or to 5V respectively. The output side also has a default state: normally open or normally closed, where 'normally' refers to the state when IN is held LOW.
The ESP32 only needs to set its GPIO pin, the one wired to IN, to LOW or HIGH to switch the relay, and in turn the connected high-voltage device, on or off.
Code
The MicroPython code configures the GPIO pin connected to the relay's IN pin as a digital output. From there, the program writes HIGH or LOW to that pin to turn the relay on or off, which switches the connected high-voltage device, like the LED strip, accordingly.
esp32-micropython-relay.py - the sketch from the tutorial, unchanged.
Full Tutorial
The complete tutorial, wiring diagram and code explanation: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-relay