ESP32 - DC Motor - Limit Switch

In this ESP32 tutorial, we are going to learn how to use ESP32 to control DC motor by limit switch and L298N driver. In detail, we are going to learn:

In this tutorial on ESP32, we'll explore the process of utilizing the ESP32 to manage a DC motor using a limit switch and an L298N driver. Specifically, we'll cover the following aspects:

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Limit Switch (KW12-3)
1×Limit Switch (V-156-1C25)
1×5V DC Motor
1×5V Power Adapter for 5V DC motor
1×DC Power Jack
1×L298N Motor Driver Module
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables.

Introduction to DC Motor and Limit Switch

If you do not know about DC motor and limit switch (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

This tutorial provides the ESP32 codes for two cases: One DC motor + one limit switch, One DC motor + two limit switches.

  • Wiring diagram between the DC motor and a limit switch
ESP32 DC motor and limit switch wiring diagram

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram between the DC motor and two limit switches
ESP32 DC motor and two limit switches wiring diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code - Stop DC Motor by a Limit Switch

The below code make a DC motor spin infinitely and stop immediately when a limit switch is touched

/* * 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 } }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Connect ESP32 to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search “ezButton”, then find the button library by ArduinoGetStarted.com
  • Click Install button to install ezButton library.
ESP32 button library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • If the wiring is correct, you will see the motor rotates clockwise direction.
  • Touch the limit switch
  • You will see the motor is stopped immediately
  • The result on Serial Monitor looks like below
COM6
Send
The limit switch: TOUCHED The DC motor is STOPPED The DC motor is STOPPED The DC motor is STOPPED The DC motor is STOPPED
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

Read the line-by-line explanation in comment lines of code!

ESP32 Code - Change Direction of DC Motor by a Limit Switch

The below code make a DC motor spin infinitely and change its direction when a limit switch is touched

/* * 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 DIRECTION_CCW -1 #define DIRECTION_CW 1 #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 int direction = DIRECTION_CW; 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")); direction *= -1; // change direction Serial.print(F("The direction -> ")); if (direction == DIRECTION_CW) { Serial.println(F("CLOCKWISE")); digitalWrite(IN1_PIN, HIGH); // control motor A spins clockwise digitalWrite(IN2_PIN, LOW); // control motor A spins clockwise } else { Serial.println(F("ANTI-CLOCKWISE")); digitalWrite(IN1_PIN, LOW); // control motor A spins anti-clockwise digitalWrite(IN2_PIN, HIGH); // control motor A spins anti-clockwise } } }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • If the wiring is correct, you will see the motor spins in the clockwise direction.
  • Touch the limit switch
  • You will see the DC motor's direction is changed to the anti-clockwise
  • Touch the limit switch again
  • You will see the DC motor's direction is changed to clockwise
  • The result on Serial Monitor looks like below
COM6
Send
The limit switch: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch: TOUCHED The direction -> CLOCKWISE
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

ESP32 Code - Change Direction of DC Motor by two Limit Switches

The below code make a DC motor spin infinitely and change its direction when one of two limit switches is touched

/* * 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 DIRECTION_CCW -1 #define DIRECTION_CW 1 #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_1(25); // create ezButton object that attach to pin GPIO25 ezButton limitSwitch_2(26); // create ezButton object that attach to pin GPIO26 int direction = DIRECTION_CW; int prev_direction = DIRECTION_CW; void setup() { Serial.begin(9600); limitSwitch_1.setDebounceTime(50); // set debounce time to 50 milliseconds limitSwitch_2.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_1.loop(); // MUST call the loop() function first limitSwitch_2.loop(); // MUST call the loop() function first if (limitSwitch_1.isPressed()) { direction *= -1; // change direction Serial.println(F("The limit switch 1: TOUCHED")); } if (limitSwitch_2.isPressed()) { direction *= -1; // change direction Serial.println(F("The limit switch 2: TOUCHED")); } if (prev_direction != direction) { Serial.print(F("The direction -> ")); if (direction == DIRECTION_CW) { Serial.println(F("CLOCKWISE")); digitalWrite(IN1_PIN, HIGH); // control motor A spins clockwise digitalWrite(IN2_PIN, LOW); // control motor A spins clockwise } else { Serial.println(F("ANTI-CLOCKWISE")); digitalWrite(IN1_PIN, LOW); // control motor A spins anti-clockwise digitalWrite(IN2_PIN, HIGH); // control motor A spins anti-clockwise } prev_direction = direction; } }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • If the wiring is correct, you will see the motor spins in the clockwise direction.
  • Touch the limit switch 1
  • You will see the DC motor's direction is changed to anti-clockwise
  • Touch the limit switch 2
  • You will see the DC motor's direction is changed to clockwise
  • The result on Serial Monitor looks like below
COM6
Send
The limit switch 1: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch 2: TOUCHED The direction -> CLOCKWISE
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES