1

I wanted run two motors using Cytron MDD10A Dual Channel motor driver. So my concern is based on execution of code that is done line by line, in that case when I'm driving two motors will this delay in output signal at the PWM pins make a difference in nature of performance i.e. for example a car wheel, in case there is such delay it'll cause one wheel to rotate prior to another which is not desired. If yes, how do I rectify the same. I also wanted to know when I use the delay function does it cause the delay in execution of the previous line or reads the upcoming line upon some 'x' seconds of delay?

Thank you for your help.

edit: code sample:

void FWD() {       // to move forward
    digitalWrite(m1, HIGH);  // motor 1 ON 
    digitalWrite(m2, HIGH);  // motor 2 ON                           
    analogWrite(tm1, 225);   // tm1 assigned to a PWM pin
    analogWrite(tm2, 225);   // tm2 assigned to another PWM pin
} 

in the above function I wish to run both analogWrite at the same time as I'm not sure how will it affect the output? I wish for the motors to start at the same time.

10
  • What is an O/P signal? Sep 25, 2020 at 15:56
  • sorry, output signal. I wish to send two PWM signal at the same time to two different PWM pins. Sep 25, 2020 at 15:58
  • 1
    you would have to post your code so we can fully understand what your talking about. But in general PWM signal are generated via timers and underlying hardware, so you can have multiple running at the same time. A delay call will pause on its line. It will have already completed the previous line.
    – Chad G
    Sep 25, 2020 at 16:06
  • code sample: void FWD(){ //to move forward digitalWrite(m1, HIGH);//motor 1 ON digitalWrite(m2,HIGH);//motor 2 ON analogWrite(tm1,225);// tm1 assigned to a PWM pin analogWrite(tm2,225;)//tm2 assigned to another PWM pin } in the above function I wish to run both analogWrite at the same time as I'm not sure how will it affect the output? I wish for the motors to start at the same time. Sep 25, 2020 at 16:14
  • 1
    Arduino's(and any other device with a single core processor) can only do one thing at a time. But you will not be able to tell which motor starts. The analog write function does not wait for the motor to do anything, it simply sets timers and interrupts to handle this. You should study the "Blink without Delay" example to understand how to do multiple things "at the same time". Both motors will start at the same time.
    – Chad G
    Sep 25, 2020 at 16:18

2 Answers 2

2

Technically, it is possible to start two PWM outputs at the exact same time on an Uno or Nano. If they are both controlled by the same timer, you just have to update the corresponding control register once with the relevant bits set to enable both channels. For example, you could start outputs 5 and 6 simultaneously, as both are on Timer 0:

OCR0A   = value;  // set the duty cycle on OC0A = digital 6
OCR0B   = value;  // set the duty cycle on OC0B = digital 5
TCCR0A |= _BV(COM0A1) | _BV(COM0B1);  // enable both outputs

If the outputs are on different timers, it is a bit more tricky. Timers 0 and 1 can be synchronized by clearing their shared prescaler with the Timer/Counter Synchronization mode active. I do not think there is a simple way to synchronize Timer 2 with any of the others.

All this is highly hardware-dependent, and not supported at all by the Arduino core library. Thus, if you want to apply any of this, you will have to study the datasheet, and poke directly into the I/O registers of the microcontroller.

But then, as Delta_G clearly points out in his answer, all this is irrelevant to motor control. Motors are mechanical devices that have significant inertia. They take time to speed up and slow down. Just to give you a sense of scale, PWM outputs 3, 9, 10 and 11 deliver a square wave with a period of 2.04 ms. Yes, those are milliseconds, and this is too short for the motors to significantly change their speed within one period. Contrast this with the handful of microseconds, that the call to analogWrite() will take.

1

I don’t think your fear is founded. Yes technically one motor starts after the other but the amount of time would be a few microseconds at most. For perspective it takes you about 200,000 microseconds to blink your eye. You will not be able to tell. They will appear to start at exactly the same time. You would need some very sophisticated high speed cameras to see the difference in timing. It wouldn’t affect how your car moves.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.