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

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.

/*
* 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 ENA_PIN 19 // ESP32 pin GPIO19 connected to the IN1 pin L298N
#define IN1_PIN 18 // ESP32 pin GPIO18 connected to the IN2 pin L298N
#define IN2_PIN 17 // ESP32 pin GPIO17 connected to the EN1 pin L298N

ezButton limitSwitch(25); // create ezButton object that attach to pin GPIO25

void setup() {
Serial.begin(9600);

limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds

// initialize digital pins as outputs.
pinMode(ENA_PIN, OUTPUT);
pinMode(IN1_PIN, OUTPUT);
pinMode(IN2_PIN, OUTPUT);

digitalWrite(ENA_PIN, HIGH); // max speed
digitalWrite(IN1_PIN, HIGH); // control motor A spins clockwise
digitalWrite(IN2_PIN, LOW); // control motor A spins clockwise
}

void loop() {
limitSwitch.loop(); // MUST call the loop() function first

if (limitSwitch.isPressed()) {
Serial.println(F("The limit switch: TOUCHED"));
digitalWrite(IN1_PIN, LOW); // stop motor
digitalWrite(IN2_PIN, LOW); // stop motor
}
}

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