Go Down

Topic: Can arduino uno R3 analog pins be used as digital PWM pins? (Read 12305 times) previous topic - next topic

tiestobob

Can I use the analogWrite() function to use analog pins A0 through A5 as digital PWM out pins ? If so, how do you do that?

JimboZA

The reference page says this:

Quote
The analogWrite function has nothing to do with the analog pins
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

tiestobob

Yes I know but I am wondering how to use my analog pins as PWM digital out pins? How can I do this?

MarkT

The analog input pins can also be configured as standard digital pins (not hardware PWM),
so software PWM can be used with them, but not hardware.  I think there are a couple of
software PWM libraries knocking around, and for instance the Servo library
can drive these pins as servo control lines of course.

Remember to call the pins A0..A5 when using them with pinMode(), digitalRead() and
digitalWrite ().  analogRead () allows the numbers 0..5 as aliases for these full pin names.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

retrolefty


Yes I know but I am wondering how to use my analog pins as PWM digital out pins? How can I do this?


Well you can't because the arduino analogWrite() function uses chip hardware timers and the timers are 'hardwired' internally in the chip to just specific pins. Some avr chips have more timers then other chips so some have more hardware PWM pins the others, as in the mega2560 Vs mega328 chips.

You can however write or find a software PWM library that performs the PWM function in software and would be free to utilize any digital output pin including the analog pins.

Lefty

tiestobob

Ok let me explain the problem maybe its more clear:

I bought a motor driver shield, a bluetooth shield and an arduino uno r3. The motor driver shield stacks up onto the uno r3 and I don't know which pins the motor drivers uses so I have to stack it and know it's working properly. But now my HC05 bluetooth shield requires a digital PWM pin and I dont have access to them anymore since the motor shield has covered them when i stacked it. I only have access to analog pins A0 through A5. Can I use these instead??

JimboZA

Surely if you know the motor shield is working, you have a sketch running to drive your motors, and therefore you must know what pins it uses?- how else could you write a sketch for it to drive motors?  You need to know what pins to turn off and on for motor control and to pwm for speed.

As far as physical access goes, most shields reveal the pins on top again through their headers.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

tiestobob

Hi Jimboza,

Yes the shield has headers and it reveals the pins of the uno R3 but I don't know if those pins are being used or not. What if I connect something to pin #10 and the motor shield was using it? What would happen?

And I am using the motor shield library and just initializing an object of their library class so I don't know what's going on under the hood. I just use their library and tell the motor to take 10 steps forward or 10 steps backward. Here's what they say on their FAQ page:

All 6 analog input pins are available. They can also be used as digital pins (pins #14 thru 19)

Digital pin 2, and 13 are not used.

The following pins are in use only if the DC/Stepper noted is in use:
Digital pin 11: DC Motor #1 / Stepper #1 (activation/speed control)
Digital pin 3: DC Motor #2 / Stepper #1 (activation/speed control)
Digital pin 5: DC Motor #3 / Stepper #2 (activation/speed control)
Digital pin 6: DC Motor #4 / Stepper #2 (activation/speed control)

The following pins are in use if any DC/steppers are used
Digital pin 4, 7, 8 and 12 are used to drive the DC/Stepper motors via the 74HC595 serial-to-parallel latch

The following pins are used only if that particular servo is in use:
Digitals pin 9: Servo #1 control
Digital pin 10: Servo #2 control


I don't understand the first two blocks where it says "the following are used if the DC/stepper noted is in use" and "the following are used if any DC/stepper are used". What's the difference what are they trying to say it's not clear to me. I simply have 1 stepper motor connected to M3 and M4 of the Adafruit Motor Shield (link: http://learn.adafruit.com/adafruit-motor-shield?view=all)

JimboZA

#8
Nov 15, 2013, 09:09 am Last Edit: Nov 15, 2013, 09:19 am by JimboZA Reason: 1
It means that if you use ANY stepper or DC motors, 4, 7, 8 and 12 are not available to you. Over and above that, pin 11 and 3 will also be unavailable if you are using stepper 1, and 5 and 6 will be stolen if you use stepper 2. Dc1 uses 11, dc2 uses 3, dc3 and dc4 use 5 and 6.

Eg, if you use stepper 1, you will lose pins 4, 7, 8, 12 AND 11 and 3.

But if you use DC1 (and not 2) you'll lose 4, 7, 8, 12 AND 11 but you'll be able to use 3.

Use stepper 2, you lose 4, 7, 8, 12, AND 5 and 6.

Use both steppers, you lose 4, 7, 8, 12, AND 11 and 3 AS WELL AS 5 and 6.

That's the way I read that, anyway. E&OE.

EDIT: Presumably a stepper on m3 and m4 is either stepper 1 or 2 in the above terminology, I'd guess it's stepper 2 for numerical simplicity. (ie, probably stepper 1 is m1 m2, stepper 2 is m3 m4.... but that's a guess.)
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

tiestobob

You are a genius Jimboza. Thanks for clearing that up. They're basically saying then 4,7,8, and 12 are being used regardless (probably for power and GND and whatnot) and the following depicts which pins are used depending on if i'm using a stepper or DC motor (it makes sense since  a stepper has 4 wires so its equivalent pin usage is to 2 DC motors since DC motors just have 2 wires):

Digital pin 11: DC Motor #1 / Stepper #1 (activation/speed control)
Digital pin 3: DC Motor #2 / Stepper #1 (activation/speed control)
Digital pin 5: DC Motor #3 / Stepper #2 (activation/speed control)
Digital pin 6: DC Motor #4 / Stepper #2 (activation/speed control)

So in my case since I'm using M3 and M4 as shown on the Adafruit Motor Shield I'm assuming it's Digital Pins 5 & 6.

Cheers and thanks again

JimboZA

Quote
You are a genius Jimboza.


Please tell my kids that....., but yeah  8)
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

tiestobob

You have answered a lot of my noob questions. Thanks for being patient, unlike some of the other members.

Go Up