Arduino 9: The H-Bridge

An H-Bridge is like an electric double-switch, or double-relay. A small current from the Arduino at one of two pins can turn on a much larger current at two other pins. This is useful for controlling two-state devices, like motors that you want to run both forwards and backwards. For this project I used an SN75441 H-bridge chip.

Example:

Have the Arduino read the state of a switch to control a high-voltage motor running forwards and backwards. Use a 9V battery to power the Arduino (remember to change the jumper pin), so Vin is 9V. Connect the two motor wires directly to the H-bridge chip pins labeled “Motor”, and connect both pins 4 and 5 of the H-bridge to ground.

Schematic:

D2, D3 and D4 are digital pins on the Arduino

Program:

int motorPin0 = 2;

int motorPin1 = 3;

int switchPin = 4;

int state = 0;

void setup() {

pinMode(motorPin0, OUPUT);

pinMode(motorPin1, OUTPUT);

pinMode(switchPin, INPUT);

}

void loop() {

state = digitalRead(switchPin);

switch(state) {

case 0:

digitalWrite(motorPin0, HIGH);

digitalWrite(motorPin1, LOW);

break;

case 1:

digitalWrite(motorPin0, LOW);

digitalWrite(motorPin1, HIGH);

break;

}

}

Note the use of the switch statement:

switch(variable){

case value1:

(code block here);

break;

case value2:

(code block here);

break;

case value3:

(code block here);

break;

default:

(code block here);

break;

}

Where variable can take on the values value1, value2, value3, etc. (which are usually integer values), and based on the value of variable, different code is run. The default option is run if the value of variable doesn’t match any of the other cases. The keyword break tells the program to jump to the end of the switch statement, skipping the code in between.

About these ads

One response to “Arduino 9: The H-Bridge

  1. Pingback: Problem | cchaykinseniorproject

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s