Analog Reference is not properly set in AMUX

When i write a small code, to enable the internal AREF and check it with my DMM, it works:

void setup() 
{
  Serial.begin(9600);

  analogReference(INTERNAL);  // Interne ADC Referenz einschalte
  
  analogRead(A5);
  Serial.println(ADMUX, BIN);
}

The Value in ADMUX is 0b11000101 according to the serial Output, exactly what i expect.

But when i integrate this to my Code, the selection of the AnalogReference has no affect, means AREF is 5V from USB and the Value in ADMUX is 0b01000101 after reading A5.

void setup()
{
#if(EXTENDED_DEBUG)
  bDebug = true;
#endif

  SerialCom.begin(BAUDRATE);
  SerialDebug.begin(BAUDRATE);
  
  if(bDebug)
  { 
    SerialDebug.println("HALLO WELT!");
  }
  Serial.println(ADMUX, BIN);
  analogReference(INTERNAL);  // ADC Referenz einschalten

  Serial.println(ADMUX, BIN);
  ADMUX = 0b11000000;

  Serial.println(ADMUX, BIN);
  
  analogRead(A5);
  Serial.println(ADMUX, BIN);
  ......

The Serial Monitor sends me the Following:

HALLO WELT!
0
0
11000000
1000101

This shows me, that the Reference selection must be stored in a Global Variable by the Arduino Library Function and gets set in the same register modification with the analog channel selection, and my entry the ADMUX-Register gets owerwritten by analogRead.

How can i check what part of my code causes, that the Reference Selection is not properly stored?

We do not know what SerialCom and SerialDebug are. Hint: post the complete code (or a representative example) and tell us which board you are using.

analogReference() just stores the value in a variable
ADMUX is not written untill you do a read.
The default value for ADMUX is 0b00000000

===> 0b000x0000

SerialCom is defined as Serial, same as SerialDebug, these are used for the Communication over serial that follows later in code.
They are different, that i can change the Serial Port in only one define, when i want my debugging Informations on Serial2 of a Arduno Mega.

->this does not affect the behaviour

If you are using Arduino UNO, then it is NOT AMUX in your thread title; it is ADMUX.

I found my error, i used the Keyword INTERNAL for somthing else and redefined it in my Globals.h, sadly the IDE does not show a error, when i redefine something that is already defined by the Library.

In my Globals.h was written
#define INTERNAL 1

now i use another Keyword for my define and it works

i use instead
#define USB 1

#define DEFAULT 0

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