I've been experimenting with a nano, TB6612FNG motor driver and LiPo to practice swich cases and coding syntax. I'm happy to report that's going great, and I have this neat little robot following my commands reliably at this point. As you can see, he's pretty little.. part of that is that I ignored a few of the recommended wiring guides and instead am sharing the motor input with a the arduino power input. My other sin is powering the whole thing with a single spare battery I had lying around. Despite these details everything is working great and the only shortcoming I've observed is that the PWM speed control seems to have no effect. My best guess is that this has to do with the shared motor and battery connection, but having split that off and temporarily clipped on a battery to wire separately the same lack of speed control seems to be in effect.
So, I'm interested to hear any thoughts on that, and further on the subject of batteries & motors, it seems fairly awkward especially for little test projects like this to always have to make room and complicate the design with two separate batteries (or at least a boost/buck converter). Is there some combination of boards that simplifies that aspect? I want to spend my time learning coding more than mix & matching power supplies/batteries. I get why you'd want separate supplies for a lot of applications, but for simple mechanical things in the 3-6v range I'd like to try to keep it simple.
https://www.youtube.com/watch?v=IHbCFEgCqkI
// FOR "ACEIRMC 5pcs DRV8833 Dual Motor Driver Compatible with TB6612 for Arduino Microcontroller Better Than L298N"
const int pinPWMA = 8;
const int pinPWMB = 2;
const int pinA1 = 6;
const int pinA2 = 7;
const int pinB1 = 4;
const int pinB2 = 3;
char CMDS[] = "PFLFBRBI";
int Ttime = 250; //turn time
int Mtime = 1000; //move time
int Ptime = 5000; //pause time
int Btime = 500; //Motor break time
int Ltime = 500; //LED blink timing
int Speed = 140; //presently only 0 or 255 will make the motor stop or start. It's supposed to be speed control.
int LED = 3; //number of LED blinks
void setup (){
{Serial.begin (115200);
while (!Serial); //meant to prevent running the program while USB is plugged in. It doesn't work (yet). Maybe I need to measure 5v at a pin.
pinMode (pinPWMA, OUTPUT);
pinMode (pinPWMB, OUTPUT);
pinMode (pinA1, OUTPUT);
pinMode (pinA2, OUTPUT);
pinMode (pinB1, OUTPUT);
pinMode (pinB2, OUTPUT);
pinMode (LED_BUILTIN, OUTPUT);
Serial.begin (115200);
//Serial.println(CMDS); print out the commands before we start.
}
int incomingByte = 0; //for incoming serial data
int len = strlen (CMDS); //define the variable len(gth) as the number of characters.
for (int i=0;i<len;i++) {Serial.println(CMDS[i]);;
switch (CMDS[i]) {
case 'F':
Serial.println ("F-orward");
digitalWrite (pinA1, HIGH);
digitalWrite (pinA2, LOW); // forward
digitalWrite (pinB1, HIGH);
digitalWrite (pinB2, LOW);
analogWrite (pinPWMA, Speed); //THIS IS SUPPOSED TO CHANGE SPEED BUT PRESENTLY ONLY TURNS ON.
analogWrite (pinPWMB, Speed);
delay (Mtime); //Move time for position change.
analogWrite (pinPWMA, 0); //Set PWM to zero stops motor.
analogWrite (pinPWMB, 0);
delay (Btime); //Brief motor stop to reduce back motor EMF
break;
case 'B':
Serial.println ("B-ack");
digitalWrite (pinA1, LOW);
digitalWrite (pinA2, HIGH); // back
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, HIGH);
analogWrite (pinPWMA, Speed);
analogWrite (pinPWMB, Speed);
delay (Mtime);
analogWrite (pinPWMA, 0);
analogWrite (pinPWMB, 0);
delay (Btime);
break;
case 'L':
Serial.println ("L-eft");
digitalWrite (pinA1, LOW);
digitalWrite (pinA2, HIGH); // turn left
digitalWrite (pinB1, HIGH);
digitalWrite (pinB2, LOW);
analogWrite (pinPWMA, Speed);
analogWrite (pinPWMB, Speed);
delay (Ttime); //Time spent making a turn
analogWrite (pinPWMA, 0);
analogWrite (pinPWMB, 0);
delay (Btime);
break;
case 'R':
Serial.println ("R-ight");
digitalWrite (pinA1, HIGH);
digitalWrite (pinA2, LOW); // turn right
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, HIGH);
analogWrite (pinPWMA, Speed);
analogWrite (pinPWMB, Speed);
delay (Ttime);
analogWrite (pinPWMA, 0);
analogWrite (pinPWMB, 0);
delay (Btime);
break;
case 'P':
Serial.println ("P-ause");
analogWrite (pinPWMA, 0);
analogWrite (pinPWMB, 0);
delay (Ptime);
break;
case 'I':
Serial.println ("I-nternal LED");
for (int i=0;i<LED;i++)
{
digitalWrite(LED_BUILTIN, HIGH); //Turns internal LED ON
delay (Ltime); //waits LED delay time while on
digitalWrite (LED_BUILTIN, LOW); //Turns off internal LED
delay (Ltime); //waits LED time while off.
}
}
} }
void loop (){}