Why I can't change analogReference

I have a project with ATMega328P-AU internal 8mhz. I want to power it with 3V battery and read the value of temp with TMP36. I have a problem when the reading of the sensor changing because of battery was draining so battery voltage also changing. I want to use internal reference for ADC so the reading still the same even battery voltage was changing. But, after using analogReference(INTERNAL), I still get the same value and the value of the sensor still changing depends on battery voltage. Do you guys know why this is happening??

We can make some guesses. But we won't need to when you post your schematic and code. Please read the forum guide first.

Welcome to the forum

Your topic was MOVED to its current forum category as it is more suitable than the original

Please post a full sketch that illustrates the problem


Here's the shematic for it, and for the code just like this:

#include<avr/power.h>
void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL);
  power_twi_disable();
  power_timer1_disable();
  power_timer2_disable();
  power_spi_disable();

}

void loop() {
  float value = analogRead (A0);
  Serial.println(value);
  float tempC = 0.4088*value-75.934;
  Serial.println(tempC);
  delay(1000);

}

Try disconnecting AREF.

Ohh thanks, that works.

1 Like

For best results, connect a 0.1uF cap from AREF to GND, put the cap close to the chip's pins.

Using analogReference(INTERNAL) does not disconnect the AREF pin, it just connects the internal reference voltage to the AREF pin. You should be able to measure the internal voltage ref by connecting your multimeter between AREF and GND, I think. But if you connect AREF to 3V or 5V, that will be used as the reference voltage.

If you select INTERNAL or DEFAULT and apply a voltage to AREF (the battery) and then call analogRead(), then you have a shortcut inside the ATmega328P.

[EDIT] Correction: If you use INTERNAL or DEFAULT it can go wrong. Since DEFAULT is the default, just calling analogRead() causes the shortcut !

short circuit ?

Yes, the internal voltage reference is also at the AREF pin.
The DEFAULT internal reference connects to VCC and it can output enough current via the AREF pin to turn a led on. When a INTERNAL voltage reference is selected, then it is weaker.

I noticed a battery at AREF, that means only the EXTERNAL mode is valid.

There is a warning on the Arduino AnalogReference() page: "If you’re using an external reference on the AREF pin, you must set the analog reference to EXTERNAL before calling analogRead()".

Manufacturer's page of the ATmega328P: https://www.microchip.com/en-us/product/ATmega328P
In the datasheet it says: "If the user has a fixed voltage source connected to the AREF pin, the user may not use the other reference voltage options in the application, as they will be shorted to the external voltage".

Was your question about using the English language ? In Dutch the word "kortsluiting" is very close to "shortcut" and I think it is sometimes allowed to indicate a short circuit. The term "short circuit" is a circuit that is not long :rofl:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.