Pages: [1]   Go Down
Author Topic: Code to control TB6612FNG motor driver  (Read 5689 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

TB6612FNG motor driver:
http://www.sparkfun.com/commerce/product_info.php?products_id=9457

Some code demonstrating how to use this motor driver:

Code:
//Configure these to fit your design...
#define out_STBY 5
#define out_B_PWM 3
#define out_A_PWM 11
#define out_A_IN2 9
#define out_A_IN1 10
#define out_B_IN1 8
#define out_B_IN2 7
#define motor_A 0
#define motor_B 1
#define motor_AB 2

void setup()
{
  pinMode(out_STBY,OUTPUT);
  pinMode(out_A_PWM,OUTPUT);
  pinMode(out_A_IN1,OUTPUT);
  pinMode(out_A_IN2,OUTPUT);
  pinMode(out_B_PWM,OUTPUT);
  pinMode(out_B_IN1,OUTPUT);
  pinMode(out_B_IN2,OUTPUT);
  
  motor_standby(false);
  motor_speed2(motor_A,-100);
  motor_speed2(motor_B,-100);
  delay(2000);
  for (int i=-100; i<100; i++) {
    motor_speed2(motor_A,i);
    motor_speed2(motor_B,i);
    delay(50);
  }
  delay(2000);
  motor_standby(true);
}

void loop()
{

}

void motor_speed2(boolean motor, char speed) { //speed from -100 to 100
  byte PWMvalue=0;
  PWMvalue = map(abs(speed),0,100,50,255); //anything below 50 is very weak
  if (speed > 0)
    motor_speed(motor,0,PWMvalue);
  else if (speed < 0)
    motor_speed(motor,1,PWMvalue);
  else {
    motor_coast(motor);
  }
}
void motor_speed(boolean motor, boolean direction, byte speed) { //speed from 0 to 255
  if (motor == motor_A) {
    if (direction == 0) {
      digitalWrite(out_A_IN1,HIGH);
      digitalWrite(out_A_IN2,LOW);
    } else {
      digitalWrite(out_A_IN1,LOW);
      digitalWrite(out_A_IN2,HIGH);
    }
    analogWrite(out_A_PWM,speed);
  } else {
    if (direction == 0) {
      digitalWrite(out_B_IN1,HIGH);
      digitalWrite(out_B_IN2,LOW);
    } else {
      digitalWrite(out_B_IN1,LOW);
      digitalWrite(out_B_IN2,HIGH);
    }
    analogWrite(out_B_PWM,speed);
  }
}
void motor_standby(boolean state) { //low power mode
  if (state == true)
    digitalWrite(out_STBY,LOW);
  else
    digitalWrite(out_STBY,HIGH);
}
void motor_brake(boolean motor) {
  if (motor == motor_A) {
    digitalWrite(out_A_IN1,HIGH);
    digitalWrite(out_A_IN2,HIGH);
  } else {
    digitalWrite(out_B_IN1,HIGH);
    digitalWrite(out_B_IN2,HIGH);
  }
}
void motor_coast(boolean motor) {
  if (motor == motor_A) {
    digitalWrite(out_A_IN1,LOW);
    digitalWrite(out_A_IN2,LOW);
    digitalWrite(out_A_PWM,HIGH);
  } else {
    digitalWrite(out_B_IN1,LOW);
    digitalWrite(out_B_IN2,LOW);
    digitalWrite(out_B_PWM,HIGH);
  }
}
You might wanna change this line depending on your motors:
PWMvalue = map(abs(speed),0,100,50,255);
For "Micro Metal Gearmotor 30:1" 50 is the minimum PWM I wanted to give it, stronger engines can have a value closer to zero though.

Wanna remote control it?
Like I do in this video: http://www.facebook.com/v/295202320029
Then get BlueSMiRF or similar and put this in setup():
Code:
Serial.begin(115200);

Then put this in the loop():
Code:
 byte data, length, motor, command; //0-255
  char speed; //-128-128
  data = Serial.available();
  if (data > 0) {
    length = Serial.read();
    while (data < length-1) { //wait until we got everything
      data = Serial.available(); delay(20); //todo: add timeout
    }
    command = Serial.read();
    switch (command) {
      case 0: //speed
        motor = Serial.read();
        speed = Serial.read();
        if (motor == motor_AB) {
          motor_speed2(motor_A,speed);
          motor_speed2(motor_B,speed);
        } else {
          motor_speed2(motor,speed);
        }
        break;
      case 1: //brake
        motor = Serial.read();
        if (motor == motor_AB) {
          motor_brake(motor_A);
          motor_brake(motor_B);
        } else {
          motor_brake(motor);
        }
        break;
      case 2: //coast
        motor = Serial.read();
        if (motor == motor_AB) {
          motor_coast(motor_A);
          motor_coast(motor_B);
        } else {
          motor_coast(motor);
        }
        break;
      case 3: //standby
        motor_standby(Serial.read());
        break;
    }
  }

But how to steer it?
Well, use your brain... I'll give a hint though, this is to set motor 1 to max backward speed:
byte1 = 4 //this is the total size of message
byte2 = 0 //this is the command id
byte3 = 1 //motor id, 0 or 1
byte4 = -100 //backward = -value (0 to 100)
« Last Edit: January 18, 2010, 07:06:50 pm by JoakimCh » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks. This is exactly what i need.  smiley

Much appreciated.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you share how you hooked up the motor driver to the arduino? Thanks
Logged

Upstate NY
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Vault-Tec Engineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code saved my life JoakimCh smiley-grin

I was lost trying to figure out how to wire the motor driver to my arduino until I found the pin definitions in your code.

I was happy to credit you on my project page smiley (Found on my WWW link below my post. Pictures coming soon!)
Logged

The Netherlands (Rotterdam)
Offline Offline
Full Member
***
Karma: 1
Posts: 169
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have written a library for the motor control:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1280519240

I will upload it to the playground if people think it is useful and want to use it.

Cheers.

BTW: JoakimCh, I've seen you video on facebook.
I am actually doing exactly the same, only my car is controlled using a nordic module (from sparkfun) and a GamePad  smiley
« Last Edit: August 27, 2010, 12:28:11 pm by OpenSource » Logged

Pages: [1]   Go Up
Jump to: