Control DC Motor with H-Bridge Circuit

Control DC Motor with H-Bridge Circuit

Objective

The aim of this tutorial is to help you control the direction and speed of a DC motor using an Arduino board and an H-Bridge motor driver. By the end of this project, you will have learned how to use Pulse Width Modulation (PWM) signals for motor speed control and understand the workings of an H-Bridge circuit in robotics and automation projects.

What You'll Need

Understanding the H-Bridge

The H-Bridge motor driver enables you to control the direction of a DC motor by toggling its input pins. By sending PWM signals, it allows you to adjust the motor's speed, making it an essential component for building projects like robots and motorized devices.

Wiring the Circuit

Follow these steps to wire the components correctly:

Arduino Code

Below is the Arduino code to control the motor's speed and direction:


// H-Bridge DC Motor Control

// Pin assignments
int motorPin1 = 8; // IN1
int motorPin2 = 9; // IN2
int enablePin = 10; // ENA (PWM pin)

// Setup function
void setup() {
  pinMode(motorPin1, OUTPUT); // Set motor control pins as outputs
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
}

// Loop function
void loop() {
  // Spin motor forward
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  analogWrite(enablePin, 191); // 75% speed
  delay(2000); // Run forward for 2 seconds

  // Spin motor in reverse
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  analogWrite(enablePin, 127); // 50% speed
  delay(2000); // Run reverse for 2 seconds

  // Stop the motor
  analogWrite(enablePin, 0); // 0% speed
  delay(1000); // Pause for 1 second
}
            

Upload and Test

  1. Connect your Arduino to your computer using a USB cable.
  2. Open the Arduino IDE and paste the code provided above.
  3. Verify the code and upload it to your Arduino board.
  4. Ensure that the H-Bridge is powered by the 12V supply.
  5. Observe the motor running forward, then reversing, and eventually stopping.

Additional Tips

Troubleshooting

Common Issues and Solutions:

Expanding the Project

Adding Multiple Motors

If you want to control multiple motors, you can add more H-Bridge motor drivers and adjust the Arduino code accordingly. Here's an example for controlling two motors:


// Pin assignments for second motor
int motorPin3 = 4; // IN1 for motor 2
int motorPin4 = 5; // IN2 for motor 2
int enablePin2 = 6; // ENA (PWM pin) for motor 2

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(motorPin3, OUTPUT); // Set second motor pins
  pinMode(motorPin4, OUTPUT);
  pinMode(enablePin2, OUTPUT);
}

void loop() {
  // Motor 1 forward
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  analogWrite(enablePin, 191); 
  delay(2000);

  // Motor 2 forward
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  analogWrite(enablePin2, 191); 
  delay(2000);
  
  // Stop motors
  analogWrite(enablePin, 0);
  analogWrite(enablePin2, 0);
  delay(1000);
}
        

Powering Your Circuit

When working with motors and microcontrollers, it’s important to provide a stable power supply. Here’s a more detailed explanation of the power requirements:

More Advanced Control Techniques

Controlling Motor Direction Based on Input

You can add an external input (e.g., a button or sensor) to control the direction of the motor. Here’s an example:


int buttonPin = 7; // Button input pin
int buttonState = 0;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin); // Read the button state

  if (buttonState == HIGH) {
    // Spin motor forward
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    analogWrite(enablePin, 191); 
  } else {
    // Spin motor in reverse
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    analogWrite(enablePin, 191); 
  }
}
        

Conclusion

You've successfully controlled the speed and direction of a DC motor using an Arduino and an H-Bridge motor driver. This skill is foundational for various robotics, automation, and motorized systems. Keep experimenting to expand your knowledge and capabilities!