Arduino - DC Motor

In this tutorial, we are going to learn:

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×L298N Motor Driver Module
1×5V DC Motor
1×5V Power Adapter for 5V DC motor
1×DC Power Jack
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About DC Motor

DC Motor Pinout

DC Motor has two wires:

  • Positive wire: usually red
  • Negative wire: usually black
DC Motor Pinout

How It Works

When you buy a DC motor, you need to know what voltage DC motor work. Let's take a 12V DC motor as an example.

When you power the 12V DC motor by a 12V power source:

  • 12V and GND to the positive wire and negative wire, respectively: the DC motor rotates at maximum speed in the clockwise direction
  • 12V and GND to the negative wire and positive wire, respectively: the DC motor rotates at maximum speed in the anti-clockwise direction

As described above, when the power pole is swapped between two wires of the DC motor, the rotating direction is reversed. This method is used to control the direction of the DC motor. Of course, not by changing manually but by programming.

If we provide power to DC motors below 12V, the motor still rotates but not at maximum speed. It means if we change the voltage of the power supply, we can change the speed of the DC motor. However, this method is not used in practice because of the difficulty in controlling the voltage of the power source. Instead, we fix the voltage of the power source and control the speed of the DC motor via a PWM signal. The more duty cycle the PWM is, the higher speed the DC motor rotates.

How to control DC motor

How to control DC motor using Arduino

Controlling DC motor includes two factors: speed and direction. Arduino can generate the PWM signal. However, this PWM signal has low voltage and current, We cannot use it to control the DC motor. We need to use a hardware driver in between Arduino and DC motor. The driver does two works:

  • Amplify the PWM signal from Arduino (current and voltage) → for speed control
  • Receive the control signal from Arduino to swap pole of power supply → for direction control.
Arduino control DC motor

※ NOTE THAT:

  • This tutorial can be applied to all DC motors. 12V DC motor is just an example.
  • When you controls 5V DC motor, although Arduino pin outputs 5V (the same as DC motor voltage), you still needs a driver in between Arduino and DC motor because the Arduino pin does not provide enough the current for DC motor.

There are many kinds of the chip, modules (e.g. L293D, L298N) can be used as DC motor drivers. In this tutorial, we will use the L298N driver.

About L298N Driver

L298N Driver can be used to control DC motor and stepper motor. In this tutorial, we learn how to use it to control the DC motor.

L298N Driver Pinout

L298N Driver Pinout

L298N Driver Pinout

L298N Driver can control two DC motor independently at the same time, called motor A and motor B. L298N Driver has 13 pins:

The common pins for both motors:

  • VCC pin: supplies power for the motor. It can be anywhere between 5 to 35V.
  • GND pin: is a common ground pin, needs to be connected to GND (0V).
  • 5V pin: supplies power for L298N module. It can be supplied by 5V from Arduino.

Motor A pins (Channel A):

  • ENA pins: are used to control the speed of Motor A. Removing the jumper and connecting this pin to PWM input will let us control the speed of Motor A.
  • IN1 & IN2 pins: are used to control the spinning direction of Motor A. When one of them is HIGH and the other is LOW, the Motor A will spin. If both the inputs are either HIGH or LOW the Motor A will stop.
  • OUT1 & OUT2 pins: are connected to Motor A.

Motor B pins (Channel B):

  • ENB pins: are used to control the speed of Motor B. Removing the jumper and connecting this pin to PWM input will let us control the speed of Motor B.
  • IN3 & IN4 pins: are used to control the spinning direction of Motor B. When one of them is HIGH and the other is LOW, Motor B will spin. If both the inputs are either HIGH or LOW the Motor B will stop.
  • OUT3 & OUT4 pins: are connected to Motor B.

As described above, the L298N driver has two input powers:

  • One for DC motor (VCC and GND pins): from 5 to 35V.
  • One for the L298N module's internal operation (5V and GND pins): from 5 to 7V.

The L298N driver also has three jumpers for advanced uses or other purposes. For the sake of simplicity, please remove all jumpers from the L298N driver.

We can control two DC motors independently at the same time by using an Arduino and an L298N Driver. To control each motor, we need only three pins from Arduino.

※ NOTE THAT:

The rest of this tutorial controls a DC motor using channel A. Controlling the other DC motor is similar.

How To Control the Speed of DC Motor via L298N Driver

It is simple to control the speed of the DC motor by generating a PWM signal to the ENA pin of L298N. We can do this by:

  • Connect an Arduino pin to ENA of L298N
  • Generate PWM signal to ENA pin by using analogWrite() function. L298N Driver amplify PWM signal to DC motor
analogWrite(ENA_PIN, speed); // speed is a value from 0 to 255

The speed is a value between 0 and 255. If the speed is 0, the motor stops. If the speed is 255, the motor spins at maximum speed.

How To Control the Direction of DC Motor via L298N Driver

The spinning direction of a motor can be controlled by applying a logic HIGH/LOW to IN1 and IN2 pins. The below table illustrates how to control the direction in both channels.

IN1 pin IN2 pin Direction
LOW LOW Motor A stops
HIGH HIGH Motor A stops
HIGH LOW Motor A spins Clockwise
LOW HIGH Motor A spins Anti-Clockwise
  • Control motor A spins clockwise
digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW);
  • Control motor A spins anti-clockwise
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, HIGH);

※ NOTE THAT:

The spinning direction is inverted if the OUT1 & OUT2 pin connects to two pins of the DC motor in an inverse way. If so, it just needs to swap between OUT1 & OUT2 pin or change the control signal on IN1 and IN2 pin in the code.

How To Stop DC Motor Spinning

There are two ways to stop DC motor

  • Controls the speed to 0
analogWrite(ENA_PIN, 0);
  • Controls IN1 IN2 pins to the same value (LOW or HIGH)
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW);
  • Or
digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, HIGH);

How to control a DC motor using L298N driver.

Wiring Diagram

Please remove all three jumpers on the L298N module before wiring.

Arduino DC Motor L298N Driver Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code

The below code does:

  • Speed up DC motor
  • Change the direction
  • Speed down DC motor
  • Stop the motor
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-dc-motor */ // constants won't change const int ENA_PIN = 9; // the Arduino pin connected to the EN1 pin L298N const int IN1_PIN = 6; // the Arduino pin connected to the IN1 pin L298N const int IN2_PIN = 5; // the Arduino pin connected to the IN2 pin L298N // the setup function runs once when you press reset or power the board void setup() { // initialize digital pins as outputs. pinMode(ENA_PIN, OUTPUT); pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(IN1_PIN, HIGH); // control motor A spins clockwise digitalWrite(IN2_PIN, LOW); // control motor A spins clockwise for (int speed = 0; speed <= 255; speed++) { analogWrite(ENA_PIN, speed); // control the speed delay(10); } delay(1000); // rotate at maximum speed 1 seconds in in clockwise direction // change direction digitalWrite(IN1_PIN, LOW); // control motor A spins anti-clockwise digitalWrite(IN2_PIN, HIGH); // control motor A spins anti-clockwise delay(1000); // rotate at maximum speed 1 seconds in in anti-clockwise direction for (int speed = 255; speed >= 0; speed--) { analogWrite(ENA_PIN, speed); // control the speed delay(10); } delay(1000); // stop motor 1 second }

Quick Steps

  • Remove all three jumpers on the L298N module.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • You will see:
    • DC motor is speeded up and then rotates at the maximum speed 1 second
    • DC motor's direction is changed
    • DC motor rotates at the maximum speed of 1 second in the reverse direction
    • DC motor is speeded down
    • DC motor stop 1 second
    • The above process is run repeatedly.

    ※ NOTE THAT:

    In this tutorial, we learn how to control the DC motor speed that is relative to the maximum speed. In order to control the absolute speed (round per second), we need to use a PID controller and an encoder. Controlling the absolute speed of the DC motor will be presented in another tutorial.

How to Control two DC Motors using L298N Driver

(comming soon)

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