Two DC Motors Spin Alternatively; Not Simultaneously?

L298N Motor Driver is fine.
DC Motors are fine.
Arduino Nano is fine.
Connections are fine.
9V 850mA Battery and currents/voltages are fine.

Basically I have two 1.5V~3V DC motors and each of their codes run fine, just not at the same time, so maybe not fine. One motor will spin, THEN the other will in sequence. Motor A > Motor B > Motor A > Motor B

Plus another issue or two with the motor functions…

Pin defining, setup, and loop :

// Motor A and B speed control and logic pins
int const pwm_1 = 3;
int const in_1 = 4;
int const in_2 = 5;
int const pwm_2 = 6;
int const in_3 = 7;
int const in_4 = 8;

//setup for initalizing both motors' outputs
void setup() 
{
  pinMode(in_1, OUTPUT);
  pinMode(in_2, OUTPUT);
  pinMode(pwm_1, OUTPUT);
  pinMode(in_3, OUTPUT);
  pinMode(in_4, OUTPUT);
  pinMode(pwm_2, OUTPUT);
}

//loop containing the two sets of rotations from both motors with brakes in between them
void loop()
{
  motorclock();
  brake();
  motorcounter();
  brake();
}

So all of the above should be alright except the loop… but I’m not sure if I’ll need to change anything other than that because of this specific issue.

Motor functions are below :

//motor one and two clockwise for three seconds at 255
void motorclock()
{

  digitalWrite(in_1,HIGH);
  digitalWrite(in_2,LOW);
  digitalWrite(in_3,HIGH);
  digitalWrite(in_4,LOW);

  analogWrite(pwm_1,255);
  analogWrite(pwm_2,255);
  
  delay(3000);
}

//motor one and two counterclockwise for three seconds at 255
void motorcounter()
{

  digitalWrite(in_1,LOW);
  digitalWrite(in_2,HIGH);
  digitalWrite(in_3,LOW);
  digitalWrite(in_4,HIGH);
  
  analogWrite(pwm_1,255);
  analogWrite(pwm_2,255);

  delay(3000);
}

//brake function for once second before each rotation
void brake()
{

//motor one and two double highs for brake
  digitalWrite(in_1,HIGH);
  digitalWrite(in_2,HIGH);
  digitalWrite(in_3,HIGH);
  digitalWrite(in_4,HIGH);
  delay(1000);
}

There’s also one more issue, the motors don’t seem to change direction, it seems like it’s just running the first two lines over and over like so:

motorclock();
brake();
motorclock();
brake();
motorclock();
brake();
motorclock();
brake();
motorclock();
brake();
motorclock();
brake();

Instead of both directions as it’s supposed to like so:

motorclock();
brake();
motorcounter();
brake();
motorclock();
brake();
motorcounter();
brake();
motorclock();
brake();
motorcounter();

I did think of one possible solution but it involved something along the lines of arrays/stringing and I’m not well-versed in that just yet as a beginner. I assumed I could just force them to spin at the same time like so;

  digitalWrite(in_1 + in_3,HIGH)
  digitalWrite(in_2 + in_4,LOW)

  analogWrite(pwm_1 + pwm_2,255)

But obviously that didn’t work and the motors didn’t move at all when I tried that. But that’s the only idea I had. I’m sure there’s an appropriate way to combine them, and I’m not sure if that’s the right way to go about getting the alternating DC motors to run at the same time.

Someone might think to mention it has to do with the voltage not being able to power both DC motors, but the behavior is so coordinated and they’re only 1.5~3V and I’m using a 9V 850mA DC cable. I’ve been told the L298N eats voltage but it doesn’t feel like the motors are fighting over power or shorting each other out; its timely.

Please post the complete program. It’s irritating have to switch between little snippets never knowing what you’ve missed out that might be important.

If anything it’s more likely to be that the power source cannot supply enough CURRENT to run two motors at the same time. Exactly what is powering the motor driver? First you call it a battery and then a “DC cable”.

Steve

can you post a diagram (hand drawn if necessary) of how your motors are connected

