The following circuit diagram shows how to connect 5V relay module with ESP32-S3 to control a DC motor.
This MicroPython script continuously runs an infinite loop (while True) that monitors the state of a pushbutton switch connected to input GP40 (held HIGH by an internal pull-up resistor), and controls an active-low relay module wired to output GP5. When the switch is pressed, it pulls GP40 to 0 (LOW), causing the script to output 0 (LOW) on GP5, which energizes the active-low relay to close its contacts and turn the motor on; conversely, when the switch is released, GP40 returns to 1 (HIGH), prompting the script to output 1 (HIGH) on GP5 to de-energize the relay and turn the motor off, with a short 0.1-second sleep delay included in each cycle to prevent excessive CPU usage and debounce the switch.
Here is a complete breakdown of how your circuit is wired, how each component interacts, and how the MicroPython firmware controls everything.
1. How the ESP32-S3 is Powered
- Power Supply (B1 / VCC): The ESP32-S3 board is powered by a 5V DC source connected to its VCC / 5V pin.
- Ground (GND): The system shares a common ground rail (GND), connecting the battery, motor, relay module, and microcontroller together to ensure a complete return path for current.
2. How the ESP32-S3 is Connected to the Relay
- Control Signal (GP5):
- GPIO pin 5 on the ESP32-S3 is wired to the IN pin of the relay module.
- The ESP32-S3 acts as the brain: it outputs either a HIGH (3.3V) or LOW (0V) signal on GP5 to command the relay.
- Relay Power:
- The relay module's power pins are connected to +5V (V) and GND (G) so that its internal transistor/optocoupler and electromagnetic coil have enough power to operate.
3. How the Motor and Relay Switch are Connected
A relay acts as an electrically operated switch that isolates the low-voltage control circuit (ESP32-S3) from the high-power circuit (12V motor):
- High-Power Source (BAT1): A 12V battery powers the load. Its positive terminal is connected to the relay's R_COM (Common, pin 5) terminal.
- Motor Control Path:
- One side of the 12V DC motor is connected to the relay's R_NO (Normally Open, pin 7) terminal.
- The other side of the motor is connected directly to GND.
- Switching Action:
- Relay OFF: The switch inside the relay is open, so no current flows from R_COM to R_NO. The motor stays OFF.
- Relay ON: When commanded, the relay coil energizes, pulling the internal switch closed between R_COM and R_NO. This completes the 12V circuit, allowing current to flow from the battery through the relay and into the motor, turning it ON.
4. How the Input Switch is Connected
- Pushbutton (SWITCH):
- Connected between GP40 (on the right side of the ESP32-S3) and GND.
- When pressed, it pulls GP40 to 0V (LOW). When released, the internal pull-up resistor keeps GP40 at 3.3V (HIGH).
5. Explanation of the MicroPython Code (main.py)
from machine import Pin
from time import sleep
# Configure Relay pin on GP5 (Connected to Active-Low Relay Module IN)
# For Active-Low: LOW (0) turns the relay ON, HIGH (1) turns it OFF
relay = Pin(5, Pin.OUT)
# Configure Switch/Button pin on GP40 (Right side of the ESP32-S3 board)
switch = Pin(40, Pin.IN, Pin.PULL_UP)
print("Starting ESP32-S3 Active-Low Relay & Switch Control...")
while True:
# Read switch state (0 when pressed)
if switch.value() == 0:
print("Switch pressed on GP40 -> Motor ON (Relay LOW)")
relay.value(0) # Active-Low: LOW turns relay ON
else:
print("Switch released on GP40 -> Motor OFF (Relay HIGH)")
relay.value(1) # Active-Low: HIGH turns relay OFF
sleep(0.1) # Polling delay / debounce
If you want the ESP32-S3 Devkit Proteus library and 5V relay library for simulation, click on the download link below:
You can find other proteus models in the following download link:
See also:
