Controlling 2 motors with the TB6612FNG + Arduino

Discussions area for bildr's blog posts.
From: http://bildr.org/2012/04/tb6612fng-arduino/

In previous articles we have discussed how to control motors with simple transistors. And... with PWM you could control the speed. But that is just one motor, and you can only go one direction. Today we are going to talk about the TB6612FNG dual motor driver, specifically the...
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

A bit of an edit-induced typo?: "...and you could your arduino if you do". Probably everybody who has fried an electronic device knows where you were headed with the sentence but it might make it clearer if you added a word or two to describe the carnage to be wrought.
Bodger
 
Posts: 4
Joined: Tue Jan 31, 2012 9:37 am

Thanks!

Bodger wrote:A bit of an edit-induced typo?: "...and you could your arduino if you do". Probably everybody who has fried an electronic device knows where you were headed with the sentence but it might make it clearer if you added a word or two to describe the carnage to be wrought.
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Hi

Was inspired by your article to get my father in-laws watch winder working. I replaced the motors in it and swopped the controller out for an arduino and a TB6612FNG controller.

The box has a 3 way switch in it so I connected this up along with a pull down resistor. I checked with a small serial monitor program to check if the switches are working correctly and all seems to be ok(I still have the monitor in my code).

I want to have three programs for the motors.

- A continuous mode that will just alternate direction of the motors for 30 seconds each direction
- A mode that winds for 2 minutes each direction every 30 minutes
- a mode that winds for 2 minutes each direction every 60 minutes

Problem is that it only seems to run the first selection that I have i.e. continuous. I am pretty sure it is a problem with the code but I am pretty new to coding and the arduino so still finding my way. Any chance anyone could have a quick look at it and let me know where the problem could be?

The code is a mix of a serial reader code that I used to check the switches are working and the code form the article. Switche reads either 10, 01, or 00 depending on the position on pins 6 & 7

Any suggestions on getting this to work and getting around the large "delay" commands I have would be appreciated

This is what the watch winder looks like btw
Imag

JohnB

Code: Select all
/*
  DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor
 
 This example code is in the public domain.
 */

// digital pin 6,7 control the program selection for the watch winder:
const int pushButton1 = 6;
const int pushButton2 = 7;

int switchstate1 = 0;
int switchstate2 = 0;

//motor A connected between A01 and A02
//motor B connected between B01 and B02

int STBY = 10; //standby

//Motor A
int PWMA = 3; //Speed control
int AIN1 = 9; //Direction
int AIN2 = 8; //Direction

//Motor B
int PWMB = 5; //Speed control
int BIN1 = 11; //Direction
int BIN2 = 12; //Direction


// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton1, INPUT);
  pinMode(pushButton2, INPUT);
 
  pinMode(STBY, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int switchstate1 = digitalRead(pushButton1);
  int switchstate2 = digitalRead(pushButton2);
  // print out the state of the button:
  Serial.println(switchstate1);
  Serial.println(switchstate2);
 
  if (switchstate1 = 1)
 
 {move(1, 100, 1); //motor 1, full speed, left
  move(2, 100, 1); //motor 2, full speed, left

  delay(30000); //go for 30 sec
  stop(); //stop
  delay(1000); //hold for 250ms until move again

  move(1, 100, 0); //motor 1, half speed, right
  move(2, 100, 0); //motor 2, half speed, right

  delay(30000);  //go for 30 sec
  stop();
  delay(1000);}
 
  else if(switchstate2 = 1)
 
 {move(1, 100, 1); //motor 1, full speed, left
  move(2, 100, 1); //motor 2, full speed, left

  delay(60000); //go for 2 min
  stop(); //stop
  delay(1000); //hold for 1s until move again

  move(1, 100, 0); //motor 1, half speed, right
  move(2, 100, 0); //motor 2, half speed, right

  delay(6000); //go for 2min
  stop();
  delay(900000);} //wait for 28 minutes
 
  else
 
   {move(1, 100, 1); //motor 1, full speed, left
   move(2, 100, 1); //motor 2, full speed, left

  delay(120000); //go for 1 second
  stop(); //stop
  delay(1000); //hold for 1s until move again

  move(1, 100, 0); //motor 1, half speed, right
  move(2, 100, 0); //motor 2, half speed, right

  delay(120000);
  stop();
  delay(3360000);}
 
}

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-clockwise

  digitalWrite(STBY, HIGH); //disable standby

  boolean 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 standby 
  digitalWrite(STBY, LOW);
}
sc00bs
 
Posts: 2
Joined: Fri Apr 27, 2012 9:09 am

So this threw me through a loop. But I think I got it.

So delays are blocking. Meaning it never gets to the top of the loop to check the switches until the delays are all done. SO.... What I did is changed the delays to a function I made called wait.

what wait does it is just loops until the time is up just like delay, but can be stopped if a variable called stopWait is set to true. In the second sample, this also is where all the switches are checked.


Sample 2 requires no change to your setup. Sample 1 does.


In sample 1, to set stopWait to true in the middle of operation we need to use interrupts. These watch for any change on pins 2,3 and if there is any, it will set stopWait to true, and will break out of the wait loop and go to check the switches again.

sample 2 it is does by checking the switched over and over.

In sample 1
You will need to make the switches on pins 2/3 (cant be changed, just how the interrupts are) also you need to move PWMA from 3 to pin 6. (because 3 is used for the switches now)


Also, neither will wont work with momentary push buttons.






Sample 1
Code: Select all

/*
  DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor

This example code is in the public domain.
*/

// digital pin 2,3 control the program selection for the watch winder:
const int pushButton1 = 2;
const int pushButton2 = 3;

int switchstate1 = 0;
int switchstate2 = 0;

volatile boolean stopWait = false;

//motor A connected between A01 and A02
//motor B connected between B01 and B02

int STBY = 10; //standby

//Motor A
int PWMA = 6; //Speed control  NEED TO CHANGE THIS to 6
int AIN1 = 9; //Direction
int AIN2 = 8; //Direction

//Motor B
int PWMB = 5; //Speed control
int BIN1 = 11; //Direction
int BIN2 = 12; //Direction


// the setup routine runs once when you press reset:
void setup() {
  attachInterrupt(0, change, CHANGE); //will trigger immediately when there is a change on pin2 (inturrupt 0)
  attachInterrupt(1, change, CHANGE); //will trigger immediately when there is a change on pin3 (inturrupt 1)
 
 
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton1, INPUT);
  pinMode(pushButton2, INPUT);
 
  pinMode(STBY, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
 
  stopWait = false; //reset this
 
  // read the input pin:
  int switchstate1 = digitalRead(pushButton1);
  int switchstate2 = digitalRead(pushButton2);
  // print out the state of the button:
  Serial.println(switchstate1);
  Serial.println(switchstate2);
 
  if (switchstate1 = 1){
 
  move(1, 100, 1); //motor 1, full speed, left
  move(2, 100, 1); //motor 2, full speed, left

  wait(30000); //go for 30 sec
  stop(); //stop
  wait(1000); //hold for 250ms until move again

  move(1, 100, 0); //motor 1, half speed, right
  move(2, 100, 0); //motor 2, half speed, right

  wait(30000);  //go for 30 sec
  stop();
  wait(1000);

}else if(switchstate2 = 1){
  move(1, 100, 1); //motor 1, full speed, left
  move(2, 100, 1); //motor 2, full speed, left

  wait(60000); //go for 2 min
  stop(); //stop
  wait(1000); //hold for 1s until move again

  move(1, 100, 0); //motor 1, half speed, right
  move(2, 100, 0); //motor 2, half speed, right

  wait(6000); //go for 2min
  stop();
  wait(900000); //wait for 28 minutes

}else{
 
  move(1, 100, 1); //motor 1, full speed, left
  move(2, 100, 1); //motor 2, full speed, left

  wait(120000); //go for 1 second
  stop(); //stop
  wait(1000); //hold for 1s until move again

  move(1, 100, 0); //motor 1, half speed, right
  move(2, 100, 0); //motor 2, half speed, right

  wait(120000);
  stop();
  wait(3360000);}
 
}

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-clockwise

  digitalWrite(STBY, HIGH); //disable standby

  boolean 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 standby 
  digitalWrite(STBY, LOW);
}

void wait(unsigned long waitTime){
  unsigned long start = millis();
 
  //pause until time is up, or stopWait was changed.
  while(millis() - start > waitTime && stopWait == false);
 
}

void change(){
  stopWait = true;
  delay(50); // here to help prevent bounce issue
}






Sample 2
Code: Select all
/*
  DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor

This example code is in the public domain.
*/

// digital pin 2,3 control the program selection for the watch winder:
const int pushButton1 = 6;
const int pushButton2 = 7;

int switchstate1 = 0;
int switchstate2 = 0;

volatile boolean stopWait = false;

//motor A connected between A01 and A02
//motor B connected between B01 and B02

int STBY = 10; //standby

//Motor A
int PWMA = 3; //Speed control
int AIN1 = 9; //Direction
int AIN2 = 8; //Direction

//Motor B
int PWMB = 5; //Speed control
int BIN1 = 11; //Direction
int BIN2 = 12; //Direction


// the setup routine runs once when you press reset:
void setup() {
 
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton1, INPUT);
  pinMode(pushButton2, INPUT);
 
  pinMode(STBY, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
 
  stopWait = false; //reset this
 
 

  Serial.println(switchstate1);
  Serial.println(switchstate2);
 
  if (switchstate1 = 1){
 
  move(1, 100, 1); //motor 1, full speed, left
  move(2, 100, 1); //motor 2, full speed, left

  wait(30000); //go for 30 sec
  stop(); //stop
  wait(1000); //hold for 250ms until move again

  move(1, 100, 0); //motor 1, half speed, right
  move(2, 100, 0); //motor 2, half speed, right

  wait(30000);  //go for 30 sec
  stop();
  wait(1000);

}else if(switchstate2 = 1){
  move(1, 100, 1); //motor 1, full speed, left
  move(2, 100, 1); //motor 2, full speed, left

  wait(60000); //go for 2 min
  stop(); //stop
  wait(1000); //hold for 1s until move again

  move(1, 100, 0); //motor 1, half speed, right
  move(2, 100, 0); //motor 2, half speed, right

  wait(6000); //go for 2min
  stop();
  wait(900000); //wait for 28 minutes

}else{
 
  move(1, 100, 1); //motor 1, full speed, left
  move(2, 100, 1); //motor 2, full speed, left

  wait(120000); //go for 1 second
  stop(); //stop
  wait(1000); //hold for 1s until move again

  move(1, 100, 0); //motor 1, half speed, right
  move(2, 100, 0); //motor 2, half speed, right

  wait(120000);
  stop();
  wait(3360000);}
 
}

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-clockwise

  digitalWrite(STBY, HIGH); //disable standby

  boolean 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 standby 
  digitalWrite(STBY, LOW);
}

void wait(unsigned long waitTime){
  unsigned long start = millis();
 
  //pause until time is up, or stopWait was changed.
  while(millis() - start > waitTime && stopWait == false){
     
    if(digitalRead(pushButton1) != switchstate1){
       switchstate1 = digitalRead(pushButton1);
       stopWait = true;
    }
   
    if(digitalRead(pushButton2) != switchstate2){
       switchstate2 = digitalRead(pushButton2);
       stopWait = true;
    }
   
    delay(50) //help reduce bounce issues
   
  }
 
}

ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Hi Adam

Thanks for the suggestions

I tried option 1 first, swapped the connections around but get no movement on the motors at all so something wrong there, checked all my connection three times to make sure all 100%
Swapped connections back to as I had it and tried to load sample 2 but will not compile.

Will check it out and see if I can see where the problem is.

Regards

JohnB
sc00bs
 
Posts: 2
Joined: Fri Apr 27, 2012 9:09 am

Opps, I forgot a semicolon. Try this:

Code: Select all

/*
  DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor

This example code is in the public domain.
*/

// digital pin 2,3 control the program selection for the watch winder:
const int pushButton1 = 6;
const int pushButton2 = 7;

int switchstate1 = 0;
int switchstate2 = 0;

volatile boolean stopWait = false;

//motor A connected between A01 and A02
//motor B connected between B01 and B02

int STBY = 10; //standby

//Motor A
int PWMA = 3; //Speed control
int AIN1 = 9; //Direction
int AIN2 = 8; //Direction

//Motor B
int PWMB = 5; //Speed control
int BIN1 = 11; //Direction
int BIN2 = 12; //Direction


// the setup routine runs once when you press reset:
void setup() {
 
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton1, INPUT);
  pinMode(pushButton2, INPUT);
 
  pinMode(STBY, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
 
  stopWait = false; //reset this
 
 

  Serial.println(switchstate1);
  Serial.println(switchstate2);
 
  if (switchstate1 = 1){
 
  move(1, 100, 1); //motor 1, full speed, left
  move(2, 100, 1); //motor 2, full speed, left

  wait(30000); //go for 30 sec
  stop(); //stop
  wait(1000); //hold for 250ms until move again

  move(1, 100, 0); //motor 1, half speed, right
  move(2, 100, 0); //motor 2, half speed, right

  wait(30000);  //go for 30 sec
  stop();
  wait(1000);

}else if(switchstate2 = 1){
  move(1, 100, 1); //motor 1, full speed, left
  move(2, 100, 1); //motor 2, full speed, left

  wait(60000); //go for 2 min
  stop(); //stop
  wait(1000); //hold for 1s until move again

  move(1, 100, 0); //motor 1, half speed, right
  move(2, 100, 0); //motor 2, half speed, right

  wait(6000); //go for 2min
  stop();
  wait(900000); //wait for 28 minutes

}else{
 
  move(1, 100, 1); //motor 1, full speed, left
  move(2, 100, 1); //motor 2, full speed, left

  wait(120000); //go for 1 second
  stop(); //stop
  wait(1000); //hold for 1s until move again

  move(1, 100, 0); //motor 1, half speed, right
  move(2, 100, 0); //motor 2, half speed, right

  wait(120000);
  stop();
  wait(3360000);}
 
}

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-clockwise

  digitalWrite(STBY, HIGH); //disable standby

  boolean 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 standby 
  digitalWrite(STBY, LOW);
}

void wait(unsigned long waitTime){
  unsigned long start = millis();
 
  //pause until time is up, or stopWait was changed.
  while(millis() - start > waitTime && stopWait == false){
     
    if(digitalRead(pushButton1) != switchstate1){
       switchstate1 = digitalRead(pushButton1);
       stopWait = true;
    }
   
    if(digitalRead(pushButton2) != switchstate2){
       switchstate2 = digitalRead(pushButton2);
       stopWait = true;
    }
   
    delay(50); //help reduce bounce issues
   
  }
 
}





sc00bs wrote:Hi Adam

Thanks for the suggestions

I tried option 1 first, swapped the connections around but get no movement on the motors at all so something wrong there, checked all my connection three times to make sure all 100%
Swapped connections back to as I had it and tried to load sample 2 but will not compile.

Will check it out and see if I can see where the problem is.

Regards

JohnB
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Hello,

nice project !

may i ask where you have got the schematic and material for Fritzing this part ?

Thanks in advance
Chris
MBChris
 
Posts: 2
Joined: Sun May 27, 2012 12:08 pm

It's actually not Fritzing, I just did the illustration my self in adobe illustrator.


MBChris wrote:Hello,

nice project !

may i ask where you have got the schematic and material for Fritzing this part ?

