Aref question using one arduino, and two arduinos

Hello everyone,

I have two questions regarding the use of the Aref pin and analog sensors.

Lets say I have a 3.3V accelerometer that I have hooked up to the analog inputs. I also have it powered by the Arduino Uno board 3.3V out, and grounded to the Arduino Uno ground pins. Do I need to connect the 3.3V pin to the Aref pin? Or because my power source is the Arduino board, I don't need to use aref at all?

Now, lets assume I want to use two Arduinos and use the one accelerometer for both. Arduino 1 is hooked up in the same manner as the first scenario above. Arduino 2 needs to get the same readings that Arduino 1 is seeing. Do I need to connect Arduino 1's 3.3V pin to Arduino 2's Aref pin and then use the external reference? Is this correct? Should I connect the ground pins? Also, in my code, should I put analogReference(External) before EACH of the analogRead(xvalue), analogRead(yvalue) etc, or once at the top of my code suffice?

I guess it turned out to be more than two questions! Thanks for the help guys!

Lets say I have a 3.3V accelerometer that I have hooked up to the analog inputs. I also have it powered by the Arduino Uno board 3.3V out, and grounded to the Arduino Uno ground pins. Do I need to connect the 3.3V pin to the Aref pin? Or because my power source is the Arduino board, I don't need to use aref at all?

Without using aref the ADC maps 0..5 Volt => 0..1023 . If you use 3.3 volt at max you will not use the full possible resolution (~66%) . But if the range suits your need, its OK. If you need max resolution you can connect AREF to 3.3V.

As Far as I know: if you connect the sensor to both Arduinos you will get slightly different readings per Arduino. You need to connect at least all grounds. And if you want full range (see above) you need to connect 3.3V to both Arduino's AREF pin. Personally I would start just with the 5V range to get a feeling of the behaviour of the sensor.

Thanks for the response, that makes sense.

Only question left, is do I need to use the analogReference(external); before every line that I check the analog pins, or just once at the top of my code?

99% of the time just once, typically in setup()

However if you want A0 use 5V and A1 use 3,3V and A2 1.1V then your program logic must deal with it and you should apply an appropiate delay (few millis I guess) so the internals can "adapt".

Hello Everybody,
I do not completely understand the use of AREF. Is there a way to know the voltage fed to AREF inside our sketch? When we issue the command analogReference(EXTERNAL) what is it that takes place? We feed a particular voltage to the pin that has been measured by us with a multimeter or not?
Let's say that the ref voltage is 5v, as you say the ADC converts the 0-5v to 0-1023. When it is 3.3v the ADC converts 0-3.3v to 0-1023 and so on. But if we have to measure manually the voltage of our rail what's the use of feeding AREF pin? Regards,

Is there a way to know the voltage fed to AREF inside our sketch?

No. Values reported by the analogRead function are the measured voltage relative to the reference voltage. If you tried to measure the reference voltage, you'd get 1023 out of the analogRead function.

When we issue the command analogReference(EXTERNAL) what is it that takes place?

The Arduino is told to use a different reference voltage to compare the measured voltage to.

But if we have to measure manually the voltage of our rail what's the use of feeding AREF pin?

If you know that the range of voltages to be measured will always be 0 to 2.0 volts, using 5.0 as the reference voltage means that only 40% of the scale from 0 to 1023 will be reported. If you use, instead, a 2.0 volt reference value, the full range of values will be returned, and it will be easier to see small changes in voltage.

Is there a way to know the voltage fed to AREF inside our sketch?

Not directly, but if one was using the Aref pin as an external voltage source, but for some reason not sure what value was, if one had a known fixed voltage wired to one of the analog pins, say the 3.3vdc from the shield connector, then one could 'back' calculated the Aref voltage by the value of the known voltage wired to the analog pin. Kind of weird but doable. I in the past wrote a sketch that could 'measure' the actually voltage being supplied to the AVR's Vcc and Avcc pins by using the internal constant band-gap voltage as a measurement value rather then a reference voltage and therefore obtained the needed values to 'back' calculate the chips Vcc voltage. Handy for applications where you are powering the chip directly with battery voltage where the normal 'reference' voltage used by the A/D would slowly change as the battery discharges.

When we issue the command analogReference(EXTERNAL) what is it that takes place?

That depends on what your application is doing with the A/D functions. If your sketch is always going to utilize the same external referece the the command only needs to be made once, usually in the setup function. However if you are changing the reference used for measuring different analog pins then you need to issue a new analogReference statement prior to reading a pin that required a different reference from the other analog pins. That can be tricky for a couple of reasons. First that when changing analog references the first reading of a analog pin tends to be inaccurate due to mux switching of the reference voltage. One needs a few 'dummy' reads to get a stable and more accurate reading.

The second caution is a little complex as explaned below from the arduino reference on using analogReference with the Aref pin. One can cause damage to the chip if there is a external voltage wired to the Aref pin but switch to the internal voltage source which could cause high current to flow into the Aref pin. So advice on using real external voltages wired to the Aref pins is to be sure you understand the following safety points:

Warning
Don't use anything less than 0V or more than 5V for external reference voltage on the AREF pin! If you're using an external reference on the AREF pin, you must set the analog reference to EXTERNAL before calling analogRead(). Otherwise, you will short together the active reference voltage (internally generated) and the AREF pin, possibly damaging the microcontroller on your Arduino board.

Alternatively, you can connect the external reference voltage to the AREF pin through a 5K resistor, allowing you to switch between external and internal reference voltages. 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.5 * 32 / (32 + 5) = ~2.2V at the AREF pin.