Tags: , , , ,

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

  • Computer
  • Arduino UNO
  • USB cable to connect Arduino to computer
  • SN754410
  • Breadboard
  • DC motor
  • jumper cables

 

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:

  • GND are connected between them and need to be connected to the ground of the project.
  • +5V recieve the power for the logic circuit, usually provided by the output 5V from Arduino
  • M1 Enable and M2 Enable are used to activate the bridges. They recieved a HIGH/LOW voltage from the Arduino.
  • M1/M2 Forward, M1/M2 Reverse recieve the PWM signals and correspond to the motor directions.
  • +Motor Power IN recieve the motor power supply (5,6, 7V and sometimes 24V or more)
  • Motor 1 and Motor 2 are connected to the motor’s wires.

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

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); // Enable Hbridge 1
// Rotate forward for 2 seconds
analogWrite(MotorForward1,Power);
analogWrite(MotorReverse1,0);
delay(2000);
// Rotate abckward for 3 seconds
analogWrite(MotorForward1,0);
analogWrite(MotorReverse1,Power);
delay(3000);
//Stop motor for 1 second
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; //Motor speed between 0 and 255
void setup(){
dcBegin();
}
void loop(){
dcForward(Power);
delay(2000);
dcReverse(Power);
delay(3000);
dcStop();
delay(1000);
}
void dcBegin(){
// Initialize pins used for motor
pinMode(MotorForward,OUTPUT);
pinMode(MotorReverse,OUTPUT);
pinMode(enableBridge,OUTPUT);
}
void dcForward(int P){
digitalWrite(enableBridge,HIGH); // Activate Hbridge
// Run forward at a speed specified by P
analogWrite(MotorForward,P);
analogWrite(MotorReverse,0);
}
void dcReverse(int P){
digitalWrite(enableBridge,HIGH); // Activate Hbridge
// Run backward at a speed specified by P
analogWrite(MotorForward,0);
analogWrite(MotorReverse,P);
}
void dcStop(){
// Stop motor and deactivate Hbridge
analogWrite(MotorForward,0);
analogWrite(MotorReverse,0);
digitalWrite(enableBridge,LOW);
}

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

 

Sources

THE référence https://www.arduino.cc/

Programming with Arduino