Go Down

Topic: using the aref pin.. (Read 7 times) previous topic - next topic

Kristian Gohlke

Mar 13, 2006, 10:02 pm Last Edit: Mar 13, 2006, 10:02 pm by kr1 Reason: 1
Hej,

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?




vorb

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.

Daniel

#2
Mar 04, 2007, 03:44 am Last Edit: Mar 04, 2007, 03:49 am by Daniel Reason: 1
Hi

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.

Code: [Select]
     // 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.


Quote

ADC Multiplexer Selection
Register - ADMUX
ADC Multiplexer Selection

Bit 7 6 5 4 3 2 1 0
REFS1 REFS0 ADLAR - MUX3 MUX2 MUX1 MUX0 ADMUX
Read/Write R/W R/W R/W R R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0

? 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




vorb

#3
Mar 04, 2007, 04:21 am Last Edit: Mar 04, 2007, 07:47 am by vorb Reason: 1
Thank you Daniel. It looks like in order to connect 3V to the AREF pin I should change the code to:

Code: [Select]
     
     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.

Thanks again Daniel!

Daniel

#4
Mar 04, 2007, 09:52 am Last Edit: Mar 04, 2007, 09:53 am by Daniel Reason: 1
no sweat!


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.

D

mellis

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

#6
Mar 04, 2007, 11:53 am Last Edit: Mar 04, 2007, 12:07 pm by vorb Reason: 1
Quote
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?

-Edit-

I added the following definitions and it WORKS!  8-) :
Code: [Select]

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif


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.

CrashingDutchman

#7
Nov 05, 2007, 09:57 am Last Edit: Nov 05, 2007, 02:10 pm by jds Reason: 1
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...

hans12000

is it possible to use a aref, that is higher than 5V? for example 7V?

mrmeval

No, max is VCC

http://www.atmel.com/dyn/resources/prod_documents/doc2545.pdf

If you want higher use an opamp.

Quote
is it possible to use a aref, that is higher than 5V? for example 7V?

If it was designed by man it can be repaired by man.

kg4wsv

Quote
If you want higher use an opamp.

or a voltage divider (two resistors).

-j


jet

Looking at the schematic for the MINI 03, it appears that the AREF pin is unavailable.    Is this correct?  (Should I be asking in the MINI section?)

fatphil

Quote
Quote
If you want higher use an opamp.

or a voltage divider (two resistors).

-j



According to Wikipedia :
Quote
Any ratio between 0 and 1 is possible. That is, using resistors alone it is not possible to either reverse the voltage or increase Vout above Vin

http://en.wikipedia.org/wiki/Voltage_divider

So how would a two resistor voltage divider increase the output voltage above the voltage at AREF?  I don't get it.

kg4wsv

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.

-j


fatphil

Aha!  thank you.  I got lost in the Wikipedia description!

Your explanation is clear and precise.

Phil

Go Up