Thanks in advance
Chris
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

ameyer wrote:It's actually not Fritzing, I just did the illustration my self in adobe illustrator.
]


Hey, thanks anyway
MBChris
MBChris
 
Posts: 2
Joined: Sun May 27, 2012 12:08 pm

How would i wire up a single motor? Also, I've been reading that I need to put a diode in the circuit to protect against back EMF; where in the circuit would I put this diode?
phaggood
 
Posts: 1
Joined: Sat Oct 20, 2012 3:46 pm

You would do the same, just dont plug in a second motor. You could remove the the wires for the B motor control if you wanted as well.

I though it had diodes built in, but nope. Look here for how to hook them up: http://www.instructables.com/id/Using-t ... B6612FNG-/

And you can use these: https://www.sparkfun.com/products/8589

If the motor only needs to spin a single direction, these would be much easier:

http://bildr.org/2012/03/rfp30n06le-arduino/
http://bildr.org/2011/03/high-power-con ... nd-tip120/



Screen Shot 2012-10-22 at 11.19.25 AM.png
Screen Shot 2012-10-22 at 11.19.25 AM.png (20.4 KiB) Viewed 11073 times



phaggood wrote:How would i wire up a single motor? Also, I've been reading that I need to put a diode in the circuit to protect against back EMF; where in the circuit would I put this diode?
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Hello,

May i ask what Morror Power means? It's probably a typo but it is in your Custom Fritzing layout.

Can anyone help?

Kindest Regards,

Joe
Joe2884
 
Posts: 1
Joined: Fri Dec 21, 2012 4:24 pm

Sorry... Motor power. The power specifically for the motor.
No, no fritzing. The illustration was done in Adobe Illustrator.

Joe2884 wrote:Hello,

May i ask what Morror Power means? It's probably a typo but it is in your Custom Fritzing layout.

Can anyone help?

Kindest Regards,

Joe
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

This is a great write up and intro project for the TB6612FNG. I would like to build on this project, but I'm a dangerous noob when it comes to electronic projects. That being said I would really appreciate some guidance on how to include some sort of dial(s) to adjust the global speed of the motors (not the rotation pattern defined in the arduino code). Would a pot or digital encoder be the right component to add? How would I change the code to reflect added component?

Thanks!

Abe
abefroman
 
Posts: 3
Joined: Mon Jan 07, 2013 4:17 pm

A pot would be the way to go.
Say you have the pot on A0, you could just do something like this


Code: Select all
int potReading = analogRead(A0); //reads 0-1024=3, so we /4 so it is 0-255 needed for the speed
move(1, potReading/4, 0); //motor 1 @ speed set by pot, right





abefroman wrote:This is a great write up and intro project for the TB6612FNG. I would like to build on this project, but I'm a dangerous noob when it comes to electronic projects. That being said I would really appreciate some guidance on how to include some sort of dial(s) to adjust the global speed of the motors (not the rotation pattern defined in the arduino code). Would a pot or digital encoder be the right component to add? How would I change the code to reflect added component?

Thanks!

Abe
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

The previous page mentioned how to control the SPEED two motors with one Uno board, but my question is how to control the ACCELERATION of those two motors.

Also, what is the nature of the signal required by the Uno board? Is it the voltage of the motor, the desired motor speed or the desired motor acceleration?


Thank you!
bangchui886
 
Posts: 1
Joined: Wed Jan 09, 2013 2:27 am

I don't think you can. The acceleration is a property of the motor, and the voltage applied to it.

