Site icon AranaCorp

Control a DC motor with Arduino

One of the main objectives in robotics is to make things move on their own. To give motion to a robot, electrical motors are oftenly used such as direct current (DC) motors especially for mobile robots. In this tutorial, we will see how to control such a motor using Arduino and an H-bridge.

prerequisit: Programming with Arduino

Material

DC Motor

Direct Current motors are oftenly used with a gearbox to increase the torque while keeping small dimensions. DC Motor is quite simple to use. To make it run, the only thing to do is to apply voltage to it. Sign and level of the voltage will determine the speed and direction of rotation.

To modulate the votage applied to the motor, an electrical relay or H-bridge can be used.

H-bridge

An H-bridge distributes power to electrical motor and modulate the voltage accroding to signals sent to its inputs. It can be drawn as four switches that will redirect current depending on the command from the microcontroller.

The device we use for this project is a SN754410NE. It can drive two dc motors and has several pins that have their own purpose.

Little reminder on the pinout:

When selecting a component, always be careful regarding nominal voltage ane current limitations. 

N.B.: There are cards available to facilitate the connection such as the L298N module.

Scheme

Arduino can be powered by the computer.

Programming

Base code

To drive the motor, Hbridge should be enabled and a PWM signal should be sent to Forward or Reverse depending on the rotation direction.

// Constants
const int enableBridge1 = 2;
const int MotorForward1 = 3;
const int MotorReverse1 = 5;

// Variables
int Power = 80; //Motor velocity between 0 and 255

void setup(){
  pinMode(MotorForward1,OUTPUT);
  pinMode(MotorReverse1,OUTPUT);
  pinMode(enableBridge1,OUTPUT);
}

void loop(){
  digitalWrite(enableBridge1,HIGH); // Active pont en H

  // Tourne dans le sens direct pendant 2 secondes
  
  analogWrite(MotorReverse1,0);
  analogWrite(MotorForward1,Power);
  delay(2000);
  
  // Tourne dans le sens indirect pendant 3 secondes
  analogWrite(MotorForward1,0);
  analogWrite(MotorReverse1,Power);
  delay(3000);
  
  //Arrête le moteur pendant 1 seconde
  analogWrite(MotorForward1,0);
  analogWrite(MotorReverse1,0);
  digitalWrite(enableBridge1,LOW); 
  delay(1000);
}

Functions

When a system is composed of several devices, it is generally a good call to create subfunctions to clarify the main code.

// Constants
const int enableBridge = 2;
const int MotorForward = 3;
const int MotorReverse = 5;

// Variables
int Power = 80; //Vitesse du moteur entre 0 et 255

void setup(){
  dcBegin();
}

void loop(){
  dcForward(Power);
  delay(2000);
  
  dcReverse(Power);
  delay(3000);
  
  dcStop();
  delay(1000);
}

void dcBegin(){
  // Initialise les pines utilisées pour le moteur
  pinMode(MotorForward,OUTPUT);
  pinMode(MotorReverse,OUTPUT);
  pinMode(enableBridge,OUTPUT);
  }

void dcForward(int P){
  digitalWrite(enableBridge,HIGH); // Active pont en H
  // Tourne dans le sens Forward à la vitesse spécifiée par P
  analogWrite(MotorReverse,0);
  analogWrite(MotorForward,P);
}

void dcReverse(int P){
  digitalWrite(enableBridge,HIGH); // Active pont en H
  // Tourne dans le sens Reverse à la vitesse spécifiée par P
  analogWrite(MotorForward,0);
  analogWrite(MotorReverse,P);
}

void dcStop(){
  // Arrête le moteur et désactive le pont en H
  analogWrite(MotorForward,0);
  analogWrite(MotorReverse,0);
  digitalWrite(enableBridge,LOW); 
}

You can now drive any DC motors with this simple code.

Sources

Find other examples and tutorials in our Automatic code generator
Code Architect

Exit mobile version