Pages: [1] 2   Go Down
Author Topic: using the aref pin..  (Read 7044 times)
0 Members and 1 Guest are viewing this topic.
Bremen, Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?



« Last Edit: March 13, 2006, 04:02:47 pm by kr1 » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Daniel
Guest
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
     // 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



« Last Edit: March 03, 2007, 09:49:05 pm by Daniel » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Daniel. It looks like in order to connect 3V to the AREF pin I should change the code to:

Code:
     
      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!
« Last Edit: March 04, 2007, 01:47:50 am by vorb » Logged

Daniel
Guest
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: March 04, 2007, 03:53:06 am by Daniel » Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
« Last Edit: March 04, 2007, 06:07:29 am by vorb » Logged

Netherlands
Offline Offline
Sr. Member
****
Karma: 0
Posts: 414
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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   :smiley

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...
« Last Edit: November 05, 2007, 08:10:53 am by jds » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 37
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Greenwood, Indiana
Offline Offline
God Member
*****
Karma: 0
Posts: 508
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If you want higher use an opamp.
or a voltage divider (two resistors).

-j

Logged

pittsburgh, pa
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?)
Logged

France
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Mr. Watson. Come Here. I need you.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

France
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Mr. Watson. Come Here. I need you.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Your explanation is clear and precise.

Phil
Logged

Pages: [1] 2   Go Up
Jump to: