Electronics
3 min read

H-Bridge Integration with Arduino for Motor Control

Published on 
May 13, 2024
Written by
No items found.
Need help for your project?
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

This article explores our latest initiative, the H-Bridge Demo Set, which integrates a L293D H-Bridge module with Arduino microcontrollers to steer motor functions. Our objective is to demonstrate the practical application of H-Bridge in automated systems, thereby enhancing precision in motor control and broadening the scope of its use in automation and robotics.

Understanding H-Bridge Modules

Image credit: Axotron

An H-Bridge is an electronic circuit that enables a voltage to be applied across a load in either direction. These circuits are fundamental in controlling the direction of motors. Composed of four switches, typically transistors or relays, the H-Bridge allows for selective activation in pairs—either the left pair or the right pair—enabling the motor to run forwards or backwards. 

Component Selection

  • Arduino Uno: A microcontroller board based on the ATmega328P, widely used in hobbyist and educational projects for its versatility and user-friendly nature.
  • L293D H-Bridge Module: An accessible, easy-to-use H-Bridge module with built-in diodes to protect against voltage spikes.
  • HC-SR04 Ultrasonic Sensor: A dynamic switch in our system to trigger motor direction changes by proximity.
  • DC Motor: A simple motor to demonstrate the system’s response with motor control.

System Design

To provide a simple yet interactive module to showcase H-Bridge usage, we used an ultrasonic sensor with an Arduino to trigger motor direction controls based on real-time feedback from the environment. 

The motor will spin in one direction when the system by default, but as an object/user is in front of the ultrasonic sensor, the motor will spin in the opposite direction. 

Image credit: L293D Schematic by miliohm

Forward Spin: When the signal from the Arduino to Input 1 of the L293D is HIGH, and to Input 2 is LOW, the motor spins in one direction.

Reverse Spin: Conversely, when the signal to Input 1 is LOW, and to Input 2 is HIGH, the motor spins in the opposite direction.

The direction of motor spin will be determined by proximity of the user/object to the ultrasonic sensor.

Key Design Considerations

  • Dynamic Motor Direction: Automatically adjusts based on object proximity, demonstrating the motor’s responsiveness.
  • Immediate Response to Distance Changes: Changes in motor direction are executed immediately upon object detection.
Schematic Design of H-Bridge Demo Set

Coding and Operation

Below, we detail the complete code that drives the interaction within our system, followed by an explanation of key code segments that handle data processing and motor control.

const int controlPin1 = 10;     // H-bridge control pin 1

const int controlPin2 = 11;     // H-bridge control pin 2

const int trigPin = 6;          // Ultrasonic sensor trigger pin

const int echoPin = 7;          // Ultrasonic sensor echo pin

float SPEED_OF_SOUND = 0.0345;

void setup() {

  pinMode(controlPin1, OUTPUT);

  pinMode(controlPin2, OUTPUT);

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

}

void loop() {

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  int microsecs = pulseIn(echoPin, HIGH);

  float cms = microsecs * SPEED_OF_SOUND / 2;

  Serial.println(cms);

  if (cms < 10) {  // Threshold distance 10 cm

    digitalWrite(controlPin1, HIGH);

    digitalWrite(controlPin2, LOW);

  } else {

    digitalWrite(controlPin1, LOW);

    digitalWrite(controlPin2, HIGH);

  }

  delay(10);

}

Key Code Snippets Explained

Ultrasonic Distance Measurement:

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

int microsecs = pulseIn(echoPin, HIGH);

float cms = microsecs * SPEED_OF_SOUND / 2;

This part handles the distance measurement using the HC-SR04 ultrasonic sensor, crucial for determining the proximity of an object.

Motor Direction Control:

if (cms < 10) {  // Threshold distance 10 cm

    digitalWrite(controlPin1, HIGH);

    digitalWrite(controlPin2, LOW);

} else {

    digitalWrite(controlPin1, LOW);

    digitalWrite(controlPin2, HIGH);

}

This section of the code determines the direction in which the motor will spin based on the proximity of an object detected by the ultrasonic sensor.

Future Development

Our team is committed to enhancing the capabilities of the H-Bridge Demo Set. We plan to develop interactions between multiple device sets, aiming to create a complex network of systems that can communicate and respond synchronously. The scalability of this H-Bridge Demo Set will facilitate more elaborate interactive environments and demonstrations, further pushing the boundaries of what is possible with H-Bridge and Arduino integration.

If you are interested in learning more about this project, please do not hesitate to contact our Makers in Residence.