Learn to code

ARDUINO & CODES

Previous Post
Driving a brushed DC Motor with Arduino
Next Post
Controlling a stepper motor with an H-Bridge and Arduino

Direction and speed of a DC Motor with an H-Bridge and Arduino

in Arduino Motor Control

In this Arduino Tutorial, you will control the direction of a DC motor by using an H-Bridge. Servos are the easiest way to start making a motion with a microcontroller. Unfortunately, servos do not turn 360 degrees but only 180.  DC Motors does have the capability to turn 360 degrees around. In the previous tutorial: Arduino Tutorial 6.3: Driving a brushed DC Motor we created code to spin the motor in one direction. If you were to take power and ground on the motor and flip their orientation, the motor would spin in the opposite direction. However, it is not very convenient to do that every time you want to spin the DC Motor in another direction. So, you will be using a component called an H-Bridge to reverse the polarity of the motor.

With the Arduino Uno Board you will be able to control the H-Bridge quite easily. In this tutorial, you will use the l293 H-Bridge chip. To control the direction of the spin of DC motor, without interchanging the leads, you can use a circuit called an H-Bridge. An H-bridge is an electronic circuit that can drive the motor in both directions. H-bridges are used in many different applications. One of the most common application is to control motors in robots.

Connecting an H-bridge chip to a microcontroller such as the Arduino isn’t too complicated. This tutorial will show how to connect the H-Bridge with the Arduino Uno Board so that you can control motors and allow them to move forwards and backward.

H-Bridge L293d

H-Bridges are a type of component known as integrated circuits (IC). ICs are components that hold large circuits in a tiny package. These can help simplify more complex circuits by placing them in an easily replaceable component. For
example, the H-bridge you’re using in this example has a number of transistors built-in. To build the circuit inside the H-bridge you would probably need another breadboard.

H-Bridge L293d

So, to be able to use the H-Bridge L293d correctly, understanding of the component is essentiel. Below are all the pins that you can use on a H-Bridge.

Pin 1 is the enable pin for terminals 1 and 2. This enables the motor to turn on when it is connected to power.

Pin 2 is the motor logic pin for terminal 1. It will provide one voltage to one of the motor terminals.

Pin 3 is the pin where we connect one of the terminals of the motor to.

Pin 4 and Pin 5 both get grounded.

Pin 6 is where we connect the other motor terminal to. This completes the 2-terminal connections necessary for a motor to be hooked up to the H-bridge IC.

Pin 7 is the motor logic pin for terminal 2. This is the second voltage signal feed to the mtor.

Pin 8 is the pin which receives the voltage needed to power on the motor. This is the pin that is connected to the positve voltage supply pof the motor. Remeber to use the appropriate voltage supply. If you have a 9V DC Motor, then you will need to feed 9V into this pin. If the motor is 12V you will need to feed 12V in this pin.

Pin 9 is the enable pin for terminals 3 and 4. It enables the motor to turn on when connected to power and disables the motor when connected to ground.

Pin 10 is the motor logic pin for terminal 3. This is one of the logic voltage signals that determines the action that the second motor will take.

Pin 11 is the pin where we connect one of the terminals of the second motor to.

Pin 12 and Pin 13 both get grounded.

Pin 14 is the pin where we connect the other terminal of the second motor to.

Pin 15 is the motor logic pin for terminal 4. It is the second voltage signal that we feed into the second motor to determine the action the motor will take.

Pin 16 is the pin which receives the voltage needed for power for the IC. It is the IC power pin. The IC of the H-Bridge needs 5Volts to operate. Therefore, we feed 5V into this pin. This can easily be done if you are using an Arduino Uno Board.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 14
H-Bridge × 1
Hobby DC Motor Hobby DC Motor × 1
Battery Snap Battery Snap × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

BreadBoard Layout

BreadBoard Layout –  Attach 5V and ground to one side of your BreadBoard. You will use this to power the IC of the H-Bridge.

Place the H-Bridge in the center of your BreadBoard. Connect pin1 of the H-Bridge to pin9 on the Arduino. This enables the H-Bridge. When the H-Bridge receives 5V, it will turn the motor on, when it receives 0V, the motor will turn off. You will use this pin to control the speed of the motor by using PWM.

