pulseIn retuns zero every second time called

I have a weather station and have been having some issues with my mh-z19 co2 sensor returning bad values. I actually have two different problems:

  1. every second call the pulseIn line returns 0, when it doesn't return 0 the reading is exactly what it should be. I have tried different wiring configs with a pull down and with logic level conversion to 5v (the sensor outputs 3.3v) and neither worked (this is on a pro micro).

  2. when i try the code on a M0, the value is roughly half what it should be, does the sam21 processor time pulseIn different? i tried a few different pins to make sure I wasn't using a reserved pin, but had the same result.

here is my basic testing code

//MH-Z19 CO2 Sesnor Setup
#define mhz19_SENSOR_PIN 6 //pwm
unsigned long co2ppm = 0;

void getco2ppm () {
  float duration;
  //pulseIn(pin, value, timeout)
  duration = pulseIn(mhz19_SENSOR_PIN, HIGH, 1004000);
  duration = duration / 1000;
  Serial.print(F("Duration: ")); Serial.println(duration);
  co2ppm = 5000 * (duration - 2) / (duration + (1004 - duration) - 4);
}

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

  //co2 sensor pin init
  pinMode(mhz19_SENSOR_PIN, INPUT);
}

void loop() {
  getco2ppm();
  Serial.print("CO2 (PWM): "); Serial.print(co2ppm); Serial.println(" ppm");
  delay (2000);
}

when you do pulseIn(mhz19_SENSOR_PIN, HIGH, 1004000);it waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing and returns the length of the pulse in microseconds. it will also give up and returns 0 if no pulse starts within a specified time out.

==> are you sure you'll see every time a LOW to HIGH transition within the 1.004 second you listed ?

J-M-L:
==> are you sure you'll see every time a LOW to HIGH transition within the 1.004 second you listed ?

thanks! i hadn't though about the waiting time for the high, the duty cycle is 1004ms (as per the spec sheet) so i hadn't accounted for wait time. i doubled the timeout value (worse case scenario assuming it starts waiting the moment it goes low and it is a the maximum duty time) and it now is working on the 32u4.

i tried on the M0/samd21 though and still no luck. the output is still a fraction of what it should be.

Zorac:
i tried on the M0/samd21 though and still no luck. the output is still a fraction of what it should be.

how do you power your mh-z19 co2 sensor?

On a M0 you can get 7mA as maximum DC current for I/O pins versus a maximum of 40mA (which you should limit to 20mA in practice and there are other rules for total draw) on your proMini. The mh-z19 co2 sensor requires roughly 18mA so can be powered from a digital pin of the pro-Mini but not from the M0.

could that be this?

(of course you don't need the logic level shifter on the M0 as it operates at 3.3V logic as well)

J-M-L:
how do you power your mh-z19 co2 sensor?

On a M0 you can get 7mA as maximum DC current for I/O pins versus a maximum of 40mA (which you should limit to 20mA in practice) on your proMini. The mh-z19 co2 sensor requires roughly 18mA so can be powered from a digital pin of the pro-Mini but not from the M0.

could that be this?

(of course you don’t need the logic level shifter on the M0 as it operates at 3.3V logic as well)

No, i use a 3a 5v power supply to power everything seperately, the co2 sensor is not powered from the m0. currently i have a second 3.3v power supply to power the m0 as well (power in on the 3.3v pin, not Vin since I am already regulating it)

*edit
just tried using an interrupt instead of pulseIn and it works fine. would still like to get pulseIn working though as there is no need to have an interrupt continuously running.

But an interrupt takes almost no time away from your main sketch. pulseIn() is blocking so your sketch is locked up and unable to do anything else for up to 2 seconds at a time.

and GND are connected I suppose when you wire all this on your m0?

J-M-L:
and GND are connected I suppose when you wire all this on your m0?

yes, im using a common ground for everything.