Hooking it up
As you can tell from the illustration this guy requires a few pins from your arduino to get it running. And it probably looks complicated at first. But it’s not that bad. The first thing to notice however, is that you do need an external power source for your motors (the TB6612FNG can work with 2.5 to 13v), the 5v pin on the arduino just can not source enough power to drive 2 motors, and you could your arduino if you do.
But why it uses so many pins is for several reasons. First, there is a standby pin, if this pin is held LOW, the motors are basically disconnected from power. And… Each motor also has 3 control pins, 2 for direction, and one for speed.
Code
The code for this is very basic. We created a function for you that makes controlling the TB6612FNG from your arduino easier, but you can also change it up and do it your own way.
As I mentioned above, Each motor has 3 control pins, 2 for direction, and one for speed. When one direction pin is HIGH and the other is LOW the motor will spin one direction, flip them and it spins the other direction (both HIGH or both LOW and the motor stops). The PWM pin allows you to analogWrite to this pin to control the speed of that one motor. andlogWrite 0 and the motor stops, 255, and it will go full speed.
//motor A connected between A01 and A02//motor B connected between B01 and B02int STBY =10;//standby//Motor Aint PWMA =3;//Speed control int AIN1 =9;//Directionint AIN2 =8;//Direction//Motor Bint PWMB =5;//Speed controlint BIN1 =11;//Directionint BIN2 =12;//Directionvoid setup(){pinMode(STBY,OUTPUT);pinMode(PWMA,OUTPUT);pinMode(AIN1,OUTPUT);pinMode(AIN2,OUTPUT);pinMode(PWMB,OUTPUT);pinMode(BIN1,OUTPUT);pinMode(BIN2,OUTPUT);}void loop(){move(1, 255, 1);//motor 1, full speed, leftmove(2, 255, 1);//motor 2, full speed, leftdelay(1000);//go for 1 secondstop();//stopdelay(250);//hold for 250ms until move againmove(1, 128, 0);//motor 1, half speed, rightmove(2, 128, 0);//motor 2, half speed, rightdelay(1000);stop();delay(250);}void move(int motor,int speed,int direction){//Move specific motor at speed and direction//motor: 0 for B 1 for A//speed: 0 is off, and 255 is full speed//direction: 0 clockwise, 1 counter-clockwisedigitalWrite(STBY,HIGH);//disable standbyboolean inPin1 =LOW;boolean inPin2 =HIGH;if(direction == 1){inPin1 =HIGH;inPin2 =LOW;}if(motor == 1){digitalWrite(AIN1, inPin1);digitalWrite(AIN2, inPin2);analogWrite(PWMA, speed);}else{digitalWrite(BIN1, inPin1);digitalWrite(BIN2, inPin2);analogWrite(PWMB, speed);}}void stop(){//enable standbydigitalWrite(STBY,LOW);}
[via: http://bildr.org]
