Control Motor Direction Using H Bridge in Embedded C
To control motor direction using an
H-Bridge in Embedded C, set the two control pins connected to the H-Bridge inputs to HIGH or LOW in opposite combinations. For example, setting IN1 = HIGH and IN2 = LOW makes the motor rotate forward, while reversing these signals makes it rotate backward.Syntax
Use two digital output pins connected to the H-Bridge inputs to control motor direction. Set pins HIGH or LOW to change rotation.
IN1: Controls one side of the motorIN2: Controls the other side- Setting
IN1 = HIGHandIN2 = LOWrotates motor forward - Setting
IN1 = LOWandIN2 = HIGHrotates motor backward - Setting both LOW or HIGH stops the motor
embedded_c
void setMotorDirection(int IN1, int IN2, int direction) { if(direction == 1) { // Forward digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); } else if(direction == 0) { // Stop digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); } else if(direction == -1) { // Backward digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); } }
Example
This example shows how to control a motor connected to pins 2 and 3 of a microcontroller using an H-Bridge. It rotates the motor forward for 3 seconds, stops for 2 seconds, then rotates backward for 3 seconds.
embedded_c
#include <avr/io.h> #include <util/delay.h> #define IN1_PIN PD2 #define IN2_PIN PD3 void setup() { DDRD |= (1 << IN1_PIN) | (1 << IN2_PIN); // Set pins as output } void setMotorDirection(int direction) { if(direction == 1) { // Forward PORTD |= (1 << IN1_PIN); PORTD &= ~(1 << IN2_PIN); } else if(direction == 0) { // Stop PORTD &= ~((1 << IN1_PIN) | (1 << IN2_PIN)); } else if(direction == -1) { // Backward PORTD &= ~(1 << IN1_PIN); PORTD |= (1 << IN2_PIN); } } int main(void) { setup(); while(1) { setMotorDirection(1); // Forward _delay_ms(3000); setMotorDirection(0); // Stop _delay_ms(2000); setMotorDirection(-1); // Backward _delay_ms(3000); setMotorDirection(0); // Stop _delay_ms(2000); } return 0; }
Output
Motor rotates forward for 3 seconds, stops for 2 seconds, rotates backward for 3 seconds, then stops for 2 seconds, repeating.
Common Pitfalls
- Setting both H-Bridge inputs HIGH can cause a short circuit (called 'shoot-through'), damaging the motor or driver.
- Not configuring microcontroller pins as outputs will prevent motor control.
- Forgetting to stop the motor before changing direction can cause mechanical stress.
- Incorrect wiring of H-Bridge inputs to microcontroller pins leads to unexpected motor behavior.
embedded_c
/* Wrong: Both pins HIGH causes short circuit */ digitalWrite(IN1, HIGH); digitalWrite(IN2, HIGH); /* Right: Stop motor before reversing direction */ digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); delay(100); digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); // Reverse direction
Quick Reference
| IN1 | IN2 | Motor State |
|---|---|---|
| LOW | LOW | Motor stopped |
| HIGH | LOW | Motor rotates forward |
| LOW | HIGH | Motor rotates backward |
| HIGH | HIGH | Invalid - risk of short circuit |
Key Takeaways
Use two digital pins to control motor direction via H-Bridge by setting them HIGH or LOW in opposite states.
Always stop the motor (both pins LOW) before changing direction to avoid damage.
Never set both H-Bridge inputs HIGH simultaneously to prevent short circuits.
Configure microcontroller pins as outputs before controlling the motor.
Test motor control with delays to observe direction changes safely.