---

Showing posts with label co2. Show all posts
Showing posts with label co2. Show all posts

Friday, October 5, 2018

Arduino Air Quality Monitor

I've recently spent some time developing the MonkMakes CO2 Sensor for micro:bit and have spent a fair amount of time researching into indoor air quality. It struck me that I actually had no idea how healthy (or otherwise) the air is, in the house that I both sleep and work in. 

Time for a project! What's more I can reuse some of the sensors that I have accumulated while researching. These are low cost sensors, the whole lot costing probably less that GBP 50 (USD 50).

The project logs the following readings through USB back to the Arduino Serial Monitor, where you can copy and paste it into a spreadsheet:
  • True CO2 level using the serial interface to the MH-Z14A CO2 sensor.
  • tVOC (Total Volatile Organic Compounds) in parts per billion (ppb) using a low cost CCS811 MEMs sensor breakout board
  • eCO2 - equivalent CO2 (derived from the tVOC sensor by the CCS811. You don't need these readings if you have real CO2 measurement, but I wanted them for comparison.
  • Particulates - µg/cubic metre - this was measured using a Sharp GP2Y1010 optical particulate sensor.
It all squeezed onto a breadboard and I used a MonkMakesDuino Uno compatible as the 'Arduino'. 


The output in the Serial Monitor is in Tab separated fields that can just be copied and pasted into a spreadsheet.


Sensors

MH-Z14A (CO2)

You can find the MH-Z14A on eBay pretty cheap. While not the nicest CO2 sensor (I like the COZIR Ambient) they do have the benefit of being really cheap (for a true CO2 sensor) and when compared with better sensors are accurate enough for this kind of project.



The device has a number of interfaces, but I used the TTL serial interface. This just requires 4 pins from the device (19-Tx, 18-Rx, 17-GND, 16-5V) shown left to right in the figure above.

The sensor has a serial protocol that requires a message to be sent from the Arduino, some-time after which a response will be received from which the PPM (parts per million) of CO2 can be extracted.

The sensor uses spectral absorption to optically measure the CO2 compensation. No compensation is made for temperature (indoor use, so stable temp assumed) or altitude (atmospheric pressure - but this is a small factor).

You can find the datasheet here but the datasheet for the older version of this product here goes into more detail.


CCS811 (tVOC)

This air-quality sensor chip measure Total Volatile Organic Compounds (unhealthy chemicals with a boiling point that makes them likely to get into our air). We breathe these out and they are also found from pollutants and chemicals we use in our lives. There are not many of them in good clean outdoor air - in most places anyway.

My sensor came from eBay, but Sparkfun also sell a breakout board based on the same chip and made an Arduino library that I use in the code below.

It uses an I2C interface, but operates at 3.3V. You can find the datasheet here.

GP2Y1010 (Particulate density)

This is another eBay purchase. It measures reflections from particles in the air from which you can derive an approximate measurement of the number of µg per cubic meter of particles in the air. 



Pin 1 is the rightmost connection (white wire) in the figure above.

This depends on so many factors, not least the unknown mass of the particles, how much light (IR) they reflect etc that the reading has to be taken with a good dose of skepticism. But is does give an idea of how dusty the air is. 

The device uses an analog output with a sensitivity of 0.65 Volts per 100µg per metre cubed.

You can find the datasheet for this device here.

Schematic

Here's the schematic for the project.



The GP2Y1010 uses high current pulses of IR. To prevent the switching of the IR sender affecting the readings because of drops in the supply voltage, a 150Ω resistor charges a 200µF reservoir capacitor to supply the IR LED with a pulse of power. The internal amplifier of the GP2Y1010 must be supplied directly from the 5V rail (pin 6).

Note that the CCS811 is 3.3V supply. It also needs the WAK (Wake) and AD (I2C address option) pins tying to GND. The module I used has built-in I2C pullup resistors.

Software

Here's the sketch. I've added lots of comments. Between the comments and the datasheets, it should make sense. You will need to add the Sparkfun library to your IDE, before it will compile.

#include <SoftwareSerial.h>
#include "SparkFunCCS811.h"

#define CCS811_ADDR 0x5A

const int ledPin = A3;
const int sensorPin = A0;
const long samplePeriod = 1000L;   // sample period in ms

SoftwareSerial co2Sensor(10, 11); // RX, TX
CCS811 vocSensor(CCS811_ADDR);

/* 
 *  Messages to be sent by serial to the MH-Z14 CO2 Sensor
 */
const byte requestReading[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
const byte zeroCalibrate[] =  {0xff, 0x87, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf2};

byte result[9];   // buffer into which serial messages from the MH-Z14 are received

/*
 * Globals containing last sensor readings and the last time a set of readings was output
 */
long lastSampleTime = 0;   // more accurately when data was last logged to serial.
int vocCO2;
int tVoc;
int co2;
int partics;

void setup() {
  Serial.begin(9600);
  co2Sensor.begin(9600);  
  vocSensor.begin();
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH); // active low
  // write out headers for the columns of data
  Serial.println("CO2 (ppm)\teCO2 (ppm)\ttVOC(ppb)\tParts (µg/m3)");
}

/*
 * Send the calibrate message to the MH-Z14 - setting it to 400ppm
 */
void calibrate() {
  for (int i = 0; i < 9; i++) {
    co2Sensor.write(zeroCalibrate[i]); 
  }
}

void loop() {
  /*
   * Send the Z command after leaving the project in fresh 400ppm air for 5 mins
   */
  if (Serial.available()) {
    char ch = Serial.read();
    if (ch == 'z') {
      calibrate();
    }
  }
  /*
   * If the tVOC sensor has data ready, update the global variable
   */
  if (vocSensor.dataAvailable()) {
    vocSensor.readAlgorithmResults();
    vocCO2 = vocSensor.getCO2();
    tVoc = vocSensor.getTVOC();
  }
  /*
   * If we are due to log another row of data, then take the CO2 and
   * partical readings and then send them all to serial
   */
  long now = millis();
  if (now > lastSampleTime + samplePeriod) {
    lastSampleTime = now;
    co2 = readPPMSerial();
    partics = dustMicroGpm3();
    Serial.print(co2); Serial.print("\t"); 
    Serial.print(vocCO2); Serial.print("\t"); 
    Serial.print(tVoc); Serial.print("\t"); 
    Serial.println(partics);
  }
}

/*
 * Send a serial message (9 bytes) to the MH-Z14 and wait for a response
 * Parse the resulting data and return the reported CO2 concentration in ppm
 */
int readPPMSerial() {
  for (int i = 0; i < 9; i++) {
    co2Sensor.write(requestReading[i]); 
  }
  //Serial.println("sent request");
  while (co2Sensor.available() < 9) {}; // wait for response
  for (int i = 0; i < 9; i++) {
    result[i] = co2Sensor.read(); 
  }
  int high = result[2];
  int low = result[3];
  return high * 256 + low;
}

/*
 * Return the particulates reading in µg/m3
 */
int dustMicroGpm3() {
  digitalWrite(ledPin, LOW);  // active low pulse for the IR LED
  delayMicroseconds(280);     // wait before taking the analog reading
  int raw = analogRead(sensorPin);
  delayMicroseconds(40);
  digitalWrite(ledPin, HIGH); // end the pulse
  delayMicroseconds(9680);    // give the capacitor time to recharge

  float sensitivity = 0.0065; // V/µg/m3   .from the datasheet
  float v = float(raw) * 5.0 / 1023.0;
  return int(v / 0.0065);
}

Results

Here are the results for CO2, and eCO2, sampling every 10 seconds overnight in our bedroom. So, maybe time to sleep with the door open!



Links

You can find information on healthy levels of CO2 here.

For information on tVOC concentration and what it means, see here.




Friday, March 9, 2018

Review and Test of the MH-Z14A NDIR CO2 Sensor

The MH-Z14A measures CO2 concentrations using absorption of infra-red light. These devices are available on eBay, Amazon and other places from about $30-$40.

Ignore the capacitor across the power pins - this was a failed attempt by me to improve the reliability of the analog outputs.

My initial impressions of the module were pretty favourable. It looks pretty space-age with its gold coated chamber and 4 LEDs in the corner of the fabric window that allows the air to enter. These LEDs blink once a second as the readings are taken.

You can find a datasheet for the module here.

The device is available configured for different ranges of CO2 concentration. This module uses the most common range of 0 to 5000ppm.

Interfaces

The module actually has three interfaces - described in the datasheet.

  • Analog output - proportional to the ppm of CO2 range of 0.4 to 2V for full range.
  • PWM output - very slow PWM over the period of a second, with the HIGH duration being proportional to the ppm of CO2.
  • 9600 baud 3V/5V TTL UART interface. You send a message saying you want a reading, it responds with a reading.

Pinout

The module has lots of pins on it, most of which are duplicates. I just stuck to the main 0.1inch pitch 2 x 6 connector on the right. Note that the pin numbering on the board reflects the evolution of various connectors added through versions of the boards, and is therefore a bit odd.



First Use

My first experiment was just to power-up the board (5V to pin1, GND to pin 2) with a multimeter on the analog output (pin 4).

As soon as it powered up it was obvious that every second or so, the LEDs behind the fabric would light up and the current would surge to around 100mA. This was very short in duration so I expect the average power consumption of this unit is actually pretty low.

The analog output on pin 4 indicated 0.85V using the datasheet the ppm is calculated as:

ppm = (Vout - 0.4) * (5000 / 1.6)

In this case a ppm of 1406. This was wrong, I was in a big room with the door open and not breathing on the sensor, so I would expect a reading of around 400ppm.

It looked like the device was not calibrated. Or, calibrated in a somewhat unhealthy factory.

Calibration

You can calibrate by sending a serial message (see later) or by connecting pin 8 to GND for at least 7 seconds. Using the pin 8 methods, the new voltage reading was a much more believable 0.52V indicating a plausible 375 ppm.

After huffing on the sensor for a bit, it was easy to get the readings to go up and then work their way back down to ambient levels after about 5 minutes.

Its looking good!

Arduino

The next step was to hook the device up to an Arduino Uno so that I could compare the reading methods and also do a bit of logging.



Here is the test script that displays the readings from all three output methods in the Arduino Serial Monitor once every 10 seconds.

#include <SoftwareSerial.h>

const int analogPin = A0;
const int pwmPin = 9;

const long samplePeriod = 10000L;

SoftwareSerial sensor(10, 11); // RX, TX

const byte requestReading[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
byte result[9];
long lastSampleTime = 0;

void setup() {
  Serial.begin(9600);
  sensor.begin(9600);
  pinMode(pwmPin, INPUT_PULLUP);
}

void loop() {
  long now = millis();
  if (now > lastSampleTime + samplePeriod) {
    lastSampleTime = now;
    int ppmV = readPPMV();
    int ppmS = readPPMSerial();
    int ppmPWM = readPPMPWM();
    Serial.print(ppmV); 
    Serial.print("\t"); 
    Serial.print(ppmPWM); 
    Serial.print("\t");
    Serial.println(ppmS);
  }
}

int readPPMV() {
  float v = analogRead(analogPin) * 5.0 / 1023.0;
  int ppm = int((v - 0.4) * 3125.0);
  return ppm;
}

int readPPMSerial() {
  for (int i = 0; i < 9; i++) {
    sensor.write(requestReading[i]); 
  }
  //Serial.println("sent request");
  while (sensor.available() < 9) {}; // wait for response
  for (int i = 0; i < 9; i++) {
    result[i] = sensor.read(); 
  }
  int high = result[2];
  int low = result[3];
    //Serial.print(high); Serial.print(" ");Serial.println(low);
  return high * 256 + low;
}

int readPPMPWM() {
  while (digitalRead(pwmPin) == LOW) {}; // wait for pulse to go high
  long t0 = millis();
  while (digitalRead(pwmPin) == HIGH) {}; // wait for pulse to go low
  long t1 = millis();
  while (digitalRead(pwmPin) == LOW) {}; // wait for pulse to go high again
  long t2 = millis();
  long th = t1-t0;
  long tl = t2-t1;
  long ppm = 5000L * (th - 2) / (th + tl - 4);
  while (digitalRead(pwmPin) == HIGH) {}; // wait for pulse to go low
  delay(10); // allow output to settle.
  return int(ppm);
}

Here is a sample of the results from the Arduino Serial Monitor.


The first column is the analog reading, the middle column from the PWM interface and the right column from the serial interface.

The analog readings are all over the place. I don't think this interface is useable.

UPDATE: The analog output actually appears to be unbuffered. Using my DMM which has a Zin of about 10MΩ the voltage readings were within a couple of % of the serial readings. It was the relatively low Zin of the Arduino ADCs sample and hold messing things up. So, a unity gain op-amp buffer should do the trick.

The PWM interface is pretty consistent with the UART interface, although every so often there will be a bad reading.

The Serial interface is clearly the winner and if you implement the checksum algorithm specified in the datasheet I would expect 100% reliable readings.

Logging

To give the module a long term test, I set it running overnight in out bedroom with the following somewhat worrying results.

The y-axis is ppm of CO2 and the x-axis time (1 sample per minute).

I used the PWM measurement method and you can see the frequent misreadings (perhaps 1 in 50). But the trend is pretty good, showing the ppm rising well above 4000ppm, before the bedroom door was opened in the morning and the readings fell back to ambient values of 400ppm or so. Yes, we were all still alive!

The high overnight readings seem worryingly high for me, but at the moment, I do not have a calibrated sensor to compare with.

Conclusion

The sensor module's Serial interface works well and the PWM interface acceptably but the analog interface is only useful if buffered.

A serial interface that it both 5V and 3V compatible makes the module suitable for use with a Raspberry Pi or Arduino without the need for level conversion.

I cannot comment on the accuracy of the sensor without comparison to a trusted device. When I get one, I will come back here for an update.