1 Arduino Nano with 2 Motors, how?

I have searched high and low for an answer on this and only find unhelpful code examples. I am trying to control 2 motors with 1 Arduino Nano. The example code in the Arduino documentation only references 1 motor and uses a pause/delay/sleep to simulate the PWM. How can the 2nd motor be controlled independently if the loop is being paused for the 1st motor? Other example I find have hard coded frequencies for the PWM. I want to be able to have 3 speeds with 2 motors. the 1st motor might be at speed #3 going forward and the 2nd motor might be at speed #1 going in reverse. How can I achieve this with the single Arduino? Maybe there is a code example out there that I am not finding.

More details. What sort of motors ? What are you driving them with. E.g. if they're brushless motors driven by ESCs it's easy, just use the Servo library.

Steve

Thanks for the reply. They are just regular DC motors. I have tiny ones I am using for testing and will upgrade when I get the logic down. Both of the motor drivers require the same thing (motor power, and PWM on the directional pin). These are probably not brushless drivers. But the problem is not with the equipment, it is with how the Arduino is programmed. In the “loop” function the PWM directions say to turn on the pin, then sleep/pause/delay for a certain amount of time (Which is the duty cycle) and then turn the pin off. Or maybe the delay is after you turn it off. In any case the problem is that there is only one loop. So when it hits the delay for motor 1, how can it possibly be controlling motor 2?
Do you know of any examples online that both supports two bi directional motors and supports input for speed and direction of both motors independently? This is where I am failing to find examples. The closest thing I got made both motors on the same PWM frequency or turned one motor off while the other was one on. But nothing that allowed changing the PWM of each motor separately.

If you want to do two things at once then you need to use more complex timing code than the simple blocking delay(). The idea is to use milli() to keep track of timing. The basic principle is covered in the standard "Blink without delay" example and extended to include the state machine concepts you'll need for independent control with detailed descriptions in https://forum.arduino.cc/index.php?topic=223286.0

You haven't said what motor drivers you are using but most people seem to use H-bridge motor drivers like L293D or L298N. There are many examples using these and other drivers e.g. http://tronixstuff.com/2014/11/25/tutorial-l298n-dual-motor-controller-modules-and-arduino/

BTW just for information, you don't change the speed of a motor by changing the PWM frequency but by changing the pulse width i.e. more high than low, high speed etc. That's why it's called PWM (Pulse Width Modulation).

Steve

I do have the L298N, and also another that is on its own board with a heat sink and can handle 9 amps or so. I use the L298N on a breadboard with a simple small motor and a 9v battery for development. Once I get the logic working correctly I will use the other H-Bridge board which uses the same principle to control the motor so its an easy swap. I have already used python with a Pi and had the motor working correctly with the motor driver, but I now want to use an Arduino instead. Only problem I ran into is that to do PWM I am instructed to create the PWM with the use of delay within a loop. Now I can imagine that this would work for one motor, but what do I do when I need 2 motors? I think I may just have to use 2 Arduino Nanos if no one knows the answer. Just seems like a waste of a Nano when I should be able to use 1 motor driver for 2 motors, and 1 Nano to operate it.

Have you actually looked at the examples I pointed you too ? Like the second one which is driving two motors with direction and speed control from one Arduino using a L293 ? Or the other which shows exactly how to switch several things on/off at varying times...which is what creating a PWM signal is ?

What you want to do is perfectly possible.

Steve

@Pi_Guy, you need to post your program. Without that it is impossible to know what the problem is.

And when posting code please use the code button </>so your code looks like thisand is easy to copy to a text editor See How to use the Forum

…R

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your present circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

We know you do not have a hardware problem, but it makes giving you answers easier if we can see what circuit we are controlling.

Thanks… Tom… :slight_smile:

Thanks for everyone's reply. @Steve, yes I know what you mean about PWM, I was saying frequency but meaning the duration of the sleep which would dictate the duration of the power cycle. (more sleep=slower motor, less sleep=faster motor, no sleep=full power motor). Steve, the link you showed for tronixstuff .com is exactly the link I was referring to when I said I found a link with hard coded "sleep" durations, and also that the loop contains both the left motor and right motor. But my problem with that example is that it does not take in any input to vary the PWM. I guess I will just have to try that code and alter the sleep with pin input values. I was just hesitant because I do not understand how both motors can be controlled independently with variable speeds from pin input values.

@Tom, sorry for not posting my code or circuit. Not sure how that will help you assist me in understand how to apply PWM on two motors in one loop like the one in the link shown below.