The arduino outputs a PWMed signal to the motor. (read here: http://wiki.bildr.org/index.php/Pulse_Width_Modulation) So the voltage is always the same, but duty cycle changes. On most motors, this changes both top-speed and acceleration to that speed.


bangchui886 wrote:The previous page mentioned how to control the SPEED two motors with one Uno board, but my question is how to control the ACCELERATION of those two motors.

Also, what is the nature of the signal required by the Uno board? Is it the voltage of the motor, the desired motor speed or the desired motor acceleration?


Thank you!
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Awesome. I get the first line of code, but the second throws me as to where it belongs.

I dont know how much work it would be, but if you have the time (and desire) I would really appreciate it if you could post a simple working code that includes a pot. I am pretty good at taking working code and tweaking it, but usually fail and give up when I am starting from scratch.

Also, how would you recommend I add in the pot? I have a 10k pot in front of me, so would I hook up Terminal 1 and 2 to A0 on the arduino and Terminal 3 to the voltage source of the motors (variable resistor?)? Would this control the speed of both motors or just motor 1?

Thanks!

Abe

ameyer wrote:A pot would be the way to go.
Say you have the pot on A0, you could just do something like this


Code: Select all
int potReading = analogRead(A0); //reads 0-1024=3, so we /4 so it is 0-255 needed for the speed
move(1, potReading/4, 0); //motor 1 @ speed set by pot, right





abefroman wrote:This is a great write up and intro project for the TB6612FNG. I would like to build on this project, but I'm a dangerous noob when it comes to electronic projects. That being said I would really appreciate some guidance on how to include some sort of dial(s) to adjust the global speed of the motors (not the rotation pattern defined in the arduino code). Would a pot or digital encoder be the right component to add? How would I change the code to reflect added component?

Thanks!

Abe
abefroman
 
Posts: 3
Joined: Mon Jan 07, 2013 4:17 pm

That line of code would go directly below the pot read as shown.

Hook the pot up like this: http://arduino.cc/en/uploads/Tutorial/g ... rcuit3.png

Here is the full code

Code: Select all
//motor A connected between A01 and A02
//motor B connected between B01 and B02

int STBY = 10; //standby

//Motor A
int PWMA = 3; //Speed control
int AIN1 = 9; //Direction
int AIN2 = 8; //Direction

//Motor B
int PWMB = 5; //Speed control
int BIN1 = 11; //Direction
int BIN2 = 12; //Direction

void setup(){
  pinMode(STBY, OUTPUT);

  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);

  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
}

void loop(){
 
  int potReading = analogRead(A0); //reads 0-1024=3, so we /4 so it is 0-255 needed for the speed
  move(1, potReading/4, 0); //motor 1 @ speed set by pot, right
 
}


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-clockwise

  digitalWrite(STBY, HIGH); //disable standby

  boolean 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 standby 
  digitalWrite(STBY, LOW);
}




abefroman wrote:Awesome. I get the first line of code, but the second throws me as to where it belongs.

I dont know how much work it would be, but if you have the time (and desire) I would really appreciate it if you could post a simple working code that includes a pot. I am pretty good at taking working code and tweaking it, but usually fail and give up when I am starting from scratch.

Also, how would you recommend I add in the pot? I have a 10k pot in front of me, so would I hook up Terminal 1 and 2 to A0 on the arduino and Terminal 3 to the voltage source of the motors (variable resistor?)? Would this control the speed of both motors or just motor 1?

Thanks!

Abe

ameyer wrote:A pot would be the way to go.
Say you have the pot on A0, you could just do something like this


Code: Select all
int potReading = analogRead(A0); //reads 0-1024=3, so we /4 so it is 0-255 needed for the speed
move(1, potReading/4, 0); //motor 1 @ speed set by pot, right





abefroman wrote:This is a great write up and intro project for the TB6612FNG. I would like to build on this project, but I'm a dangerous noob when it comes to electronic projects. That being said I would really appreciate some guidance on how to include some sort of dial(s) to adjust the global speed of the motors (not the rotation pattern defined in the arduino code). Would a pot or digital encoder be the right component to add? How would I change the code to reflect added component?

Thanks!

Abe
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Worked like a charm. Thanks ameyer!