your code seems pretty straight forward. if there's only enough current for one motor, neither motor would turn

i think one possible explanation is that you've connected one of your "in_" pins the the enable of an h-bridge instead connecting the "pwm_" pin.

Is this somehow related to this thread? https://forum.arduino.cc/index.php?topic=663829.0

slipstick:
Please post the complete program. It’s irritating have to switch between little snippets never knowing what you’ve missed out that might be important.

If anything it’s more likely to be that the power source cannot supply enough CURRENT to run two motors at the same time. Exactly what is powering the motor driver? First you call it a battery and then a “DC cable”.

Steve

Yeah, sure. Thought breaking it down would be simpler but didn’t think that it would probably confuse you at a first glance.

If you look towards the end of this post you’ll see I picked up a new lipo battery and I forget the specs but I typed it down there.

I thought it could be a power source issue as well but again the motors seem to be intentionally alternating as if the issue is in the code format for the motor clockwise/counter-clockwise movements.

It doesn’t seem like messy, jumbled-up voltage transferring. Of course you could be right, who knows, maybe it is the current. Also, it’s a DC cable, not a battery, I apologize for the confusion.

Here you go;

// Motor A and B speed control and logic pins
int const pwm_1 = 3;
int const in_1 = 4;
int const in_2 = 5;
int const pwm_2 = 6;
int const in_3 = 7;
int const in_4 = 8;

//setup for initalizing both motors' outputs
void setup() 
{
  pinMode(in_1, OUTPUT);
  pinMode(in_2, OUTPUT);
  pinMode(pwm_1, OUTPUT);
  pinMode(in_3, OUTPUT);
  pinMode(in_4, OUTPUT);
  pinMode(pwm_2, OUTPUT);
}

//loop containing the two sets of rotations from both motors with brakes in between them
void loop()
{
  motorclock();
  brake();
  motorcounter();
  brake();
}

//motor one and two clockwise for three seconds at 255
void motorclock()
{

  digitalWrite(in_1,HIGH);
  digitalWrite(in_2,LOW);
  digitalWrite(in_3,HIGH);
  digitalWrite(in_4,LOW);

  analogWrite(pwm_1,255);
  analogWrite(pwm_2,255);
  
  delay(3000);
}

//motor one and two counterclockwise for three seconds at 255
void motorcounter()
{

  digitalWrite(in_1,LOW);
  digitalWrite(in_2,HIGH);
  digitalWrite(in_3,LOW);
  digitalWrite(in_4,HIGH);
  
  analogWrite(pwm_1,255);
  analogWrite(pwm_2,255);

  delay(3000);
}

//brake function for once second before each rotation
void brake()
{

//motor one and two double highs for brake
  digitalWrite(in_1,HIGH);
  digitalWrite(in_2,HIGH);
  digitalWrite(in_3,HIGH);
  digitalWrite(in_4,HIGH);
  delay(1000);
}

gcjr:
can you post a diagram (hand drawn if necessary) of how your motors are connected

your code seems pretty straight forward. if there’s only enough current for one motor, neither motor would turn

Did my best, take a look. The actual images of the project on the 3rd/4th page in my original thread someone below you brought up could be referenced.

I thought the same thing, either neither would work, or they would both work but very erratically and not as coordinated as they are now. I’d post a video but I’m having trouble getting them to stay in place with something on them to spin while I record. Otherwise you’ll just hear the motors turning on and off and that’s kind of pointless.

Anyways, I still feel like it’s something basic in the code that I’m overlooking. I think each motor is overriding the other somehow with the HIGH’s and LOW’s… not sure why or how though.

Here’s the diagram I put together, it was a little messy so I thought to make a legend… lol. Hope it helps.

gcjr:
i think one possible explanation is that you’ve connected one of your “in_” pins the the enable of an h-bridge instead connecting the “pwm_” pin.

I read somewhere that only some of the Arduino Nanos Pins could be designated as PWM pins, I can’t recall which exact pin numbers they were, but I remember 3 and 6 were two of them and that’s why I use 3 and 6. I could be wrong?

