How to Interface a DC Motor with Arduino

This site contains affiliate links to products. We may receive a commission for purchases made through these links.

The DC or direct current motor is one of the most popular kinds of motors and comprises two leads, a negative and positive one. After directly connecting these two leads to the battery, your motor will start rotating. There are two aspects of the DC motor which are under your control including:

  • Speed, meaning you can adjust the motor’s voltage by changing the supply voltage it’s getting.
  • You can also adjust the direction of a motor by changing the applied voltage at the terminal. When doing this, you’ll need to have a motor.

Now that you understand how the DC motor functions, this article seeks to guide you on the steps to follow when interfacing with an Arduino. If this is something you’re looking to learn, read through this detailed article and get valuable insights into doing this.

Hardware Needed

Before you even learn how to interface a DC motor using the Arduino, getting the necessary hardware should be your first step. These include:

  • Jumper wires
  • 1 DC motor
  • 1 Arduino Uno
  • 1 Solderless Breadboard
  • 1 L293D IC
  • One 10K Potentiometer 
  • 1 Power Supply/ Battery Case

Once you’ve assembled all of this hardware, feel free to start interfacing the DC motor with the Arduino.

Steps To Follow When Interfacing DC Motor with Arduino 

The first thing you should do is connect all the components to the breadboard. How many motors driver pins you connect depends on the number of motors being connected. The steps you should follow are:

  1. Connect your Arduino to a source of power, for instance, your computer.
  2. Connect the 5V and GND on the Arduino to a single side of the breadboard. After doing this, use jumper wires to lengthen them in the other direction.
  3. Position the L293D in the middle of the breadboard, with half of these pins on both sides of the breadboard.
  4. Connect the 5V to the Vs., Vss, as well as Enable 1 on the L293D.
  5. Plug-in the digital output pins 4,7,11 on L293D’s both input one and input 2.
  6. Connect GND of the Arduino to both GND pins located on the similar side of the L293D.
  7. The last thing to do is connect the L293D’s output one and output 2 to the motor pins.

The motor driver’s Vs pin delivers power to the motor. If the motor you’re using needs more power than what is being produced by the Arduino, connecting a battery pack or external battery will do the trick. After doing this, connect the Vs pin of the L293D to the positive lead and the GND to the negative lead on your breadboard.

Connect the components! 🙂
How to Interface a DC Motor with Arduino

The Code Setup

If you want to understand the Arduino IDE better, here are the steps to follow:

  • Make sure to choose the correct port under Tools > Port Ensure to also select the correct Arduino board under Tools > Board 
  • Click on File > New when looking to create a new sketch
  • Copy and paste the code below, placing the motorPin1 and motorpin2 values when using any other pin on the Arduino except for 4 and 7.

After the sketch is successfully uploaded, the motor starts moving in one direction. When looking to change the motor’s direction, you need to revise LOW and HIGH in the loop function.

Code:

//For defining the pin numbers
int motorPin1 = 4;
int motorPin2 = 7;

void setup() {
//For setting the pin modes
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
}

void loop() {
//For turning the motor in one direction
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  delay(2000);
}

Errors You Should Expect When Interfacing DC Motor With Arduino 

Several errors pop up as you interface the Arduino to the DC motor. Due to these errors, the module features an onboard 5v regulator that can either be disabled or enabled with a jumper. You can use a 5V regulator should the motor supply voltage reach 12V, with the 5V pin being used as the output, maybe for instance, to power the Arduino Uno board.

Nonetheless, if your motor has a voltage exceeding 12V, make sure to pull out the motor. Doing this is crucial because these high voltages lead to the destruction of the onboard 5V regulator. Therefore, for this instance, use the 5V as the input because it needs to be connected to a 5V source of power to guarantee the smooth operation of the IC.

Motors that have an input voltage between 5 to12V, as well as a 2A current, can be controlled by the Arduino with a motor controller. The reason for this is that the voltage, in this case, is over 12V, and as a result, causes the destruction of the IC found in the motor controller. In addition to this, the Arduino gets affected to a great extent by this.

You also should never share both the Arduino and external battery to the GND pin. If you do this, it’ll lead to the powering up of the Arduino, which causes the battery to dry a lot faster. Moreover, the IC’s voltage drop is 2V, meaning you won’t be able to achieve the motor’s full speed.

arduino dc motor 1

Safety Precautions Before Turning On The Arduino 

You’ll need to observe some safety precautions before switching on the Arduino. These include:

  • Double-checking the polarity to confirm whether 5V/Vin and GND pins have been mixed up by any chance. If this does happen, it can lead to the destruction of the Arduino board.
  • Ensure the 5V input is steady and stable when using GND and 5V pins to power your Arduino.
  • When the AC-DC adapter and barrel connector are used to deliver power to the Arduino, ensure the adapter’s output ranges between 7 to 12V. It’s essential to stay within these recommended limits to safeguard the voltage regulator from too much heating.

Conclusion 

Learning how to connect the DC motor with the Arduino usually seems complicated to most individuals. If you also thought so, reading through this detailed guide has taken you through the steps on what to do. Therefore, you can do all this with this knowledge in mind without any hassle.

About The Author