 # 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?

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

void setup()
{
Serial.begin(115200);
ADMUX |= (0 & 0x07);  //A0
**ADMUX |= (0 & 0x04);    // A3**
**  ADMUX |= (0 & 0x01);  // A6**
**  ADMUX |= (0 & 0x02);  //A5**

}

{

}

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

for(int i=0; i<nbData; i++){

}
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:-
would set bit 2 to a 1, if this is followed by
then bit 2 and bit zero would be set , if this is followed by
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

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.

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