Reading multiple analog inputs

Dear Forum,

I’m encountering a very strange issue. When I read multiple analog ports at the same time, the program freezes. To be more specific, if I read three then everything is fine. If I read four then the program freezes for several seconds before continuing:

//This works
int  a = analogRead(A0);
int b = analogRead(A1);
int c = analogRead(A2);
//int d = analogRead(A3);

//This freezes the program for several seconds
int  a = analogRead(A0);
int b = analogRead(A1);
int c = analogRead(A2);
int d = analogRead(A3);

I’ve tried adding timers, delays between the reads, even dummy reads but none of it works. I’m on the brink of implementing proto/hyperthreading but before I go there, I thought I’d ask around here first. Although the program freezes, the readings from in this case A3 are correct and also displayed. The freeze is causing issue in my program further down the road. The sensor connected to A3 (MH-Z14A CO2) also has a PWM output and when I use that on a digital input, there is no problem whatsoever :

int pwm_co2 = 0;
void pwm_in_interrupt() {
  static int t_last = 0;
  int t = micros();
  if (digitalRead(D5) == 0) {
    pwm_co2 = (t - t_last - 2) * 4 / 1000;
  t_last = t;

I know analogRead() is blocking so there may well be an issue with the sensor. I order to investigate that, I took the analogRead(A3) out of loop() and placed it in a timer to be called every ten seconds :

if ((unsigned long)(currentMillis - prevMilCO2) >= 10000)
      prevMilCO2 = currentMillis;
      float curCO2 = analogRead(A3) * 4.887; //5000/1023) change this if you have a different CO2 reader

and all my problems went away. No, that’s not entirely true. The problem is still there but they don’t cause issues in the rest of the program anymore. Could it be that it takes too long for the sensor to produce the value when analogRead() is called and therefore blocking the program? The datasheet is not really clear on that.


AFAIK reading an analogue port has nothing to do with sensor input. This test sketch reads four analogue inputs in 544 microseconds (Uno clone). The problem must be elswere in your code. Leo..

int a, b, c, d;
unsigned long startMicros, stopMicros;

void setup() {
  startMicros = micros();
  a = analogRead(A0);
  b = analogRead(A1);
  c = analogRead(A2);
  d = analogRead(A3);
  stopMicros = micros();
  Serial.println(stopMicros - startMicros);

void loop() {

Agreed. No relationship with the sensor (analogRead() simply reads the current value on the port), must be something else.

Please post COMPLETE sketch (copy/paste from the IDE) that has the problem.