Go Down

Topic: is there a way to print aref value? (Read 979 times) previous topic - next topic

tbarbe


Is there a way to show / print - what the Arduino is seeing on AREF input pin?  Id like to confirm what voltage I think Im sending in there...

? anyone ?


Peter_n

ATmega328p:
analog MUX can be set to one of the analog channels, or 1.1V or GND.
reference can be set to AVcc (5V), 1.1V, AREF.

It means you can measure the 1.1V, with AREF as reference. So AREF has to be between 1.1V and 5V.
The 1.1V is not very accurate, but you can calculate what the AREF is.

Do you know that when the external reference is used, you can not use analogRead before you select the external reference ?

tbarbe

Hi

I meant that I have a voltage going into AREF and using (EXTERNAL) as the command...

what I wanted to see was what Arduino thinks is now coming into AREF... like any other analog input - can I read the value and spit it out to see if it matches what I think Im feeding it?

:)

cheers and thanks for replying!

tbarbe

and yes thank you - I was setting external in the setup... and then only after was I trying to read anything else..

I just want to see what AREF is seeing on its input pin... I know what voltage Im feeding it... I just want to see what it thinks its getting... :)

Peter_n

I wrote in my previous post what is possible. You can read AREF against the 1.1V, but only if AREF is between 1.1V and 5V.

tbarbe

Oh Im sorry - I didn't understand... my apologies.

I see what you are saying now .. yes - I am not feeding it anything beyond the limits of 1.1 or 5.0... Im in between actually Im sending it 2.5V...

how does one get the AREF value then?   do I just do analogRead(AREF) ?

that was my question really... :)  thanks again for responding... much appreciated.

Peter_n

You don't have to apologize, it's okay. I was writing about using the registers in the chip, because reading AREF is not in the Arduino libraries.
Actually, what I wrote about is not reading AREF, it is reading the internal 1.1V, and using AREF as a reference. You need to calculate what the value of AREF would be.

You have to read the datasheet of the ATmega328p, and read about the MUX and reference.
And after that, try to set the ATmega328p into normal mode, so the Arduino can read the analog inputs again. Returning to normal operation is the hardest part.

There are some examples to read VCC (the 5V), by reading the 1.1V and using the VCC as reference. That is very close to reading AREF.
You can try to search for : Arduino ReadVcc
Or search for : arduino secret voltmeter
That code should give you an idea how it is done.


tbarbe

:)

Ok thank you... I started poking around the Atmel site and now I realize I have a lot of reading and learning to do... all fun anyhow... thanks for heading me in the right direction!

Peter_n

#8
Sep 20, 2014, 02:45 pm Last Edit: Sep 20, 2014, 02:51 pm by Peter_n Reason: 1
The next sketch works for an Arduino Uno.
Which Arduino board are you using ?

You can safely use it, the dangerous part is to supply a voltage to the AREF pin.

There is one thing I'm not sure of: returning to normal.
Can you measure a precise voltage with a normal analog pin, in a sketch that also calls readAref() and without any call to readAref().
I hope the accuracy is still the same, I'm 99% sure of it. I only had troubles with the Arduino Mega 2560 board to return to normal.


Code: [Select]


// Read the analog value of an external voltage at the AREF pin.
//
// Warning: Do no use this sketch !
//
// This sketch is when an external voltage is supplied to the AREF pin.
// That is a very rare situation.
//
// Warning: using an external voltage at AREF might cause an
// internal shortcut when analogRead() is called before the
// reference is set to external.
//

void setup() {
  Serial.begin (9600);
  Serial.println ("\nRead voltage at AREF pin");

  analogReference (EXTERNAL);
}

void loop() {
  float voltage = readAref();
  Serial.println(voltage); 
  delay(1000);
}


//
// Function readAref
//
// Reads AREF (when external voltage is supplied).
// When the AREF pin is open, a value of 1.1V is returned.
// This function is only valid for a voltage at AREF of 1.1 to 5V.
//
// The calculations can be translated for integers to prevent
// use of float.
// Only for the Arduino Uno, Nano, Pro Micro at this moment.
// Experimental, no guarantees.
// public domain
//
float readAref (void) {
  float volt;

#if defined (__AVR_ATmega8__)
#elif defined (__AVR_ATmega168__)
#elif defined (__AVR_ATmega168A__)
#elif defined (__AVR_ATmega168P__)
#elif defined (__AVR_ATmega328__)
#elif defined (__AVR_ATmega328P__)

  // set reference to AREF, and mux to read the internal 1.1V
  // REFS1 = 0, REFS0 = 0, MUX3..0 = 1110
  ADMUX = _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
 
  // Enable the ADC
  ADCSRA |= _BV(ADEN);

  // Wait for voltage to become stable after changing the mux.
  delay(20);

  // Start ADC
  ADCSRA |= _BV(ADSC);

  // wait for the ADC to finish
  while (bit_is_set(ADCSRA,ADSC));

  // Read the ADC result
  // The 16-bit ADC register is 'ADC' or 'ADCW'
  unsigned int raw = ADCW;

  // Calculate the Aref.
  volt = 1.1 / (float) raw * 1024.0;

#elif defined (__AVR_ATmega32U4__)
#elif defined (__AVR_ATmega1280__) || defined (__AVR_ATmega2560__)
#endif


  // Try to return to normal.
  analogReference(EXTERNAL);
  analogRead(A0);            // the mux is set, throw away ADC value
  delay(20);                 // wait for voltages to become stable

  return volt;
}

tbarbe

Wow! Thank you... I will look thru this and see what I can learn... Very kind of you - cheers!

:)

Btw I am on an UNO for now :)

Go Up