Problems using TB6612FNG with arduino nano

Hey there,

I have a problem using the TB6612FNG motor controller with arduino nano.
By using the instructions provided by sparkfun (TB6612FNG Hookup Guide - SparkFun Learn), I am able to control the motors, but not their speed.

I am using the serial monitor to send commands to activate a certain motor, with a certain speed. Unfortunately the motor only starts turning if I give 255, or -255 as an input.

Here is my code (you can ignore servo 1-3, they are working just fine):

  
#include <Servo.h>
#include <SparkFun_TB6612.h>

const unsigned int MAX_MESSAGE_LENGTH = 12;

//initialize the servos
Servo servo1;
Servo servo2;
Servo servo3;


//initialize all the servo info
String readString; //main captured String 
int pos = 0;
String servo_id;
String servo_angle;

//locations of the , in  the message
int ind1;
int ind2;
int ind3;

//initialize paremeters for stirrer and pump control
// these constants are used to allow you to make your motor configuration 
// line up with function names like forward.  Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;


// Pins for all inputs, keep in mind the PWM defines must be on PWM pins
#define AIN1 2
#define BIN1 7
#define AIN2 4
#define BIN2 8
#define PWMA 9
#define PWMB 10
#define STBY 11

// Initializing motors.  The library will allow you to initialize as many
// motors as you have memory for.  If you are using functions like forward
// that take 2 motors as arguements you can either write new functions or
// call the function more than once.
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);


void setup() {
  Serial.begin(9600);
  servo1.attach(3);
  servo2.attach(5);
  servo3.attach(6);

}
  
void loop() {

 //Check to see if anything is available in the serial receive buffer
 while (Serial.available() > 0)
 {
   //Create a place to hold the incoming message
   static char message[MAX_MESSAGE_LENGTH];
   static unsigned int message_pos = 0;

   //Read the next available byte in the serial receive buffer
   char inByte = Serial.read();

   //Message coming in (check not terminating character) and guard for over message size
   if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) )
   {
     //Add the incoming byte to our message
     message[message_pos] = inByte;
     message_pos++;
   }
   //Full message received...
   else
{
 //Add null character to string
 message[message_pos] = '\0';

 //Print the message (or do other things)
 Serial.println(message);

 //Or convert to integer and print
 int number = atoi(message);



 int servo_id = atoi(strtok(message, ","));
 int servo_angle = atof(strtok(NULL, ","));
 int motor_speed = atof(strtok(NULL, ","));

 //Serial.println(number);
 Serial.print("servo_id = ");
 Serial.println(servo_id);
 Serial.print("servo_angle = ");
 Serial.println(servo_angle);
 Serial.print("motor_speed = ");
 Serial.println(motor_speed);
 
 
 //Reset for the next message
 message_pos = 0;

 // execute motor functions
 if (servo_id==1){
  servo1.write(servo_angle);
  Serial.println("Changing servo1 position");
  delay(1000);
 }
else if (servo_id==2) {
  servo2.write(servo_angle);
  Serial.println("Changing servo2 position");
  delay(1000);
}
else if (servo_id==3) {
  servo3.write(servo_angle);
  Serial.println("Changing servo3 position");
  delay(1000);
}
else if (servo_id==4) {
     //Use of the drive function which takes as arguements the speed
   //and optional duration.  A negative speed will cause it to go
   //backwards.  Speed can be from -255 to 255(servo_angle).  Also use of the 
   //brake function which takes no arguements.
   if (servo_angle==0) {
    motor1.brake();
   }
   else {
    motor1.drive(servo_angle,1000);
   }
   
   
}
else if (servo_id==5) {
     //Use of the drive function which takes as arguements the speed
   //and optional duration.  A negative speed will cause it to go
   //backwards.  Speed can be from -255 to 255(servo_angle).  Also use of the 
   //brake function which takes no arguements.
   if (servo_angle==0) {
    motor2.brake();
   }
   else {
    motor2.drive(servo_angle);
    
   }
   
   
}


 else {
  Serial.println("No motor logic here");
}
}

 }
}

//int move_servo(){
//  servo1.write(angle)
//  }

and here is the wiring diagram i drew:

I am banging my head against a wall for two days now and I am unable to solve this, so help is really appreaciated!

Dig into the Sparcfun library and discover what functions it provides.

I found the solution here:

The problem was the servo library, which is based on Timer1. PWM pins 9 and 10 also controled by Timer1.

By swithing pin3/9 and 5/10, it works now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.