Darlington transistor H-Bridge.
Darlington transistor H-Bridge

Safely Build Program a H-Bridge

by Lewis Loflin

Link to YouTube video for this webpage: Arduino Programming H-Bridge

LM317 constant current source.
LM317 constant current source.

The two items above serve the following purposes: limit the current so wiring mistakes or bad programming won't blow your transistors or MOSFETs. The other is simple LED polarity indicator to show the polarity is switching - make sure this works BEFORE connecting a motor!

Make sure any H-bridge is connected to a microcontroller, not push button switches, etc.

Also see H-Bridge Motor Control 2 Input Diagram.

The program below uses direct port register access to assure proper switching. Two switches are connect to and DP2 and DP3. Press either one motor will turn on. Press reset to turn motor off.

To better understand Arduino port register commands see Arduino Port Registers Revisited

  Forward-Reverse H-bridge control
 DP2 - Forward
 DP3 - Reverse
 DP8 - Q1 PNP PB0
 DP9 - Q2 NPN PB1
 DP10 - Q3 PNP PB2
 DP11 - Q4 NPN PB3
 Forward Q1, Q4 ON
 Reverse Q2, Q3 ON

void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  // DP 8,9,10,11,13 output
  DDRB = DDRB | 0b00101111;  // 4 bytes
  // All off
  PORTB = PORTB & 0x00; // 2 bytes

void loop() {

  if (!digitalRead(2))   {
    PORTB = PORTB & 0x00; // off
    PORTB = PORTB | 0b00101001;

  if (!digitalRead(3))   {
    PORTB = PORTB & 0x00; // off
    PORTB = PORTB | 0b00000110;