detown:
Is this somehow related to this thread?
https://forum.arduino.cc/index.php?topic=663829.0

Yes, that’s the original project and the photos near the end of the thread are pretty accurate to what I have set-up now.

Also, kind of unrelated but I picked up some other stuff if you want to take a look.

It’s

  1. A 7.4V 5200mAh 2S 50C Lipo battery and LiPo battery charger;
    (2 objects on the right side of 1st image)

I know 2S means there’s two of something in there, but I’m not sure what 50C refers too.

  1. A 5 set of ULN2003 motor driver boards and 5V stepper motors.
    (Second Picture, it’s in a bag but here’s a link)
    https://www.amazon.com/gp/aw/d/B01CP18J4A?psc=1&ref=ppx_pop_mob_b_asin_title

  2. this Raspberry Pi 4 kit I was able to get with store credit from an older purchase I couldn’t get a refund for at Best Buy lol; not sure if it could be used for motors but I’m tucking it away for future use.
    (Third Picture)

Hi,
Is this still your DMM?


If so, can I suggest you invest in a proper DMM that can measure voltages, not just indicate if they are in the ball park?

Tom... :slight_smile:

i don't see anything wrong with your connections based on your diagram

i would suggest you try controlling just one motor, maybe disconnecting the other motor to verify that your understanding of how it needs to be connected and controlled are correct.

and try changing the speed (pwm). i think it may be a bit ambiguous what it tells you, but may be helpful

TomGeorge:
Hi,
Is this still your DMM?


If so, can I suggest you invest in a proper DMM that can measure voltages, not just indicate if they are in the ball park?

Tom... :slight_smile:

It’s still my meter, I shouldn’t have gotten the cheaper one :/, now it’s kind of useless...

I need to head over to a drop point to pick up a DRV8833 I wasn’t at home to receive. There’s a Home Depot right by there so hopefully they have a better one.

If you had the opportunity to, how would you implement the more informative DMM to help solve the problem here though? The voltage only outputs to the motors when it’s scheduled to rotate by the coding, which the current meter reads at those moments.

gcjr:
i don't see anything wrong with your connections based on your diagram

i would suggest you try controlling just one motor, maybe disconnecting the other motor to verify that your understanding of how it needs to be connected and controlled are correct.

and try changing the speed (pwm). i think it may be a bit ambiguous what it tells you, but may be helpful

Interesting. So if there’s no change in the motor that’s currently connected the issue could be isolated as a coding issue, and not with the physical set-up. That’s a good idea, I’ll try it.

If it does run differently, that would tell me it’s definitely a current issue?...

...if that’s true I’m going to abandon that set-up and try using the new motor board, stepper motors, and Lipo battery.

But will the 7.4V Lipo battery not be enough for the two 5V steppers motors (10V) to run properly?

Maybe I should stick with the current DC cable I have.... or maybe use the Lipo for this original DC motor set-up (6V) and the DC cable for the new stepper-motor set-up (10V).

are your working with 2 DC motors or a stepper motor?

i don't understand how if there's insufficient current only one DC motor operates correctly? wouldn't the voltage drop affect both motors?

gcjr: are your working with 2 DC motors or a stepper motor?

i don't understand how if there's insufficient current only one DC motor operates correctly? wouldn't the voltage drop affect both motors?

I’m working with 2 DC motors.

That’s exactly what I’m saying, the voltage drop would be noticeable in the behavior of both motors. They’re not acting erratically they’re just not running at the same time, one runs and then the other in equal segments.

The code is running the motors alternatively instead of at the same time.

Hi, Can you please post a picture of your project so we can see your component layout?

Thanks.. Tom... :)

Are you saying that this alternate running happens both clockwise and counterclock? Or is it still only clockwise?

How long does Motor1 run before it switches over to Motor2?

Instead of guessing what code is running why don't you put some Serial.prints in motorclock(), motorcounter() and brake() so you know exactly what code is being called? If you also print millis() each time you'll also know the exact timing. Post the results.

Steve