Multi Inputs Analog using ADMUX

Hello everyone,
I'm newbie in setting of the registers.
I use MEGA2560, I wrote this code as follows for one analog sensor, I want to add 3 others Analogs Sensors like that :
ADMUX |= (0 & 0x04); // A3
ADMUX |= (0 & 0x01); // A6
ADMUX |= (0 & 0x02); //A5

But I'm confused how I can send the values of these sensors separately (successively or together) to serial print.

For rephrasing my question :
I used the value stored in ADCH/ADCL register for showing the value of one sensor, but for many ones how I can do?
Thanks for your help

unsigned long t, t0;
volatile int nbData=300;
int data[300];


void setup()
{
  Serial.begin(115200);
  ADCSRA = 0;             
  ADCSRB = 0;             
  ADMUX |= (0 & 0x07);  //A0
  **ADMUX |= (0 & 0x04);    // A3**
**  ADMUX |= (0 & 0x01);  // A6**
**  ADMUX |= (0 & 0x02);  //A5**
  
  ADMUX |= (1 << REFS0);  
  ADMUX |= (1 << ADLAR);  
 
             
  
  ADCSRA |= (1 << ADATE); 
  ADCSRA |= (1 << ADIE);  
  ADCSRA |= (1 << ADEN);  
  ADCSRA |= (1 << ADSC); 


}

ISR(ADC_vect)
{
 byte x = ADCH;  

}
 
void loop()
{
  noInterrupts();
  nbData; 
  interrupts();


  for(int i=0; i<nbData; i++){
        
      data[i]=  ADCH;
      
  }
  t = micros()-t0; 
  
  SendToSerial();
}

void SendToSerial()
{
    
     for (int x = 0 ; x <nbData; x++){
          Serial.println(data[x]);
     }

}

The Mega and most other Arduinos only have one A/D converter, so you can not set these all at once only one will be switched in.
The code
ADMUX |= (0 & 0x04); // A3
ADMUX |= (0 & 0x01); // A6
ADMUX |= (0 & 0x02); //A5
Only uses the logical or operations so that when you set them one after another the logic 1's in the ADMUX register will accumulate. So suppose ADMUX contained a value of zero, then:-
ADMUX |= (0 & 0x04);
would set bit 2 to a 1, if this is followed by
ADMUX |= (0 & 0x01);
then bit 2 and bit zero would be set , if this is followed by
ADMUX |= (0 & 0x02);
then bit 2, 1, and zero would be set
When doing this sort of thing you must use the logical AND operation to clear out (set to zero) all the mux address lines to zero than put the value you want with the OR.

You can't.
You can only set one multiplexer address at a time, and only read back the one value from the A/D.

1 Like

Thanks Grump_Mike for your reply,
if I understand :
I can't use all sensors at a time, but we can use one after another.
When the value stored in the ADCH, I read it by serial print, after that I set this register to Zéro and restarting the operation for the next value coming from another sensor.

ADMUX :
I'm not sure to understand your example to set the bit of ADMUX for switching between the values, please can you detailed this point ?
Thank you

Correct.

No need to do that it gets replaced by the next conversion.

The OR operation sets selected bits
The AND operation clears selected bits
The XOR operation inverts selected bits

See reply #8 in this thread for a full explanation
https://forum.arduino.cc/t/analog-reading-whit-registers/922403/7

1 Like

Thank you Grumpy_Mike.
I will See your reply #8
https://forum.arduino.cc/t/analog-reading-whit-registers/922403/7