Here is a good example of what my circuit looks like. http://birkwood.net/wp-content/uploads/robotcontroller.png

And this is the best example of code that caused me to write this post in the first place. Problems with this code is that it does not take in input to vary the speed of the motor. http://tronixstuff.com/2014/11/25/tutorial-l298n-dual-motor-controller-modules-and-arduino/

Again, I think I will just move forward with using that code, and alter the delay with user input through the input pins. Maybe I am just skeptical for nothing. I am new to the Arduino and am just figuring all of this out.

Hi,
OPs diagram


Thanks.... Tom... :slight_smile:

Pi_Guy: sorry for not posting my code

Just post your code. This is the 3rd time of asking. It should have been in your Original Post.

...R

@Tom, yes that circuit looks similar to the one I am trying to build. Mine currently is hooked to a Raspberry Pi but I just got an Arduino Nano that I am trying to incorporate into the circuit and have the Nano handle the PWM. I do not have a fully developed circuit yet because I am still trying to figure out code for the nano and the Pi circuit already works.

@Robin2, please read my post carefully. I DO NOT HAVE CODE YET! Sorry that I need to shout at you but the whole reason I am here is to find code. To find code! Because again I will say that all code I find is either only showing one motor, or uses hard coded values for the PWM and does not take in any input. So if you want to help then please show me an example online of Arduino code that takes in inputs from pins to control the PWM and direction of two motors. It would be of great help to me because all I can find are the opposite of that.

Hi,

https://www.arduino.cc/en/Reference/AnalogWrite

Arduino has this command called analogWrite which is actually a PWM output command.

analogWrite(pin, duty), duty is int from 0 to 255 = 0 to 100%.

You do not need to bit bang, the nano has some pins dedicated to PWM, [u]look it up[/u].

You can command as many pins independently as you need as long as the pin is designed to do PWM.

Have you any Arduino programming experience.

In the IDE, goto Files, Examples, Basics, Fade. It will show you how to use the command. The code has been with you all the time!!!!!!

Tom..... :)

Thanks Tom, no I am brand new to the Arduino. I see what you are saying and did not understand the analogwrite command. The examples I was seeing do not take in any input. I was simply looking for an example that showed the use of two motors, input to vary that "duty" parameter, and also switch other pins that dictate the direction of the motor. I think I can just try and create the code on my own, but just figured I would be able to find an example. I know you say I have have the "analogWrite" this whole time, but just needed help trying to figure out how to use it with user input from the pins. I just thought this forum was the place to find the example code that illustrates what I am looking for.

Pi_Guy: Thanks Tom, no I am brand new to the Arduino.

Maybe you're "new" to using simple websearch tools, too. I hit up google with L298 arduino two motors and the following was at the top of the resulting page - https://tronixlabs.com.au/news/tutorial-l298n-dual-motor-controller-module-2a-and-arduino/

It's John Boxall, author of Arduino Workshop

https://www.amazon.com/Arduino-Workshop-Hands-Introduction-Projects/dp/1593274483/

Thanks Runaway Pancake but if you have not been reading this thread, that link you posted is the exact link we have all been referencing and it does not have input being used to control the PWM. Also your negativity is not needed here. Thanks for not helping and pointing out that I am new to Arduino. Let me ask you this, what is this forum for if it's not to ask questions?

Hi,

https://www.arduino.cc/en/Tutorial/AnalogInOutSerial

This may help, analog input to PWM output, its to control a LED but the coding principle is the same.

Tom.... :)

google analog read arduino pwm motor control

Thanks so much Tom. This should help. I have been using an LED for testing and just add on the LED circuit instead of the motor driver circuit because I have them on separate bread boards. I see how this code is using that input but am new to that “mapping” concept/syntax. But this looks more closely like what I have been looking for. Thanks again. I appreciate all your help. You have provided me with really useful info on this.

Pi_Guy:

  1. that link you posted is the exact link we have all been referencing and it does not have input being used to control the PWM.
  2. Also your negativity is not needed here.
  3. Let me ask you this, what is this forum for if it’s not to ask questions?
  1. Show me the link to the Boxall article here. You added the input angle later.
  2. Stop being dramatic.
  3. Ask succinct, intelligent questions.

Pi_Guy: @Robin2, please read my post carefully. I DO NOT HAVE CODE YET!

Then have a go at writing some - even if it is only for one motor.

It is so much easier to give help when there is something to build on.

...R