Which pins to use for PWM in Arduino Uno

Hi,

I am creating a motor control board which requires four PWM pins to control two motors. These pins directly control motor speed without using any kind of enable pins. Now the question is, which four pins should I use so that it does not conflict usability of other functions. My requirements are:

  1. I need two interrupt pins for speed control which are Digital Pin 2 and Pin 3. So, using Pin 3 for PWM is out of question
  2. Now I am left with Pin 5,6,9,10, 11. Which pins do I use so timers does not conflict with delay(), millis() and servo() functions as I require to use all three functions in the program as there are 2 servo pins for connecting servos.

Any help is appreciated.

Best Regards,
Praveen

The Servo library uses the Timer that controls pins 9 and 10 which would just leave you with pins 5. 6 and 11.

However if you use pinChange interrupts you are not tied to using Pins 2 and 3 for that purpose

See also Nick Gammon's interrupt tutorial

...R

I need two interrupt pins for speed control

That sounds unlikely, but anything is possible. Why do you need to use interrupts for speed control ?

UKHeliBob:
Why do you need to use interrupts for speed control ?

I suspect it is one for each motor ?

...R

Yes. I have an encoder module which requires two pins for each motor. However i need atleast one hardware interrupt pin to read each motor. So, does it mean i have no other choice?

Should i give up on speed control, or is there any other way out?

Perhaps use a pot for speed control or 2 buttons to jog the speed up and down. Either way you could use the analogue pins for that.

Use pin change interrupts, and use pin 3 for PWM. I generally prefer PCINTs over the INTn pins for hardware interrupts unless I have some specific requirements that preclude doing so.

IMO attachInterrupt is an abomination. I try to minimize my contact with it.

praveen_khm:
Yes. I have an encoder module which requires two pins for each motor.

I had been assuming you have a simple revolution detector that only requires 1 pin per motor.

AFAIK the reason an encoder has two connections is so that you can identify direction. Presumably you know the direction of the motor and don't need the encoder to tell you?

However that is not the main reason for this Post. How many pulses per revolution does your encoder produce? And how fast will the motor be rotating - i.e. what is the max number of pulses per second that one motor can produce?

My concern is that if you have a lot of pulses per second the Arduino won't be able to keep up. With one or two pulses per revolution there should be no problem - and one pulse per rev should allow very good speed control.

...R

praveen_khm:
Which pins do I use so timers does not conflict with delay(), millis() and servo() functions as I require to use all three functions in the program as there are 2 servo pins for connecting servos.

Why do you need delay(), it is a blocking instruction, while the delay is in operation, ALL of your code will STOP.
Tom... :slight_smile:

Use pin change interrupts, and use pin 3 for PWM. I generally prefer PCINTs over the INTn pins for hardware interrupts unless I have some specific requirements that preclude doing so.

In this case, I need to poll the registers every time, or need to use two different ports for each motor. Using interrupts is much easy. If there is no other option, then I need to use this method.

I had been assuming you have a simple revolution detector that only requires 1 pin per motor.

You are correct. I need only one pin which will still be the interrupt pin. Since I have multiple free pins, I use the second one for detecting direction. Yes, I already know the direction, but using the calculation in interrupt routine is much easier.

However that is not the main reason for this Post. How many pulses per revolution does your encoder produce? And how fast will the motor be rotating - i.e. what is the max number of pulses per second that one motor can produce?

I am using a motor that has a revolution of 320 per minute with 330 pulses per revolution.

Why do you need delay(), it is a blocking instruction, while the delay is in operation, ALL of your code will STOP.

At this moment, no. Its not there in the code. But I do not want to lose that option in case I require it. Timer is definitely used.

I am referring to the code mentioned in this link : 12V Low noise DC Motor 146RPM w/Encoder wiki-DFRobot

praveen_khm:
In this case, I need to poll the registers every time, or need to use two different ports for each motor. Using interrupts is much easy. If there is no other option, then I need to use this method.

Is a digitalRead() really that hard? I never felt it like that. And then you say:

You are correct. I need only one pin which will still be the interrupt pin. Since I have multiple free pins, I use the second one for detecting direction. Yes, I already know the direction, but using the calculation in interrupt routine is much easier.

I think checking a pin status is easier than this, and anyway you shouldn't be doing anything more than strictly necessary in an ISR (so NO calculations more complex than increasing a counter or so - leave that to your loop() to take care of).

praveen_khm:
I am using a motor that has a revolution of 320 per minute with 330 pulses per revolution.

The Motor Specifications you have linked to are a little different

No load RPM (before gearbox):8000 rpm
Gear ratio: 51:1
No load RPM (after gearbox): 146rpm@12V
Encoder Resolution: 13 PPR (663 PPR for gearbox shaft)

At 146 rpm = 2.4rps and 663 ppr that gives about 1600 pulses per second and with two motors that means about 3200 pulses per second. If they were all evenly spaced that would mean about 312 microsecs between interrupts. That does not leave a lot of time for the Arduino to do other things.

...R

That does not leave a lot of time for the Arduino to do other things.

...R

So, does it mean it's not a good idea to read the speed? Any suggestions and a better idea? I am more of a hardware guy and i need to design the board with these implementations. If things don't seem to work, i might as well skip reading speed and just make a motor controller. But having additional options is always a plus.

I am not saying "don't read the speed". I am just suggesting that the technology you are proposing to use for reading the speed may not be appropriate if it uses so much of the processor's time that it does not have enough time to do the rest of the stuff you want it do (which you have not told us).

...R

Robin2:
I am not saying "don't read the speed". I am just suggesting that the technology you are proposing to use for reading the speed may not be appropriate if it uses so much of the processor's time that it does not have enough time to do the rest of the stuff you want it do (which you have not told us)

...R

The microcontroller is basically used only to control two motors and two servos. Four additional pins are used for RC input. That's all the things this microcontroller does. Since i am still designing the board, i thought of adding encoder support which makes it a complete package. I am not sure what you mean by technology used. Do you mean the motor is inappropriate, or is there a better software solution? Or should i use any other method to read speed.

As far as this discussion goes, i have almost decided to give up on reading speed. Just checking if there is a last ray of hope.

Either ways, thank you for taking the time to help me.

praveen_khm:
The microcontroller is basically used only to control two motors and two servos. Four additional pins are used for RC input.

Slow down a bit. That's the first time you have mentioned RC input. How do you propose to obtain the RC input?

That may not sit at all nicely with speed sensing as it may also need interrupts.

I am not sure what you mean by technology used.

The encoder you are using to detect speed - because it has a lot of pulses per revolution.

...R

Hi,
What is the application that your project is for?
What are you radio controlling?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
What is the application that your project is for?
What are you radio controlling?

Thanks.. Tom... :slight_smile:

Radio controlling the motor itself.