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.


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.


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.


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() {
  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++) {

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') {
   * If the tVOC sensor has data ready, update the global variable
  if (vocSensor.dataAvailable()) {
    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"); 

 * 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++) {
  //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);
  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);


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!


You can find information on healthy levels of CO2 here.

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

Friday, September 7, 2018

Review and use of: Katsu ZD-8915 Desoldering Station

Desoldering through-hole components with two or three legs, is pretty straightforward. Apply heat and some fresh solder to the underside and pull them out from the top. But when it comes to chips and modules with more pins, this approach doesn't work. 
Some people manage to master the use of the small pen-shaped 'solder-suckers'. The idea is that you melt the solder with an iron, and as soon as the solder is molten, place the solder-sucker over the joint and release the spring to suck up the solder. I've tried and failed to learn that skill.
Enter stage left, the desoldering station.

These use a hollow tip soldering iron and a vacuum pump, so that solder removal is a LOT easier than trying to use a pen-style solder-sucker.
I bought the model shown (Katsu ZD-8915 Desoldering Station) from Amazon, but they are also available on eBay and I'm sure elsewhere too. 

What you get

The package included:
  • the control unit, that shows both the actual and set temperatures (up to 480C)
  • de-soldering gun
  • two spare tips of different hole sizes, giving you three tips in total (1.0, 1.2 and 1.5mm)
  • a good solid stand for the desoldering gun
  • spare wadding pads 
  • three sizes of cleaning tools to poke out the solder from the hollow nozzle
  • a factually correct but not very informative instruction book
Everything fitted together very well and actually felt like it was pretty good quality. The solder chamber containing the spring-loaded piston has to be occasionally emptied of solder and at first time is a bit confusing, as the wadding and metal disk just sit against the spring and fall out when you open up the chamber. Putting it back in, it took me a while to realise that the wadding pad goes in first, followed by the metal disk. After using the gun for a while I realised why there were spares of the wadding, as it gets spattered with solder.


I gave up on the instructions, which don't really explain how to use the gun and just watched a few YouTube videos.

Here's what I found to work for me, desoldering some DIL 7-segment LED modules for practice.
  1. Push the back of the piston down to prime the spring
  2. Apply heat to the solder joint until the solder melts (applying a bit of fresh solder and adjusting the temperature as required)
  3. Pull the trigger to turn on the suction pump
  4. Adjust the angle of the nozzle to get as good a fit over the solder joint as possible. I found that if you listened to the vacuum pump you could hear it work harder when you get a better seal.
  5. Release the piston to make the final suck and you should see a splodge of solder fly up the see-through piston and embed itself in the cotton. Thats the fun bit!
  6. Inspect the joint and repeat if necessary.
  7. Get a flat-bladed screwdriver under the thing you are desoldering and gently lever it out - desoldering some more if it won't move.

Feeling very pleased with my new purchase, I turned it off and went and had some lunch. When I came to start again, it didn't seem to be sucking.

If I had read the instructions properly, I would have noticed the section warning me to ALWAYS ROD OUT THE NOZZLE BEFORE YOU TURN OFF THE HEAT!

I had bunged up the nozzle, just where it meets the cylinder. It then took about an hour of poking, changing the temperature, running in more solder and even pouring in liquid flux. Eventually I unblocked it by taking the front off the gun and poking the solder out by rodding it from the other side.

Obviously this was entirely my fault and I'd like to say I have learned my lesson, but its quite possible that I'll forget again. 


Admittedly, this is the only desoldering tool that I've ever owned, so I don't have anything except the solder-sucking pens to compare it with, but I have to say I was very impressed.
This has already more than paid for itself because I had some expensive modules that I really needed to reclaim. Overall, the quality is pretty good and I didn't expect it to be as easy to use as it was.

If like me you have some high value components, or a PCB that can't easily be replaced then I can recommend this tool. It really does work. 

Just remember to clean it out before you turn it off.

Thursday, June 14, 2018

Cuban Makers at "Copincha" in Havana

Last week I found myself in front of a room full of Cubans in Havana, delivering a course on Arduino in broken Spanish. Was this a variation on the kind of dream where you are sitting down to an exam without having done a stroke of revision? Or the dream where you find yourself at work having forgotten to put on your trousers? 
The sequence of events that led to this radical departure from my comfort zone - not so much stepping outside it, but more waving it goodbye and jumping on a plane - started a couple of years ago when I was contacted by Jonnet who had seen a Spanish language edition of one of my books and wanted to borrow it for her Cuban partner Mauri.
Mauri and Jonnet were keen to establish some kind of makerspace in Havana to provide a place for local makers could come and share ideas, tools and resources. Cubans, by necessity, are an inventive and creative group of people. Resources are scarce and so a make-do-and-mend culture is totally natural to them. If ever there was a race of makers, it is probably the Cubans.
My wife Linda and I have been learning Spanish for years despite little evidence of success. We also spent our honeymoon in Cuba and have very fond memories of the country. And so, to cut a long story short, in a rash moment I volunteered to run an Arduino course at their makerspace when it was up and running. And a year or so later, that is how I found myself in front of a group of Cubans, talking about Arduino in Spanish.
Mauri, the driving force behind the makerspace also lives in the house containing the makerspace in quarters above the two rooms that make up the space.  The space is called "Copincha" - "pincha" is Cuban slang for "work" and so "copincha" is a way of saying "co-work".
Mauri and friends have used scrap wooden packaging cases to make box stools for sitting on as well as large work tables with table-tops that are removable to reveal ample storage space for tools and supplies to make best use of the space. There's probably ten square meters of table top and maybe twenty stools for sitting on. That is quite some labour of love!

My Arduino course was the first course to be run at the makerspace and since we had mixed levels of ability, with some very skilled Arduinoistas, I ran introductory Arduino lessons in the morning and in the afternoon offered "consultations" to the advanced groups who had projects that they wanted to work on.
I brought Arduino compatibles (actually MonkMakesDuinos) together with basic component kits and breadboards with me but the course relied on the students having access to a laptop to program the MonkMakesDuinos. In true Cuban style, this involved a fair amount of borrowing and inventive solutions. For example, one student used a homemade Raspberry Pi laptop to program the Arduino. Another used a Windows tablets with a pop-up keyboard. With sharing and cooperation everyone was able to participate.
I am very pleased to hear that the course is due to be repeated in July (when many Cubans are on leave from their day jobs). This time, the course is being delivered by Yusinier, one of the local experts at the course who amongst his many skills in art and technology has taught Arduino to university students.
The course and supporting code is available as open source on GitHub here and I would very much welcome any pull-requests to improve the Spanish.
Here are just a couple of the things that the course attendees made during the course:
Juan, a retired mechanical engineer with a passion for replica cars, worked on adding automation to a fibre-glass modelled replica he was building from scratch.

The chassis of the car was built to use a single gear-motor as rear-wheel drive and a servo motor to control the steering. This was combined with an ultrasonic range-finder to detect obstacles and invoke an avoidance function that involved a randomly chosen turn to the left or the right, followed by reversing. The design also included automatic brake lights. The steering mechanism was made entirely from waste pieces of plastic and skilful use of a Dremmel.
Juan has an enormous collection of vintage model cars that he restores, some of which are for display and some for sale at a shop he runs with friends, at San Lázaro 1060 entre San Francisco y Espada - somewhere around here.
Yusinier is a visual artist and film maker and amongst other things has to devise his own props, such as remotely triggered fake bullet strikes. He often uses Arduino and his project during the course was the controller for a motorized camera dolly.

The attendees at this first course at Copincha were: Sergio Valdés García, Yusnier Mentado Fernández, Lisa Gómez Blanco, Juan Infante, Alberto José Quesada Madrigal, Javier Quesada Madrigal, Os Leans and Jossue Arteche .

In England, when I need components, an Arduino or a cheap module from China, all of these things are easily obtainable with a few clicks of a mouse. This is not so easy in Cuba. For most Cubans a £30 Raspberry Pi represents more than a months pay. Even if they want one, they cannot just order it from Amazon or any of the other sources that we take for granted. Typically, getting such things into Cuba relies on visitors to Cuba like us bringing things with us when we visit. Donating things as personal gifts to Cubans. Visitors to Cuba are advised in the guide books to bring crayons, bars of soap and Lego as gifts so why not things for makers?

Perhaps if you are planning a trip to this beautiful country and will be visiting Havana, you could contact Mauri (maurice.haedo@gmail.com) and see if he could make use of a few Arduino clones, or eBay specials for "Copincha". If you do, you will be assured of a warm welcome a great coffee and a glimpse behind the scenes of  this amazing country.