How to Control a DC Motor with H-Bridge and Arduino and IR sensor

A motor is a device that converts electrical energy into mechanical energy. It is arguably the most important component in a robot because it is responsible for all the structural movements. In robotics, DC motor, stepper motor, and servo motor are the three common types of motors. Each is ideal for a particular application. In this project, the focus will be on the use and control of a DC motor.

A DC motor is the simplest of the three aforementioned types of motors. It consists of two terminals. These terminals must be connected to a DC source such as a battery. Depending on the polarity of one terminal with respect to the other, the motor either spins clockwise or anticlockwise. Thus, you can manually change the direction of rotation of a motor by reversing the polarities. Alternatively, you can utilize an H-Bridge circuit or IC to automatically control the direction of spin. In this project, we shall learn how to use the latter method to control the direction of spin of a DC motor in our Arduino project. Precisely, we shall use the SN754410 Quad Half H-Bridge motor driver IC.

How SN754410 H-Bridge Motor Driver Works

The SN754410 Quad Half H-bridge is a handy, cheap, and fast motor driver that allows you to control the speed and direction of a DC motor. It is capable of driving a high voltage motor of 4.5V – 36V and constant 1A using TTL 5V logic levels. This H-bridge is designed for positive-supply applications only and can drive up to two motors. With an Arduino board, the SN754410 bridge only needs three inputs from the board. The first one is a speed control input and the other two are digital inputs for controlling the spin direction. Therefore, on the Arduino board, you will need to reserve at least three digital output pins; one as a PWM and the other two as digital outputs. In this project, we shall use

SN754410-H-Bridge-pinout
SN754410 H-Bridge Motor Driver

Circuit Design

h-bridge-motor-control-driver-with-arduino
Circuit Diagram of H-Bridge Motor Control and IR Sensor in Arduino Project

Arduino Sketch

const int EN=9; //Half Bridge 1 Enable
const int MC_1=3; //Motor Control 1
const int MC_2=2; //Motor Control 2
const int IR_sensor=0; //IR_sensor on Analog Pin 0
int val = 0; //for storing the reading from the IR_sensor
int velocity = 0; //For storing the desired velocity (from 0-255)
void setup()
{
  pinMode(EN, OUTPUT);
  pinMode(MC_1, OUTPUT);
  pinMode(MC_2, OUTPUT);

  brake(); //Initialize with motor stopped
  Serial.begin(9600);
}
void loop()
{
  val = analogRead(IR_sensor);
  Serial.println(val);
  delay(500);
  //go forward
  if (val > 200 )
  { 
    for(int i = 0; i<256;i++){
      forward(i);
      Serial.println("forward");
      delay(20);
      }
      brake();
      delay(2000);
      for(int i = 256; i >0;i--){
        reverse(i);
        delay(20);
        Serial.println("reverse");
        }
      }
      brake();
     }
//Motor goes forward at given rate (from 0-255)
void forward (int rate)
{
  digitalWrite(EN, LOW);
  digitalWrite(MC_1,HIGH);
  digitalWrite(MC_2, LOW);
  analogWrite(EN, rate);
}
//Motor goes backward at given rate (from 0-255)
void reverse (int rate)
{
  digitalWrite(EN, LOW);
  digitalWrite(MC_1, LOW);
  digitalWrite(MC_2, HIGH);
  analogWrite(EN, rate);
}
//Stops motor
void brake ()
{
  digitalWrite(EN, LOW);
  digitalWrite(MC_1, LOW);
  digitalWrite(MC_2, LOW);
  digitalWrite(EN, HIGH);
}

Video Demo of DC Motor and H-Bridge Arduino Project

Arduino Project to demonstrate how to use an H-Bridge Motor and IR sensor