Arduino - DC Motor - Limit Switch

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

About 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:

A small DRV8833 module sits between Arduino and the motor, and only its channel A is in use. Arduino talks to IN1 and IN2; the motor hangs on OUT1 and OUT2. IN1 HIGH with IN2 LOW turns the motor one way, IN1 LOW with IN2 HIGH turns it the other way, and both HIGH make the driver brake hard. SLEEP needs a HIGH level as well, because the chip pulls it LOW inside and then goes to sleep, so a wire links SLEEP to 5V of Arduino. FAULT is not used.

DRV8833 Motor Driver Pinout

Wiring Diagram

This tutorial provides the Arduino codes for two cases: One DC motor + one limit switch, One DC motor + two limit switches. In both cases, IN1 and IN2 of the DRV8833 go to pins 10 and 9 of Arduino, SLEEP goes to 5V of Arduino, GND of the module goes to GND of Arduino, and the 5V power adapter feeds VCC and GND of the module through the DC power jack.

  • Wiring diagram between the DC motor and a limit switch
Arduino DC Motor Limit Switch DRV8833 Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Part Pin Connect To
DRV8833 IN1 Arduino pin 10
DRV8833 IN2 Arduino pin 9
DRV8833 SLEEP Arduino 5V
DRV8833 GND Arduino GND and - of the 5V power adapter
DRV8833 VCC + of the 5V power adapter
DRV8833 OUT1, OUT2 the two wires of the DC motor
Limit switch one pin Arduino pin A0
Limit switch other pin Arduino GND

A motor shakes, and jumper wires can slip out. A Proto Screw Terminal Block Shield or a Screw Shield V2 fixes this: plug it on the Arduino Uno and tighten each wire under the screw of its pin. Nothing else changes.

Arduino DC Motor Limit Switch DRV8833 Screw Shield Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram between the DC motor and two limit switches
Arduino DC Motor Two Limit Switches DRV8833 Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Keep the DRV8833 wires from the first diagram. Then add the second switch: switch 1 sits between pin A0 and GND, switch 2 between pin A1 and GND.

With a Screw Shield V2 or a Proto Screw Terminal Block Shield, the second switch simply takes the screw terminal of pin A1:

Arduino DC Motor Two Limit Switches DRV8833 Screw Shield Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino 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

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-dc-motor-limit-switch */ #include <ezButton.h> #define IN1_PIN 10 // The Arduino pin connected to the IN1 pin of DRV8833 #define IN2_PIN 9 // The Arduino pin connected to the IN2 pin of DRV8833 ezButton limitSwitch(A0); // create ezButton object that attach to pin A0 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); // motor A spins clockwise at max speed: IN1 HIGH, IN2 LOW 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 HIGH, the motor stops at once digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, HIGH); Serial.println(F("The DC motor is STOPPED")); } }

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Navigate 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.
Arduino button library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • If the wiring is correct, you will see the motor rotates clockwise direction. If the motor does not move at all, check the wire from SLEEP to 5V.
  • Touch the limit switch
  • You will see the motor is stopped immediately. The code sets IN1 and IN2 HIGH, so the DRV8833 brakes the motor.
  • The result on Serial Monitor looks like below
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
The limit switch: TOUCHED The DC motor is STOPPED
Ln 11, Col 1
Arduino Uno on COM15
2

Code Explanation

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

Arduino 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

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-dc-motor-limit-switch */ #include <ezButton.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 // constants won't change #define IN1_PIN 10 // The Arduino pin connected to the IN1 pin of DRV8833 #define IN2_PIN 9 // The Arduino pin connected to the IN2 pin of DRV8833 ezButton limitSwitch(A0); // create ezButton object that attach to pin A0 int direction = DIRECTION_CW; 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); // motor A spins clockwise at max speed: IN1 HIGH, IN2 LOW 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")); direction *= -1; // change direction Serial.print(F("The direction -> ")); if (direction == DIRECTION_CW) { Serial.println(F("CLOCKWISE")); digitalWrite(IN1_PIN, HIGH); // clockwise: IN1 HIGH, IN2 LOW digitalWrite(IN2_PIN, LOW); } else { Serial.println(F("ANTI-CLOCKWISE")); digitalWrite(IN1_PIN, LOW); // anti-clockwise: IN1 LOW, IN2 HIGH digitalWrite(IN2_PIN, HIGH); } } }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • 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
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' 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
Arduino Uno on COM15
2

Arduino 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

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-dc-motor-limit-switch */ #include <ezButton.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define IN1_PIN 10 // The Arduino pin connected to the IN1 pin of DRV8833 #define IN2_PIN 9 // The Arduino pin connected to the IN2 pin of DRV8833 ezButton limitSwitch_1(A0); // create ezButton object that attach to pin A0 ezButton limitSwitch_2(A1); // create ezButton object that attach to pin A1 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(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); // motor A spins clockwise at max speed: IN1 HIGH, IN2 LOW digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, 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(IN1_PIN, HIGH); // clockwise: IN1 HIGH, IN2 LOW digitalWrite(IN2_PIN, LOW); } else { Serial.println(F("ANTI-CLOCKWISE")); digitalWrite(IN1_PIN, LOW); // anti-clockwise: IN1 LOW, IN2 HIGH digitalWrite(IN2_PIN, HIGH); } prev_direction = direction; } }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • 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
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' 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
Arduino Uno on COM15
2

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES