Introduction

In this course we will study DC motors (also called direct current motors). It is one of the most used motors to convert electrical energy into mechanical energy.
DC motors have the particularity of operating in both directions, depending on the direction of the current.

We will see in this tutorial how to reverse the terminals of this one in order to reverse the direction.

A DC motor is reversible, meaning it can be used to transform electrical energy into mechanical energy and vice versa.

The current generator can often be used as a bicycle dynamo.

Running a DC motor

To run the motor without needing to program it, you must connect it to the 5V and GND of the Arduino board.

Tip 1

To make the motor run slower, you can connect it to 3V.

Tip 2

To change the direction of rotation of the motor, simply reverse the 2 cables.

It’s quite simple and practical to connect the motor like this, but the problem is that you always have to disconnect and reconnect the cables if you want to change direction. This is not very practical if you want to make the motor move in a particular way or control it with sensors or even a remote control. Fortunately there is a solution: the use of the shield motor card.

The MotorShield card

The Arduino Motor Shield allows you to easily control the direction and speed of a motor using the Arduino.

The Motorshield card that we use and we offer in the link just below has a 2-channel module. They allow you to control 2 DC motors or a stepper motor. However, this is not the case for all Motorshield cards, some of which can only control two DC motors.

The motorshield card for arduino is attached directly to the arduino uno card using its pins. This is very handy if you have an arduino Uno board, as the pins match perfectly. However, if you use an Arduino nano card for example, you will have more difficulty connecting it. We then advise you to use the L298N h-shaped bridge.

All this is managed by the motorshield card and the linked library. You can also vary the speed of the motor with the pwm pins associated with the board.

Pulse width modulation (pwm)

The disadvantage of the Arduino card is that it can only control the motor for two states: LOW which corresponds to 0V and HIGH which corresponds to 5V. So we cannot have an intermediate voltage to vary the speed of our motor. The motor shield card has pins called PWM allowing us to vary the speed of our motor.

The PWM, called Pulse Width Modulation, will send current pulses to the motor, with signals having a HIGH (5V) or LOW (0V) state. The motor is therefore alternately on and off but the cycle is so fast that it appears as if the motor is on permanently.

Connection and purchase


The motorshield card also has an external power supply to support the power supply of the motors. You can plug up to 12V into it. It can deliver up to 2 amps per motor.

For motors requiring 9 volts, you can add an external 8 volt power supply to the motorshield card.

The shield motor card is used to extend the possibilities of the Arduino card. You can get it through this link.

Turning a DC Motor On and Off

const int speedA = 3;
const int speedB = 11;

void setup() {
    pinMode(speedA,OUTPUT);
    pinMode(speedB,OUTPUT);
}
void loop() {
    // Motor A
    digitalWrite(speedA,HIGH); // Turn on the engine
    delay(5000);
    digitalWrite(speedA,LOW); // We turn off the engine
    
    //Motor B
    digitalWrite(speedB,HIGH); // Turn on the engine
    delay(5000);
    digitalWrite(speedB,LOW); // We turn off the engine
}

Changing the direction of a DC motor

const int speedA = 3;
const int speedB = 11;
const int directionA = 12;
const int directionB = 13;

void setup() {
    pinMode(speedA,OUTPUT);
    pinMode(speedB,OUTPUT);
    pinMode(directionA,OUTPUT);
    pinMode(directionB,OUTPUT);
}

void loop() {
    // Motor A
    digitalWrite(speedA,HIGH); // Turn on the engine
    digitalWrite(directionA,HIGH); // Turns the motor in one direction
    delay(5000);
    digitalWrite(directionA,LOW); // Turns the motor in one direction
    delay(5000);
    digitalWrite(speedA,LOW); // Turn off the engine
    
    //Motor B
    digitalWrite(speedB,HIGH); // Turn on the engine
    digitalWrite(directionB,HIGH); // Turns the motor in one direction
    delay(5000);
    digitalWrite(directionB,LOW); // Turns the motor in one direction
    delay(5000);
    digitalWrite(speedB,LOW); // Turn off the engine
}

Changing the speed of a DC motor

const int input_voltage = 9;
const int nominal_voltage = 5;
const int MAX_SPEED = int(nominal_voltage * 255 / input_voltage);// We define the maximum speed of the motor by the speed at its nominal voltage.
const int speedA = 3;
const int speedB = 11;

void setup() {
    pinMode(speedA, OUTPUT);
    pinMode(speedB, OUTPUT);
}
void loop() {
    for (int i = 0; i < MAX_SPEED; i++) { //speed increase up to maximum speed
        analogWrite(speedA, i); //Speed ​​value for motor A
        delay(100);
    }
    for (int i =MAX_SPEED; i > 0; i--) { // speed decrease to maximum speed
        analogWrite(speedA, i);
        delay(100);
    }
    // Same for engine B
    for (int i = 0; i < MAX_SPEED; i++) {
        analogWrite(speedB, i);
        delay(100);
    }
    for (int i =MAX_SPEED; i > 0; i--) {
        analogWrite(speedB, i);
        delay(100);
    }
}