I was having trouble trying to set up AREF to use an external voltage reference on a Diecimila board. I'm using version Alpha 0011. The documentation I found (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1142283743) suggests writing new values to the ADMUX register to control bits REFS0 and REFS1. However, this doesn't work in this configuration, as it looks like the analogRead() function resets these bits to use the VCC reference regardless. This appears to be a recent change in 0011 (see: http://www.avride.com/pop/?p=14):
analogReference( ); // Change analog reference voltage mode (REFS1,REFS0), but you should not use this on most current Arduino boards AREF and AVCC pin was tied together and use DEFAULT  AREF pin as reference. I think it's reserved for next version of board or some hardware modified need.
I find that if I call analogReference(EXTERNAL) the bits are cleared as desired and analogRead() no longer resets them. So should the documentation note that with the Diecimila board at least, it's now necessary to call analogReference() to change the default AREF, and writing to ADMUX directly is ineffective if analogRead() is used?
Just use analogReference(). I don't think we need to document the fact that undocumented functionality no longer works.
As it stands now though, the documentation for AREF (http://www.arduino.cc/en/Reference/AREF) points to the forum thread talking about manipulating ADMUX directly, and the "What's new on Arduino 0011 RC1" item quoted above (dated March 17, 2008) kind of gives the impression that analogReference() shouldn't be used (quote: "I think it's reserved for next version of board or some hardware modified need.")?
Whoops, I forget about that old AREF page. I changed it to redirect to the analogReference() page.
I believe analogReference() works fine on all Arduino boards (though please let me know if you have any problems). I don't really know anything about the POP-168.
I only have the one board (Diecimila) & analogReference() seems to work fine for the case where I want to get readings from an accelerometer that runs on the 3v3 source. So to get the full range of the ADC I want to simply hook up 3v3 to AREF.
If AREF is connected internally to VCC (default) and externally to 3v3 obviously there's a problem and I can report that while nothing seems to fry at least in the short term, the USB chip (which supplies the 3v3 voltage) goes out to lunch for the duration.
So analogReference() works, but still it seems like the situation is a little precarious, as while that 3v3 prototype is connected I have to remember not to upload any sketch that leaves AREF at the default (VCC). Maybe not such a great burden, but seems like it might be a better for wiring.c to leave AREF disconnected by default, and require any sketch using the ADCs to call analogReference(). But there are probably competing issues.
Excellent points. I'd like to keep things simple for the simple case (where you're just using the default 5V analog reference), but I should definitely add a warning about the case you describe. It's only a problem if you actually call analogRead() in the other sketch (the one that doesn't call analogReference()), right?
It's only a problem if you actually call analogRead() in the other sketch (the one that doesn't call analogReference()), right?
Oh yeah--you're right, I just verified that. AREF is unconnected until analogRead() is called, so it's not as precarious as I was thinking.
Here's the datasheet description of the AREF pin
ADC Voltage Reference
The reference voltage for the ADC (VREF) indicates the conversion range for the ADC. Single
ended channels that exceed VREF will result in codes close to 0x3FF. VREF can be selected as
either AVCC, internal 1.1V reference, or external AREF pin.
AVCC is connected to the ADC through a passive switch. The internal 1.1V reference is generated
from the internal bandgap reference (VBG) through an internal amplifier. In either case, the
external AREF pin is directly connected to the ADC, and the reference voltage can be made
more immune to noise by connecting a capacitor between the AREF pin and ground. VREF can
also be measured at the AREF pin with a high impedance voltmeter. Note that VREF is a high
impedance source, and only a capacitive load should be connected in a system.
If the user has a fixed voltage source connected to the AREF pin, the user may not use the other
reference voltage options in the application, as they will be shorted to the external voltage. If no
external voltage is applied to the AREF pin, the user may switch between AVCC and 1.1V as reference
selection. The first ADC conversion result after switching reference voltage source may
be inaccurate, and the user is advised to discard this result.
My take on this is that because the internal sources are high impedance sources that the external source will just take precedence over the internal ones - and no harm will be done. Some testing is in order but the way I read this now, we don't have to worry about connecting two things at once at all - it's just that the external source is going to govern the analog system, no matter what the software says about the internal connection.
Other opinions? Maybe a question to the Atmel engineers is in order.
I revised the warning on the AREF page, but it's actually incorrect if my reading above is true because any resistance would cause the internal resistance to act as a voltage divider, changing the desired AREF voltage. If we get this settled maybe deleting the warning is the best idea.
Putting a 1K resistor in there seems like a good idea. Can anyone try it out and see if it helps? (That is, doesn't cause a short when you use the wrong analogReference() setting, and also gives the correct results.)
On the other hand, this sentence seems pretty unambiguous: "If the user has a fixed voltage source connected to the AREF pin, the user may not use the other reference voltage options in the application, as they will be shorted to the external voltage. " Is it a problem to have, say, your external analog reference voltage, of, say, 2.5V connected to 5V through a 1K resistor?
OK some experiments with the AREF pin have yielded the following results.
The internal connection to the AVCC pin, when using the DEFAULT setting, appears to be a low impedance connection, and the 5K pot I connected to the pin appeared to make very small differences on ADC readings. So in this sense, low impedance connections of other voltages to the AREF pin might very well do some damage, and should be avoided. Hence the recommendation of connection through a 5k resistor.
This value was picked by shorting ground to AREF pin while using the DEFAULT setting - with an ACD reading of 1014 - it changes the value only 1 - to 1015. I also figured an extra mA of current though most circuits was not going to harm anything. Probably 10K would be just as good a recommendation and might tend to be in more students' boxes of parts, since we recommend that value for pullup/pulldown resistors. (Oops - just checked that resistor an it turned out to be 47K not 4.7K - more later on best value)
However the 1.1 volt source is a different story. I tried measuring the voltage on the AREF pin (with nothing connected externally) and my digital meter failed completely, however I succeeded in reading the voltage with a jfet input analog meter. When I connected the 5K pot to the AREF pin, it just completely overrode the internal reference and governed the ADC readings completely. So this setting doesn't really require the protection of the resistor but it does require explaining why it won't work with anything connected externally to the AREF pin.
I took a shot at updating the docs. Comments and critiques welcome. Look for them to get moved to someplace more obscure really soon