Simple H Bridge with 4 transistors

H bridge to control DC motor with Arduino

Simple H Bridge with 4 transistors

In this project I use 4 transistors (2x NPN and 2x PNP) to build a simple H bridge for a small DC motor.

Materials needed:
- Arduino UNO (or similar) + USB cable for it
- DC adapter for Arduino
- BlueSMiRF module
- breadboard
- wires
- 2x BC327 PNP transistors or similar
- 2x BC337 NPN transistors or similar
- 4x 100R resistors
- small DC motor
- Android phone with RoboRemo app installed

Introduction

If you connect a DC motor to a battery, the motor will turn in one direction. To reverse the direction, you have to reverse the polarity (reverse + and - wires). But you have to do this manually. What if you want to make a robot for example, and you want to reverse the motor direction from an Arduino program? Here is where the H bridge comes to help. The 4 transistors act like 4 switches, so by turning ON one diagonal or another, you can make the motor turn forward or backward.

The circuit is called H bridge, because the transistors and the motor form the letter H.

4-transistor-h-bridge.jpg


The circuit works as follows:
If A and B are both LOW, all 4 transistors are OFF, so the motor is not connected to anything, so it is OFF.
If only A is HIGH, it will make Q1 turn ON, which will make Q2 turn ON, and the motor will start to turn in one direction.
If only B is HIGH, it will make Q3 turn ON, which will make Q4 turn ON, and the motor will start to turn in the other direction.
If instead of HIGH you send PWM signal (analogWrite), then you can also control the speed of the motor.
Never make both A and B HIGH !!! If you do so, the transistors will make short circuit between Vcc and GND, and they may start to smoke, and then you need to replace them.
I use BC337 and BC327 for small motor. If you use bigger motor, you may need to use more powerful transistors.

Steps:

1.
Build the H bridge circuit according to the schematic in the introduction.

2.
Upload the code to your Arduino:

// Control DC motor speed (both directions) over Bluetooth using RoboRemo app
// www.roboremo.com

// Hardware setup:
// BT module   Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O ------ pin0
// RX-I ------ pin1

// H bridge -- Arduino
// Vcc ------- Vin
// GND ------- GND
// A --------- pin9
// B --------- pin10


#define bluetooth Serial

int motorForward  =  9;  // pin  9 (PWM) to contorl motor forward  speed
int motorBackward = 10;  // pin 10 (PWM) to contorl motor backward speed

char cmd[100];
int cmdIndex;



boolean cmdStartsWith(char *st) {
  for(int i=0; ; i++) {
    if(st[i]==0) return true;
    if(cmd[i]==0) return false;
    if(cmd[i]!=st[i]) return false;;
  }
  return false;
}


void exeCmd() {
  
  if( cmdStartsWith("speed ") ) { // example: if cmd is "speed 200"
    int val = atoi(cmd+6);  // val will be 200 // range is -255 to 255
                            // cmd+6, because value comes after "speed " which is 6 characters
    if(val>=0) {
      analogWrite(motorBackward, 0);
      analogWrite(motorForward, val);
    } else {
      analogWrite(motorForward, 0);
      analogWrite(motorBackward, -val);
    }     
  }

  
}



void setup() {
  
  delay(500); // wait for bluetooth module to start

  bluetooth.begin(115200); // Bluetooth default baud is 115200
  
  pinMode(motorForward, OUTPUT);
  analogWrite(motorForward, 0);
  
  pinMode(motorBackward, OUTPUT);
  analogWrite(motorBackward, 0);
  
  cmdIndex = 0;
}


void loop() {
  
  if(bluetooth.available()) {
    
    char c = (char)bluetooth.read();
    
    if(c=='\n') {
      cmd[cmdIndex] = 0;
      exeCmd();  // execute the command
      cmdIndex = 0; // reset the cmdIndex
    } else {      
      cmd[cmdIndex] = c;
      if(cmdIndex<99) cmdIndex++;
    }
   
    
  }
  
}

3.
Connect the Bluetooth module and H bridge to Arduino according to the hardware setup in the Arduino sketch.

4.
Connect your Arduino to the DC power supply.

5.
Download the RoboRemo interface file:

h_bridge_control.interface

Copy the interface file to your Android device, then import in RoboRemo using menu -> interface -> import.

6.
Enjoy :)