Connect pin 2 on the H-Bridge to digital pin3 on the Arduino. Pin7 is connected to digital pin2 on the H-Bridge. These pins are used to communicate in which direction the motor should spin. If pin3 is LOW and pin2 is HIGH the motor will spin in one direction. If pin3 is HIGH and pin2 is LOW the motor will spin in the opposite direction. If both pins are LOW or HIGH the motor stops spinning.

The H-Bridge will get its power from pin16, that pin is connected to the positive rail on your 5V rail on the BreadBoard. Pin4 and Pin5 are connected to the ground of the 5V rail.

The DC motor is connected to pin3 and pin 6 of the H-Bridge. These pins will switch on and of depending on the signal that you send from pin2 and pin3 of the Arduino Uno Board.

To be able to power the motor you will need to plug in the battery. For the first time, do not attach the battery to the holder. Connect pin8 from the H-Bridge to the battery power. Connect ground from the Arduino to the battery’s ground.

Make sure that you do not have the 9V rail and 5V rail of your breadboard connected. Only, the ground should be connected between the two rails.

The Code

/*
 * Brushed_H_Bridge sketch
 * commands from serial port control motor speed and direction
 * digits '0' through '9' are valid where '0' is off, '9' is max speed
 * + or - set the direction
 */
const int enPin = 9; // H-Bridge enable pin
const int in1Pin = 3; // H-Bridge input pins
const int in2Pin = 2;
void setup()
{
 Serial.begin(9600);
 pinMode(in1Pin, OUTPUT);
 pinMode(in2Pin, OUTPUT);
 Serial.println("Speed (0-9) or + - to set direction");
}
void loop()
{
 if ( Serial.available()) {
 char ch = Serial.read();
if(ch >= '0' && ch <= '9') // is ch a number?
 {
 int speed = map(ch, '0', '9', 0, 255);
 analogWrite(enPin, speed);
 Serial.println(speed);
 }
 else if (ch == '+')
 {
 Serial.println("CW");
 digitalWrite(in1Pin,LOW);
 digitalWrite(in2Pin,HIGH);
 }
 else if (ch == '-')
 {
 Serial.println("CCW");
 digitalWrite(in1Pin,HIGH);
 digitalWrite(in2Pin,LOW);
 }
 else
 {
 Serial.print("Unexpected character ");
 Serial.println(ch);
 }
 }
}

Code Explanation

Name the constants

The first part of the sketch / code tells the Arduino Board that pin 2 and pin 3 are connected to the H-Bridge to control the DC motor. Pin9 Is connected to the enable pin.

const int enPin = 9; // H-Bridge enable pin
const int in1Pin = 3; // H-Bridge input pins
const int in2Pin = 2;

 

Delare inputs and output

Firstly, you will need to start Serial Communication to ensure that we can control the Motor with the Serial Monitor. Secondly, you set the pins that control the motor as Output pins. Since you are going to use the Serial Monitor some information needs to be printed to that Monitor.

Serial.begin(9600);
 pinMode(in1Pin, OUTPUT);
 pinMode(in2Pin, OUTPUT);
 Serial.println("Speed (0-9) or + - to set direction");
Read the input of user ( 0 – 9 )

In the void(loop) part of the sketch you need to read the input from the Serial Monitor first. Based on that information the motor will spin at a certain speed and a certain direction. If the user entered a number between 0 – 9 the value is converted with the map function into a digital signal that controls the speed of the motor.

if ( Serial.available()) {
 char ch = Serial.read();
if(ch >= '0' && ch <= '9') // is ch a number?
 {
 int speed = map(ch, '0', '9', 0, 255);
 analogWrite(enPin, speed);
 Serial.println(speed);
 }
Read the input of user ( + / – )

If the user entered a minus or plus symbol into the Serial Monitor the direction of the motor spin will change.

else if (ch == '+')
 {
 Serial.println("CW");
 digitalWrite(in1Pin,LOW);
 digitalWrite(in2Pin,HIGH);
 }
 else if (ch == '-')
 {
 Serial.println("CCW");
 digitalWrite(in1Pin,HIGH);
 digitalWrite(in2Pin,LOW);
 }
User input is wrong

In the case that a user enteres some symbol or number that does not fit the coditions of the previous if statements, and error message is send to the Serial Monitor.

else
 {
 Serial.print("Unexpected character ");
 Serial.println(ch);
 }

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu