I am looking for information on using the Analog inputs and specifically the AREF pin. I have done a lot of searching on various forums on using the AREF pin and all I can find are bits and pieces, but nothing specific on using that pin.
Most forums talk as if using the AREF is top secret information, others simply imply that you must avoid it as the plague, but most don't give any tangible info.
I know you must tell the controller to use it in the programing, but what are the specifics and syntax for doing so.
I know that the reference voltage must be stable, but how stable? For example, If I want to use a 4 volt reference, must it be 4.000 volts or will 3.9 to 4.1 be OK?
Does anyone have any real information and or examples on using the AREF pin?
I have done a lot of searching on various forums on using the AREF pin and all I can find are bits and pieces, but nothing specific on using that pin.
There's nothing magical nor mysterious about it. The Aref pin provides an alternate voltage reference for the A-D converter. The default reference voltage is the processor's voltage, AVcc.
You can set the reference by calling analogReference(), supplying a value indicating the desired reference. This must be done prior to calling analogRead() and the specified value remains in effect for all future calls to analogRead().
For the mega168-based Arduino's, the reference values are:
0 - The voltage applied to the Aref pin.
1 - The voltage level of AVcc.
3 - The internal 1.1 volt reference.
Note that the value 2 is reserved and should not be used.
You should note that a "full scale" A-D reading (1023) indicates that the analog input voltage is greater than or equal to the analog reference voltage.
For further details, consult the Atmel datasheet for the AVR chip on your Arduing and review the code in wiring_analog.c.
Ok, now that I think I finally found the proper data sheet for the 168,
I see were it says the internal ref is 1.1.
Looking at the analogReference() function in the ArduinoReferance, my options are DEFAULT, INTERNAL, and EXTERNAL. I assume these are constants, so when useing an external ref I would state in the code,
I think there's an additional restriction that Vref can't be higher than Vcc.
"Odd" voltages are OK; there are voltage references of e.g. 4.096V or 1.024V available, so that the binary ADC steps will work out to an intregral number of mV per step.
My reference voltage will be 4.0 volts or possibly 3.5 not sure which I will need yet,
Another question is can the AREF be switched between DEFAULT and EXTERNAL?
This would most likely be done at power up and not dynamically, for example, If I want the DEFAULT ref voltage I could use a dip switch connected to a digital input to tell the program to use the default if set to HIGH, otherwise use the external.
You could switch the reference voltage but you may not need to change it. Why not use the default 5 volt reference and scale the values in software. You can use the arduino map function to scale the analogRead values to an arbitrary range. For example when measuring using a 5 volt reference you would do something like this:
int val = analogRead(pin);
int mv = map(val, 0, 1023, 0, 5000);
mv will have a value equal to the number of millivolts on the analog pin. This value should be accurate to something around 5 or 10 mv.
You would get slightly more accuracy using a lower refrence but it may not be worth the trouble.
The 5v default will normally be ok to use, but on occasion I need a bit more accuracy, useually the voltage will go between 0.4 to about 4.0 volts, but on some units it will only go as high as 1.7volts. In that cas eI would like to change the ref to a lower value. Will the map work for that as well?
Map will scale any values. But because the analog inputs on the arduino have a range up to 1024, if you want to get the maximum precision you may want to choose your reference voltage to simplify the scaling.
For example if you set the reference for 4.096 volts, each increment of analogRead will be 4 milivolts.
Setting the reference for 2.048 volts would give an increment of 2 millivolts.
Bear in mind that the Arduino analog to digital converter is only accurate to plus/minus two counts and the error will be increased by any variation in the voltage of your reference. If an accuracy of 1% is acceptable for you application, you can keep things simple and stick to the default 5v reference.
If you need greater than 1% accuracy, what are you planning to use to provide the reference voltage?
I think the 1% will be fine.
I think I will start with useing the default and see how that works out for me. It might be easier to simply change sensors to get more accuracy. Basically I was trying to make one sensor work for all applications, but I can desing the board to accept different sensor types and footprints and simply swap out the sensors.
If I find that I do need to use the AREF, what do you suggest I use for a stable 2.048v?
Thanks for all you help btw, some other forums I joined, no one seemed to want to help. I'm still learning but not a total nobe.
You could use a resistor voltage divider on the 5v line
That brings up another question, and the data sheet might tell me this, but unable to look atm. Is there an internal load resistance that will effect the values o f the Voltage divider as described here, Voltage Divider
I seem to recall reading that the input pin is a very high impedance.
The load on the analog inputs should be 20k ohms or less.
I haven't checked what the load can be on the Vref should be but the info should be in the datasheet. I do recall that it is recommended to use a 5k resistor to connect an external reference to the Vref pin.