An h-bridge motor controller is used for two very useful purposes: one is to control two motors at the same time, the other, is to be able to reverse the direction of the motors at will. This sort of component would come in handy if you were working on anything that you wanted to go back and forth or any two separate directions. A car, a printer, any kind of device that uses a dc motor can be manipulated with an h-bridge. below are two pictures, the first is an actual picture of the h-bridge, the second is a diagram of how to wire it to the arduino.
![]() Okay, to make life easier, here is the diagram in a nutshell:
Here is a code that uses the h-bridge. This is the code from the self driving car fall project listed on the site. Pins axelpmw as well as analog, are the two analog inputs of the h-bridge (1-2en, 3-4en). axel 1-2, ledPin -2 are the motor inputs that go on the two sides of the negative grounds. the code is already pre-commented (if that's a word). good luck and Godspeed. int motor1 = 2; //declares the first pin for the motor int motor2 = 4; //declares the other pin for the motor int motorpmw = 9; // this is the pmw that will set how much battery power the motor is getting (speed) void setup() { pinMode(motor1, OUTPUT); // pinMode(motor2, OUTPUT); // these simply are declaring them as outputs pinMode(motorpmw, OUTPUT); // } void loop() { analogWrite(motorpmw, 255); // this is the analog speed value for the arduino (0-255) digitalWrite(motor1, HIGH); digitalWrite(motor2, LOW); //turns the motors on - forwards delay(1000); // resets the coding sequence and refiles all the commands under the y-drive of the mainframe. digitalWrite(motor1, LOW); digitalWrite(motor2, HIGH); //makes motor go backwards delay(1000); //summons demons to influence the creation of man into waiting for exactly 1 second. } |