This build uses an ESP32 to run a 5V DC motor through a DRV8833 driver, and stop or reverse it the moment a limit switch gets pressed. It's a neat pattern if you're coming from software: a physical event (a switch closing) directly changes the state of a motor, no polling loop logic required beyond reading a pin. Useful for anything that needs to stop at a mechanical end point, like a slide, gate, or carriage.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 - DC Motor - Limit Switch
What you need
- 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)
- Limit Switch (KW12-3)
- Limit Switch Module
- Limit Switch (V-153-1C25)
- Limit Switch (V-155-1C25)
- Limit Switch (V-156-1C25)
- 5V DC Motor
- 5V Power Adapter for 5V DC motor
- DC Power Jack
- 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 DRV8833 only needs channel A here: IN1 and IN2 connect to ESP32 GPIO27 and GPIO26, and OUT1/OUT2 go to the motor terminals. The SLEEP pin (sometimes labeled EEP) is pulled LOW inside the chip by default, which puts the driver to sleep, so wire it to the ESP32's 3.3V pin to keep the driver awake. Leave FAULT unconnected. The limit switch connects to GPIO32. A separate 5V adapter powers the motor, while the ESP32 itself stays powered over USB. If the setup adds a second limit switch, it wires in the same way as the first, just to a different GPIO.
How it works
The DRV8833 exists because the ESP32's pins can't drive a motor directly: they only output 3.3V logic, not enough current or voltage. The driver's MOSFET outputs take that 3.3V signal and pass almost the full 5V through to the motor, staying cool while doing it. Direction is set by the combination on IN1/IN2: HIGH/LOW spins the motor clockwise, LOW/HIGH spins it anti-clockwise, and HIGH/HIGH acts as a fast brake, stopping the motor immediately.
Code
The sketch uses the ezButton library to debounce the limit switch, since a mechanical switch bounces and would otherwise register several presses for one touch. It sets IN1 and IN2 to spin the motor clockwise at startup, then on every loop checks limitSwitch.isPressed(). In the stop version, a press sets both IN1 and IN2 HIGH to brake the motor. Other versions of the sketch instead flip IN1/IN2 to reverse direction, either from one limit switch or from whichever of two switches gets triggered.
esp32-dc-motor-limit-switch.ino
/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-dc-motor-limit-switch
*/
#include <ezButton.h>
#define IN1_PIN 27 // ESP32 pin GPIO27 connected to the IN1 pin of the DRV8833
#define IN2_PIN 26 // ESP32 pin GPIO26 connected to the IN2 pin of the DRV8833
ezButton limitSwitch(32); // create ezButton object that attach to pin GPIO32
void setup() {
Serial.begin(9600);
limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds
// initialize digital pins as outputs.
pinMode(IN1_PIN, OUTPUT);
pinMode(IN2_PIN, OUTPUT);
// the DRV8833 has no enable pin: IN1 HIGH and IN2 LOW = full speed, clockwise
digitalWrite(IN1_PIN, HIGH);
digitalWrite(IN2_PIN, LOW);
}
void loop() {
limitSwitch.loop(); // MUST call the loop() function first
if (limitSwitch.isPressed()) {
Serial.println(F("The limit switch: TOUCHED"));
// brake: IN1 and IN2 both HIGH stop the motor fast
digitalWrite(IN1_PIN, HIGH);
digitalWrite(IN2_PIN, HIGH);
Serial.println(F("The DC motor is STOPPED"));
}
}
Things that go wrong
If the motor won't move at all, check the SLEEP wire first: without it tied to 3.3V, the DRV8833 stays asleep no matter what signals you send. If the limit switch seems to trigger multiple times from a single press, increase the debounce time passed to setDebounceTime(). And remember the motor runs off its own 5V adapter, not the ESP32's USB power, so check that supply separately if the motor is dead but the board is clearly running.
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://esp32io.com/tutorials/esp32-dc-motor-limit-switch

Top comments (0)