ESP8266 - DC Motor Limit Switch

Here, an ESP8266 drives a DC motor with a DRV8833 driver, and a limit switch decides what the motor does. In detail, we are going to learn:

ESP8266 NodeMCU NodeMCU stops or reverses a 5V DC motor with limit switches and a DRV8833 driver

Hardware Preparation

1×ESP8266 NodeMCU ESP-12F
1×Alternatively, ESP8266 D1 Mini NodeMCU ESP-12F
1×Alternatively, ESP8266 NodeMCU ESP-12E (Uno-form)
1×Micro USB Cable
1×Alternatively, ESP8266 NodeMCU ESP-12F (SMD-form)
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Limit Switch (KW12-3)
1×Limit Switch Module
1×Limit Switch (V-153-1C25)
1×Limit Switch (V-155-1C25)
1×Limit Switch (V-156-1C25)
1×5V DC Motor
1×5V Power Adapter for 5V DC motor
1×DC Power Jack
1×DRV8833 Motor Driver Module
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for ESP8266

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of 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:

DRV8833 Motor Driver Pinout

The motor gets its power through a small DRV8833 driver board. Only its channel A is used: IN1 and IN2 take the signals from the NodeMCU, and OUT1 and OUT2 go to the motor. With IN1 HIGH and IN2 LOW the motor turns one way. With IN1 LOW and IN2 HIGH it turns the other way. When both are HIGH, the driver brakes the motor. The MOSFET outputs of the DRV8833 give the motor nearly the full 5V, and the board accepts the 3.3V signals of the ESP8266. The SLEEP pin (EEP on some boards) must stay HIGH, or the driver sleeps. The chip pulls it LOW by itself, so a wire from SLEEP to the 3V3 pin of the NodeMCU keeps it awake. The FAULT pin stays unconnected.

Wiring Diagram

This tutorial provides the ESP8266 codes for two cases: One DC motor + one limit switch, One DC motor + two limit switches. Jumper wires go straight from the NodeMCU pins to the parts.

  • Wiring diagram between the DC motor and a limit switch
The wiring diagram between ESP8266 NodeMCU and NodeMCU DC Motor Limit Switch DRV8833

This image is created using Fritzing. Click to enlarge image

Part Pin ESP8266 NodeMCU / Power
DRV8833 IN1 D5
DRV8833 IN2 D6
DRV8833 GND GND, and (-) of the 5V adapter via the DC power jack
DRV8833 VCC (+) of the 5V adapter via the DC power jack
DRV8833 SLEEP 3V3
DRV8833 OUT1, OUT2 the two wires of the DC motor
Limit switch D1 and GND
  • Wiring diagram between the DC motor and two limit switches
The wiring diagram between ESP8266 NodeMCU and NodeMCU DC Motor Two Limit Switches DRV8833

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

Part Pin ESP8266 NodeMCU / Power
DRV8833 IN1 D5
DRV8833 IN2 D6
DRV8833 GND GND, and (-) of the 5V adapter via the DC power jack
DRV8833 VCC (+) of the 5V adapter via the DC power jack
DRV8833 SLEEP 3V3
DRV8833 OUT1, OUT2 the two wires of the DC motor
Limit switch 1 D1 and GND
Limit switch 2 D2 and GND

ESP8266 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. To stop the motor, it sets IN1 and IN2 both HIGH, which is the brake mode of the DRV8833.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-dc-motor-limit-switch */ #include <ezButton.h> #define PIN_IN1 D5 // The ESP8266 pin D5 connected to the IN1 pin of the DRV8833 #define PIN_IN2 D6 // The ESP8266 pin D6 connected to the IN2 pin of the DRV8833 ezButton limitSwitch(D1); // create ezButton object for pin ESP8266 pin D1 void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds // initialize digital pins as outputs. pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT); digitalWrite(PIN_IN1, HIGH); // IN1 HIGH, IN2 LOW: motor A spins clockwise at max speed digitalWrite(PIN_IN2, LOW); } void loop() { limitSwitch.loop(); // MUST call the loop() function first if (limitSwitch.isPressed()) { Serial.println(F("The limit switch: TOUCHED")); digitalWrite(PIN_IN1, HIGH); // IN1 and IN2 both HIGH: brake, the motor stops fast digitalWrite(PIN_IN2, HIGH); Serial.println(F("The DC motor is STOPPED")); } }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Connect ESP8266 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 ESP8266GetStarted.com
  • Click Install button to install ezButton library.
ESP8266 NodeMCU button library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • Plug in the 5V adapter to power the motor
  • 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
  • Nothing moves at all? Check that SLEEP of the DRV8833 is wired to 3V3
  • The result on Serial Monitor looks like below
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
The limit switch: TOUCHED The DC motor is STOPPED
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2

Code Explanation

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

ESP8266 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 ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-dc-motor-limit-switch */ #include <ezButton.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define PIN_IN1 D5 // The ESP8266 pin D5 connected to the IN1 pin of the DRV8833 #define PIN_IN2 D6 // The ESP8266 pin D6 connected to the IN2 pin of the DRV8833 ezButton limitSwitch(D1); // create ezButton object for pin ESP8266 pin D1 int direction = DIRECTION_CW; void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds // initialize digital pins as outputs. pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT); digitalWrite(PIN_IN1, HIGH); // IN1 HIGH, IN2 LOW: motor A spins clockwise at max speed digitalWrite(PIN_IN2, LOW); } 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(PIN_IN1, HIGH); // control motor A spins clockwise digitalWrite(PIN_IN2, LOW); } else { Serial.println(F("ANTI-CLOCKWISE")); digitalWrite(PIN_IN1, LOW); // control motor A spins anti-clockwise digitalWrite(PIN_IN2, HIGH); } } }

Detailed Instructions

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • 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
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
The limit switch: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch: TOUCHED The direction -> CLOCKWISE
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2

ESP8266 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 ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-dc-motor-limit-switch */ #include <ezButton.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define PIN_IN1 D5 // The ESP8266 pin D5 connected to the IN1 pin of the DRV8833 #define PIN_IN2 D6 // The ESP8266 pin D6 connected to the IN2 pin of the DRV8833 ezButton limitSwitch_1(D1); // create ezButton object for pin ESP8266 pin D1 ezButton limitSwitch_2(D2); // create ezButton object for pin ESP8266 pin D2 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(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT); digitalWrite(PIN_IN1, HIGH); // IN1 HIGH, IN2 LOW: motor A spins clockwise at max speed digitalWrite(PIN_IN2, LOW); } 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(PIN_IN1, HIGH); // control motor A spins clockwise digitalWrite(PIN_IN2, LOW); } else { Serial.println(F("ANTI-CLOCKWISE")); digitalWrite(PIN_IN1, LOW); // control motor A spins anti-clockwise digitalWrite(PIN_IN2, HIGH); } prev_direction = direction; } }

Detailed Instructions

  • Click Upload button on Arduino IDE to upload code to ESP8266
  • 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
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
The limit switch 1: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch 2: TOUCHED The direction -> CLOCKWISE
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!