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.
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.
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.
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.
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);
}
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.
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.