Is my shared battery affecting PWM/Motor control?

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 (){}

Code and wiring diagram....???

What speed control? You use PWM 140 all the time.
Please post the schematics.

It doesn't change within the program, I just set it to 140 down from 255 and the resulting speed is the same. I've run similar example codes with wheels spinning different rates (in code) but they stay the same. I didn't make a schematic for this but I will now- do you have a suggested method for that or shall I just photoshop it?

Testing PWM from 0 to 255 You must experience differences.
Post a link to the datasheet of the motors as well as, at least schematics of the motor driver.

Pen and paper can show things...


**Correction: Battery + goes to 5v pin, NOT VIN.

I don't have a datasheet for the motors, they are just cheap handy chineese planetarys that I use for stuff. Whilst free spinning a wheel in the circuit they draw 10-20mA at 3.6v

The motor drive is pretty standard from the looks of it, it is linked in the original post and described in detail at this link.

Pins 2 and 8 are NOT pwm pins, 3, 5, 6, 9, 10, 11 are.

2 Likes

Don't send videolinks, send links to datasheets. Finding interesting data takes a minute from datasheets. Spending time watching 30 - 45 minutes of video is not how helpers want to spend their free, unpaid time.
Never mind. An important reply just dropped down.

That sure was it! I actually should have remembered that from a project a few years ago. Thanks! I'm almost out of things to improve now! One of the few remaining is that I was going to try to suspend the routine when its plugged into the USB. Is there an easy way to do that? I was thinking I could read the voltage from a 5v pin but that seems like a bit of a workaround. I saw some example where you'd read from the serial port but I think that may interfere with my command instructions which are already reading from the serial.

This is a pretty minor detail, really- but I know a typical USB will struggle with current requirements (though this example is actually ok).

It's not clear ehat You ask for in the previous post.
Suspend routine.....? Please be more specific.
Measure "a voltage".....? Maybe make a drawing showing the idea?

This robot performs a little routine based on whatever letters you put in the one command string. He starts this every time he gets powered up including when he gets power from the laptop's USB rather than the battery.

I'd prefer he only perform the routine when he's turned on with his switch and running on battery power. This means I have to find a flag indicating that the USB is connected. One thing I could do is recognize that 5v is now present on the board (because of the incoming USB power). I feel like there's likely a better way to establish that flag.

Check the following:

Serial.begin(9600);
delay(100);
If (Seral.available()) USBExist= true;
else USBExist = false;

I'm on the phone for the moment.....

Your diagram suggests you are using a Nano and powering it with a 3.7 V battery to "Vin". :astonished:

oh yea! I think I did at first but later moved it to 5v. Should I connect it to 3.3v or something else instead?

Definitely not!

The 3.3 V is a reference output from the USB interface chip, and has a very limited current capability at that. Unwise to use it for anything really.

The Nano will generally run at 3.7 V but if the voltage gets too low, it may not operate with its 16 MHz clock. Also you are unnecessarily powering the USB interface chip which although it may not be much current, if it goes into "standby" mode with no USB connected, means it draws current even if you put the ATmega to sleep.

You would be better off in terms of current draw and reliability, to use an 8 MHz Pro Mini.

Unfortunately, a 3.7 V LiPo at 3.7 V and 4.2 V on charge, is beyond the ratings of most 3.3 V processors such as the ESP8266, so you need to use a (LDO) regulator. Presumably all mobile phones necessarily incorporate such a regulator as part of their power management.

Ok, thanks! I read a bit last night about resetting the nano to run at 8mhz also. Nano's are my default board while learning this stuff but I've been venturing into others like Esp32, and Tiny85 lately. I don't have an Pro Mini yet, but it sounds like a good fit for the next redesign. I'm probably going to redo this robot with some micro encoder motors so the mini seems pretty ideal.
I do have power modules also but in this case wanted to keep everything small & simple.

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