Analog write - selecting pins

Hi All,

A very noob question but I've been working with digital up until now so I need some help. When I'm defining my pins, what number do I use for the analog pins? Would I use 'A0' or '15?' Thought I should ask before I go and destroy my components.

Thanks in advance!

Jack

When I'm defining my pins, what number do I use for the analog pins? Would I use 'A0' or '15?'

No. The analog pin numbering starts at 0, just like the digital pins.

The A0, A1, A2, etc. aliases are defined for when the analog pins are being used as digital pins.

Hi Paul5,

Thanks for the speedy reply! So how should I go about defining the pins if there are digital pins being used as well? I've got sensors using pins 4-10 but I need to have an analog output on A0.

Thanks in advance,

Jack

but I need to have an analog output on A0.

Then you're out of luck. You could have bit-banged PWM, but not hardware-driven PWM

PaulS:

When I’m defining my pins, what number do I use for the analog pins? Would I use ‘A0’ or ‘15?’

No. The analog pin numbering starts at 0, just like the digital pins.

The A0, A1, A2, etc. aliases are defined for when the analog pins are being used as digital pins.

Not quite. The analog pins (talking the Uno etc here) are numbered 14 and up, which have aliases A0…A7.
Just for the purposes of analogRead there are also aliases 0…5 which refer to the analog pins, but its perfectly
fine to use A0…A7 or 14…21 as an argument to analogRead().

The source code is the place to check this sort of detail, specifically the files
/hardware/arduino/cores/arduino/wiring_analog.c
/hardware/arduino/variants/standard/pins_arduino.h

BTW analogWrite() only works on the PWM pins which are 3,5,6,9,10,11 on the Uno. This is not
actually analog output.

[and pins A6 and A7 are only available on some boards and are not digital capable pins, they only
support analogRead()]

Hi MarkT,

Right, so if I were to use pin 11 say and use analogWrite would that work? I've basically had this problem where my digital servo has given up and I've had to swap it for a more powerful one that is analog. I basically have to rework my code to allow my digital sensors to relay information to an analog servo for motion tracking.

Thanks in Advance!

Jack

If you're talking about an R/C servo, use the Servo library, not "raw" analogWrite PWM - the frequency is too high.

AWOL: If you're talking about an R/C servo, use the Servo library, not "raw" analogWrite PWM - the frequency is too high.

I thought the servo library was for digital servos ??? Does it still work for analog ones? Also, what does PWM mean? I've not come across this yet.

Thanks again,

Jack

AnalogWrite is NOT an Analog function it sets the duty cycle on the PWM outputs and applies to some of the digital pins.

AnalogRead() reads a value from the ADC and allows you to read the voltage on the analog pins.

You can also use the analog pins for dgital I/O but not for PWM.

Mark

You can also use the analog pins for dgital I/O but not for PWM.

...apart from the PWM generated by the Servo library.

I thought the servo library was for digital servos ??? Does it still work for analog ones?

It makes no difference whether the RC servos are digital or analogue, they still respond the same to signals output by the servo library just as they do when they are used with an RC receiver.

It's strange how the understanding has morphed; when digital R/C became common over thirty years ago, in the UK at least, they were called "digi-prop" for "digital-proportional".

Hi Guys!

Right, I’ve tried to write some code to try and make some progress and its not working, obviously because I don’t fully understand how I should be doing this. Here’s the code:

// setup libraries
#include <Servo.h>;
Servo myServo;

//declare pins
int servoPin = 11;
int val = 0;
int val2 = 0;

void setup() {
  //Attach the Servo to that pin
 myServo.attach(servoPin);
}

void loop(){
//Set Servo's position to 90°
val = 90;
//Map that to analog value
val2 = map(val, 0, 180, 0, 1024);
//Write it to the Servo
myServo.write(val);

}

However, my servo is just flinging itself to the most cw position it can which is all its been doing with all the previous code I’ve tried. I’ve even tried swapping out the ‘myServo.write’ line for an analogWrite but it kept throwing errors. Could someone suggest a way to fix this?

Thanks in advance!

Jack

void loop(){
//Set Servo's position to 90°
val = 90;
//Map that to analog value
val2 = map(val, 0, 180, 0, 1024);
//Write it to the Servo
myServo.write(val);

write accepts a value between 0 and 180, and you are writing 512 to it. Why?

I can see what the code is doing, but what are you expecting it to do?

Arrch: write accepts a value between 0 and 180, and you are writing 512 to it. Why?

I can see what the code is doing, but what are you expecting it to do?

Because the servo needs an analog input, not digital. The 0 to 180 worked on my old servos, but I can't get the new ones to work so tried to map the value so that it has the span of an analog range. Obviously this didn't work as its still digital data and not analog voltage. This is where I'm really stuck :sweat_smile:

Thanks again!

Jack

dantedraven13: Because the servo needs an analog input, not digital. The 0 to 180 worked on my old servos, but I can't get the new ones to work so tried to map the value so that it has the span of an analog range. Obviously this didn't work as its still digital data and not analog voltage. This is where I'm really stuck :sweat_smile:

If you're talking about setting an pin to output a specific voltage between 0 and 5V, the Arduino can't do that without the aide from additional circuitry. Have you tried one of the simple Servo examples on this new servo?

Arrch: Have you tried one of the simple Servo examples on this new servo?

Yes, I tried the servo sweep example and it just turned to its most CW possition =(

I really need to get this to work as its for a university project. What is analogWrite supposed to be used for then? I'm getting really muddled with all this code.

Thanks again!!

Jack

dantedraven13: What is analogWrite supposed to be used for then?

Pule Width Modulation (PWM), which is a digital signal. Though I've never tried it, I'm under the impression that digital-to-analog conversion (DAC) can be acheived with PWM and a low pass filter. Probably plenty of information on that through Google.

Yes, I tried the servo sweep example and it just turned to its most CW possition

How was it wired?

Because the servo needs an analog input, not digital.

Have you got a link, picture or detailed description of your servo?

Hi Arrch and AWOL,

Arrch, is this the kind of thing I’m looking for?

And can you buy them or is it a make only thing?

AWOL, the servo is a futaba s3306 24kg servo with it wired to a 6v circuit and then to pin 11 to get the pwm signal.

Thanks again!

Jack