i´m trying to figure out how the aref pin can be used.
this is what i know so far: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1139161553/3#3
massimo says: "switching reference voltage requires adding a few lines of C code to your arduino code."
i wonder what these lines of code might be, is the whole aref thing documented in some place?
Did you ever figure out how to use the AREF pin? I am trying to change it from 5V to 3V but have not been able to find any reference information on how to do this.
the really technical answer to this is in the Atmega datasheet, pages 205-206. If you look through the associated Arduino C files, you wil probably find the statement that configures the Atemga8 for an internal reference... then you can change it however you like.
Look for "ADMUX" in the C code.
The software side of things is really not my strong point, but looking in wiring.c I see the following lines, which seem to match what is stated in the manual.
// set a2d reference to AVCC (5 volts)
cbi(ADMUX, REFS1);
sbi(ADMUX, REFS0);
and here is the manual-- look at the last part, table 74.
? Bit 7:6 – REFS1:0: Reference Selection Bits
These bits select the voltage reference for the ADC, as shown in Table 74. If these bits
are changed during a conversion, the change will not go in effect until this conversion is
complete (ADIF in ADCSRA is set). The internal voltage reference options may not be
used if an external reference voltage is being applied to the AREF pin.
Table 74. Voltage Reference Selections for ADC
REFS1 REFS0 Voltage Reference Selection
0 0 AREF, Internal Vref turned off
0 1 AVCC with external capacitor at AREF pin
1 0 Reserved
1 1 Internal 2.56V Voltage Reference with external capacitor at AREF pin
Thank you Daniel. It looks like in order to connect 3V to the AREF pin I should change the code to:
cbi(ADMUX, REFS1); //clear REFS1 bit (0)
cbi(ADMUX, REFS0); //clear REFS0 bit (0)
Edit: I just changed lines 607 & 608 to the code above in arduino-0007\lib\targets\arduino\wiring.c and it worked! Now I can set the ADC reference voltage from my external supply of 3V instead of using 5V.
if you have time it is always good to put what you figured out into a small wiki page in the 'playground'. This means that later down the line people will be able to benefit form your discoveries.
vorb: in case you'd rather not mess your wiring.c file, you should be able to put those lines in your setup() function and get the same effect. It might make it easier to share your code or upgrade to future Arduino versions.
vorb: in case you'd rather not mess your wiring.c file, you should be able to put those lines in your setup() function and get the same effect. It might make it easier to share your code or upgrade to future Arduino versions.
I just tried that and I got the following error: 'cbi' was not declared in this scope In function 'void loop()'.
So I guess I need to add the definitions for cbi and sbi from the beginning of wiring.c to my code?
Thanks Mellis!
I will take pictures and put my code in the playground once I figure out how to convert my adc values into "g" values. If you guys could take a look at my thread in the programs forum I would appreciate it. I'm stuck trying to deal with the fractional calculations I need to do.
Sorry for digging up a rather old thread, but if I add the code mentioned above in my Sketch, AREF can be used as a 3V supply, instead of the normal 5V? It's not completely clear to me :
And if I burn a different Sketch, without the code above, it will automatically be back to 5V?
Edit: Don't have a multi-meter here, so that is why I ask...
The voltage divider circuit isn't to increase anything at AREF, it is to reduce the voltage.
For example, I hook up two resistors GND - 5k - 5k - +5V, and connect AREF to the point between the two resistors - presto, a 2.5V voltage reference.
If you are going the other direction, e.g. measuring the voltage of your car's electrical system (which is about 14.4V while the engine is running) you can put the voltage divider on the input side. GND - 5k - 15k - Vin, and connect the analog input pin between the two resistors, and you'll be measuring 1/4 of Vin. Don't forget to scale values accordingly when doing calculations based on the analog input.
Hi guys, I found this post very useful in helping me get my AREF pin working as I wanted it to. Just thought I would add a post on this little equation on what resistors to use to get the desired voltage at the AREF pin as I was unable to find something straight forward. I am no expert so please if anyone thinks it should be different let us know!
From Arduino AREF reference... "Note that the resistor will alter the voltage that gets used as the reference because there is an internal 32K resistor on the AREF pin. The two act as a voltage divider, so, for example, 2.5V applied through the resistor will yield ~2.2V at the AREF pin."
By knowing the voltage that the circuit operates on and the voltage required at AREF, this equation calculates the resistance required by the resistor connected to AREF. This is pretty basic stuff, just thought this equation may save some folks some time.
Vcc = Voltage supply, probably 5v
V = Voltage to AREF pin required
R = Resistance for resistor attached (externally) from Vcc to AREF pin
((Vcc * 32000) / V) - 32000 = R
(32000 is the 32K internal resistor on AREF)
Replace Vcc with the voltage you are applying to the resistor connected to the AREF pin, and replace V with the voltage you want at the AREF pin and hey presto! R becomes the resistance in ohms required to connect Vcc to AREF. Dont forget to divide this by 1000 to get K ohms.
Heres my application
I wanted ~2.9v on AREF for my Sharp IR sensors (for IR MIDI controller) and am running my circuit at 5v so...
((5 * 32000) / 2.9) - 32000 = 23 172.4138 ohms, which I round up to 24K ohms.
Check this is correct...
V = I * R
R = R1 + R2
R = 24K + 32K = 56K ohm
I = V / R
I = 5 / 56
I = 0.0892857143
I * R2 = V at AREF
0.0892857143 * 32 = 2.9 Volt
Hope this helps, im off to Maplin now to bag me a resistor to put into practice!
so a divider gets your measured peak voltage down below 0-5V but what if you have a car system and you want to measure the 5V range from 10V-15V? A divider gives you the whole range 0-15V mapped down to 0-5V but you only care about the top 5V( 10-15V ). A divider means that 2/3 of your range goes unused and you're using only 1/3 of your 1024 samples on what you care about.
Or apply the 10-15volt input to a op amp's + input (using +5vcc applied to a rail to rail device), apply +10 to the - input, set the gain resistors for X1, presto a 10-15v input equals a 0-5vdc output, ready to be read by the Arduino's analog input pin. Op amps are very simple to work with and are very useful devices and are also very inexpensive and still avalible in DIP, and, and, and.