Now that I have everything up and running, do you have any idea why one of the motors isn't as responsive as the other( motor 1 turns slowly at a speed of 5 whereas motor 2 requires a minimum speed of 30 before it will turn- when they are both operational)? Since they aren't connected in series, I don't think it is how they are configured. Could it be the motors? Both are brand new from sparkfun. Not really a big issue, but it is very noticeable at any pot resistance. Oh, i did add the diodes to my project to prevent the EMF buildup - if it matters.


Abe
abefroman
 
Posts: 3
Joined: Mon Jan 07, 2013 4:17 pm

Hmmm...

Swap the motors (make sure to leave the diodes and everything else the same) and see if it is the same motor is slower. If it is, id say it is motor related.

Remove the diodes for a moment, see if it fixes it. If so, it could be the diodes hooked up incorrectly.



abefroman wrote:Worked like a charm. Thanks ameyer!

Now that I have everything up and running, do you have any idea why one of the motors isn't as responsive as the other( motor 1 turns slowly at a speed of 5 whereas motor 2 requires a minimum speed of 30 before it will turn- when they are both operational)? Since they aren't connected in series, I don't think it is how they are configured. Could it be the motors? Both are brand new from sparkfun. Not really a big issue, but it is very noticeable at any pot resistance. Oh, i did add the diodes to my project to prevent the EMF buildup - if it matters.


Abe
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Hi there,

I'm working on a project that will involve 6 DC gear motors. (http://www.pololu.com/product/1125) I have very limited space for the electronics, so I'm using an Arduino Nano 3.0, which has 14 digital I/O pins (6 of which are PWM). If I were to use 3 of the TB6612FNG drivers, each controlling 2 motors, I would need 3 pins for each motor, plus 1 pin for each driver's standby line, for a total of 21 pins. I also need to incorporate some sensors, so this definitely won't work!

One thing that might help is that my project never needs the motors to run simultaneously. Only one motor will ever need to be running at a time. This makes me think I might be able to condense the system to fit on the Arduino.

What if I hook up the signal lines of all three TB6612FNG boards to the same six digital pins, but put the three standby pins onto three separate outputs? (Using a total of 9 digital outputs.) When I want to power a particular motor, I would set the correct driver's standby pin HIGH, and bring the standby pins of the other two boards LOW. Would this work?

Can you suggest any other ways to do this?

Thanks! I love your tutorials!

-Eitan
pirsquared
 
Posts: 4
Joined: Sun Mar 09, 2014 2:48 pm

I think that would work perfectly. I haven't tried it, so I don't know for sure. But I think it's a great way to do it.


pirsquared wrote:Hi there,

I'm working on a project that will involve 6 DC gear motors. (http://www.pololu.com/product/1125) I have very limited space for the electronics, so I'm using an Arduino Nano 3.0, which has 14 digital I/O pins (6 of which are PWM). If I were to use 3 of the TB6612FNG drivers, each controlling 2 motors, I would need 3 pins for each motor, plus 1 pin for each driver's standby line, for a total of 21 pins. I also need to incorporate some sensors, so this definitely won't work!

One thing that might help is that my project never needs the motors to run simultaneously. Only one motor will ever need to be running at a time. This makes me think I might be able to condense the system to fit on the Arduino.

What if I hook up the signal lines of all three TB6612FNG boards to the same six digital pins, but put the three standby pins onto three separate outputs? (Using a total of 9 digital outputs.) When I want to power a particular motor, I would set the correct driver's standby pin HIGH, and bring the standby pins of the other two boards LOW. Would this work?

Can you suggest any other ways to do this?

Thanks! I love your tutorials!

-Eitan
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Cool, thanks!

On Pololu, they recommend using the Qik board http://www.pololu.com/product/1110. It says that multiple Qik boards can be daisy-chained together and driven off the same serial line. I'm curious what might be the pros and cons of using this instead of the TB6612FNG.

Thanks!

-Eitan
pirsquared
 
Posts: 4
Joined: Sun Mar 09, 2014 2:48 pm

Next

Return to Blog Posts

Who is online

Users browsing this forum: No registered users and 0 guests