An H bridge is an electronic circuit that enables a voltage to be applied across a load in either direction.  Arduino entusiast usullay are working with DC-motors in their projects. Control this motors is not a big problem because is a digitalWrite output process, driving diferent pins connected to transistor or IC motor drivers. Controlling outputs is the first thing that any Arduino hobbyst learns to do.

H-bridge

But I thought that should be an, even, easier way to control DC-Motors, mainly when the problem is manage diferent motors, driving a caterpillar robot for instance. In that issue I started to work with my own libraries. So, I decided to start a MotorH library to implemement a class of objects defined has motors and controlled directly with it’s own methods.

For each motor object motor that has been created, you can perform four methods on them:

  •   void forward(): Turns clockwise.
  •   void backward(): Turns counter clockwise.
  •   void lock(): Motor is short-circuited and acting as brake.
  •   void free(): The motors are disconnected.

In the following exemple I try to control 2 DC-Motors (ML and MR) using a H-bridge for each one of them. This can be a caterpillar robot.

The direction can be decided by a digital joystick.

#include <MotorH.h>

/*
     --------------------------------------- ( + )
     |            |       |            |
    \ R1L        \ R2L   \ R1R        \ R2R
     |            |       |            |
     ----o(ML)o----       ----o(MR)o----   
     |            |       |            |
    \ R3L        \ R4L   \ R3R        \ R4R
     |            |       |            |    
     ---------------------------------------- ( - )

*/

#define REL1L 2
#define REL2L 3
#define REL3L 4
#define REL4L 5

#define REL1R 6
#define REL2R 7
#define REL3R 8
#define REL4R 9

#define UP 10
#define DOWN 11
#define RIGHT 12
#define LEFT 13

MotorH MotorR(REL1R,REL2R,REL3R,REL4R);
MotorH MotorL(REL1L,REL2L,REL3L,REL4L);

int joystick;

void setup(){
  pinMode(UP,INPUT);
  pinMode(DOWN,INPUT);
  pinMode(RIGHT,INPUT);
  pinMode(LEFT,INPUT);
 }

void loop(){
  joystick=!digitalRead(UP)+2*(!digitalRead(RIGHT))+4*(!digitalRead(DOWN))+8*(!digitalRead(LEFT));
   switch (joystick){
    case 1:
      fwd();
      break;
    case 2:
      roundRgt();
      break;
    case 3:
      rgt();
      break;
    case 4:
      bkwd();
      break;
    case 6:
      bkwd();
      break;
    case 8:
      roundLft();
      break;
    case 9:
      lft();
      break;
     case 12:
      bkwd();
      break;
    default:
      lock();
  }
  delay(100);
}

void lock(){
MotorR.lock();
MotorL.lock();
}

void fwd(){
MotorR.forward();
MotorL.forward();
}

void bkwd(){
MotorR.backward();
MotorL.backward();
}

void rgt(){
MotorR.free();
MotorL.forward();
}

void lft(){
MotorL.free();
MotorR.forward();
}

void roundRgt(){
MotorR.backward();
MotorL.forward();
}

void roundLft(){
MotorL.backward();
MotorR.forward();
}

Try to download the library from here.

Anuncios