l298

The Funduino Tracking Maze also comes with a L298 Dual H-Bridge Motor driver to control the two wheels of the robot.  It’s designed to drive inductive loads, such as relays, solenoids, DC and stepping motors. It can also let us drive two independent DC motors, controlling the speed and direction of each one .

The connections on the board are fairly simple to understand:

motorsconnections

Here we have several connectors for the motors (from left to right):

Motor A Power Motor B
VCC + GND VMS (vcc) + GND + 5v (external power) Vcc + GND
Note: To connect to arduino, we use VMS + GND. NOT 5v

channels

The PINS connections,  we have 8 (From left to right):

  • 5v
  • GND
  • ENB ( Motor B enable PIN)
  • IN4 (Motor B direction 2 PIN)
  • IN3 (Motor B direction 1 PIN)
  • IN2 (Motor A direction 2 PIN)
  • IN1 (Motor A direction 1 PIN)
  • ENA (Motor A enable PIN)

So, here’s a small resume of directions and how to control them:

Enable Motor HIGH – Enable LOW – Disable Motor
Direction 1 IN1 – HIGH IN2 – LOW
Direction 2 IN1 – LOW IN2 – HIGH
Coasting IN1 – LOW IN2 – LOW
Break IN1 – HIGH IN2 – HIGH

Bellow is a small code to test both DC motors. Here is the connections:

Arduino PIN Dual H-Bridge L298
10 ENA
9 ENB
2 IN1
3 IN2
4 IN3
5 IN4

connections1

/**
 * Bruno Santos, 2013
 * feiticeir0@whatgeek.com.pt
 * Small code to test DC motors - 2x with a L298 Dual H-Bridge Motor Driver
 * Free to share
 **/
 //Testing the DC Motors
//Define Pins
 //Motor A
 int enableA = 10;
 int pinA1 = 2;
 int pinA2 = 3;
//Motor B
 int enableB = 9;
 int pinB1 = 4;
 int pinB2 = 5;
void setup() {
Serial.begin (9600);
 //configure pin modes
 pinMode (enableA, OUTPUT);
 pinMode (pinA1, OUTPUT);
 pinMode (pinA2, OUTPUT);
pinMode (enableB, OUTPUT);
 pinMode (pinB1, OUTPUT);
 pinMode (pinB2, OUTPUT);
}
void loop() {
 //enabling motor A
 Serial.println ("Enabling Motor A");
 digitalWrite (enableA, HIGH);
//do something
 //forward
 Serial.println ("Forward");
 digitalWrite (pinA1, HIGH);
 digitalWrite (pinA2, LOW);
//5s forward
 delay (5000);
//reverse
 digitalWrite (pinA1,LOW);
 digitalWrite (pinA2,HIGH);
//5s backwards
 delay (5000);
//stop
 digitalWrite (enableA, LOW);
 delay (5000);
//enabling motor B
 //Since motor B is mounted reversed, PINs must be exchanged
 Serial.println ("Enabling Motor A");
 digitalWrite (enableB, HIGH);
//do something
 //forward
 Serial.println ("Forward");
 digitalWrite (pinB1, LOW);
 digitalWrite (pinB2, HIGH);
//5s forward
 delay (5000);
//reverse
 digitalWrite (pinB1,HIGH);
 digitalWrite (pinB2,LOW);
//5s backwards
 delay (5000);
//stop
 digitalWrite (enableB, LOW);
 delay (5000);
 }

And now, a little program with some functions defined for directions:

/**
* Bruno Santos, 2013
* feiticeir0@whatgeek.com.pt
* Small code to test DC motors - 2x with a L298 Dual H-Bridge Motor Driver
* Free to share
**/

//Testing the DC Motors
//Define Pins
//Motor A
int enableA = 10;
int pinA1 = 2;
int pinA2 = 3;

//Motor B
int enableB = 9;
int pinB1 = 4;
int pinB2 = 5;
//define time for run
// in milliseconds
int running = 10000; //10 secons
boolean play;
void setup() {
 Serial.begin (9600);
 //configure pin modes
 pinMode (enableA, OUTPUT);
 pinMode (pinA1, OUTPUT);
 pinMode (pinA2, OUTPUT); 

 pinMode (enableB, OUTPUT);
 pinMode (pinB1, OUTPUT);
 pinMode (pinB2, OUTPUT); 

 play = true;

}
//Defining functions so it's more easy
//motor functions
void motorAforward() {
 digitalWrite (pinA1, HIGH);
 digitalWrite (pinA2, LOW);
}
void motorBforward() {
 digitalWrite (pinB1, LOW);
 digitalWrite (pinB2, HIGH);
}
void motorAbackward() {
 digitalWrite (pinA1, LOW);
 digitalWrite (pinA2, HIGH);
}
void motorBbackward() {
 digitalWrite (pinB1, HIGH);
 digitalWrite (pinB2, LOW);
}
void motorAstop() {
 digitalWrite (pinA1, HIGH);
 digitalWrite (pinA2, HIGH);
}
void motorBstop() {
 digitalWrite (pinB1, HIGH);
 digitalWrite (pinB2, HIGH);
}
void motorAcoast() {
 digitalWrite (pinA1, LOW);
 digitalWrite (pinA2, LOW);
}
void motorBcoast() {
 digitalWrite (pinB1, LOW);
 digitalWrite (pinB2, LOW);
}
void motorAon() {
 digitalWrite (enableA, HIGH);
}
void motorBon() {
 digitalWrite (enableB, HIGH);
}
void motorAoff() {
 digitalWrite (enableA, LOW);
}
void motorBoff() {
 digitalWrite (enableB, LOW);
}
// Movement functions
void forward (int duration) {
 motorAforward();
 motorBforward();
 delay (duration);
}
void backward (int duration) {
 motorAbackward();
 motorBbackward();
 delay (duration);
}
void left (int duration) {
 motorAbackward();
 motorBforward();
 delay (duration);
}
void right (int duration) {
 motorAforward();
 motorBbackward();
 delay (duration);
}
void coast (int duration) {
 motorAcoast();
 motorBcoast();
 delay (duration);
}
void breakRobot (int duration) {
 motorAstop();
 motorBstop();
 delay (duration);
}
void disableMotors() {
 motorAoff();
 motorBoff();
}
void enableMotors() {
 motorAon();
 motorBon();
}

void loop() {
 enableMotors();

 while (play) {
 //Forward for 1,5s
 Serial.println ("forward...");
 forward (1500);
//turn right
 Serial.println ("right...");
 right (500);
//Forward for 1,5s
 Serial.println ("forward...");
 forward (1500);
//turn left
 Serial.println ("left...");
 left (1000);

 //Forward for 1,5s
 Serial.println ("forward...");
 forward (1500);
if (millis() > running) {
 play = false;
 Serial.println ("Falsing");
 }
 };

 //disable motors
 disableMotors();
}