ESP32 - DC Motor - Limit Switch
by DIYables in Circuits > Microcontrollers
11 Views, 0 Favorites, 0 Comments
ESP32 - DC Motor - Limit Switch
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://esp32io.com/tutorials/esp32-dc-motor-limit-switch
This build uses an ESP32 to control a DC motor through an L298N driver, and stops or reverses the motor when a limit switch is touched. You will wire one DC motor with one limit switch, then extend it to two limit switches so the motor reverses direction whenever either switch is pressed.
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)
- 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
- L298N Motor Driver Module
- 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
Connect ESP32 GPIO19 to the ENA pin on the L298N, GPIO18 to IN1, and GPIO17 to IN2, which drive the motor. The limit switch connects to GPIO25. For the two-switch version, wire a second limit switch to its own ESP32 pin the same way. Power the DC motor from the 5V power adapter rather than the ESP32 itself.
How It Works
The motor runs continuously in one direction once ENA, IN1 and IN2 are set. The ezButton library reads the limit switch with debouncing so a single press is not read as multiple triggers. In the stop version, pressing the switch sets both IN1 and IN2 low, which halts the motor. In the direction-change versions, pressing a switch flips IN1 and IN2 so the motor reverses instead of stopping. With two switches, either one triggers a reversal.
Code
The sketch includes the ezButton library and defines ENA_PIN, IN1_PIN and IN2_PIN for the L298N, plus a limitSwitch object on GPIO25. Setup configures these as outputs, sets a 50ms debounce time on the switch, and starts the motor spinning clockwise. The loop calls limitSwitch.loop() first, then checks isPressed(); when true it prints a message to Serial and changes IN1/IN2 to either stop or reverse the motor, depending on which version of the code you upload.
esp32-dc-motor-limit-switch.ino - the sketch from the tutorial, unchanged.
Things That Go Wrong
If the motor never stops or reverses, check that limitSwitch.loop() is called at the top of loop(), since skipping it breaks debouncing and isPressed() detection. If the motor does not spin at all, confirm ENA, IN1 and IN2 are wired to the pins defined in the code and that the motor has its own 5V supply. If direction seems backwards from what you expect, swap IN1 and IN2 wiring or logic.
Full Tutorial
The complete tutorial, wiring diagram and code explanation: https://esp32io.com/tutorials/esp32-dc-motor-limit-switch