When it comes to indoor air quality, especially during the cold days, we usually think of trying to keep the humidity at an acceptable level to prevent irritation of the respiratory tract occurring from dry air caused by ventilation, which reduces the relative humidity of outside air (that was well-saturated with moisture while it was still cold) by heating it up indoors.
However generally avoiding ventilation will sooner or later result in poor air quality, which is way more difficult to measure than humidity itself: indoor pollutants, mostly from human metabolism (but also furniture and everything else in your house) can vary among countless groups of complex substances that would be impossible to quantify analytically by reasonable means.
The carbon dioxide level is thus often used as an indicator of how much the air is “used up” by human respiration, so you can get a good estimate about air quality in your room or office by just monitoring that single component:
While fresh air is close to the average outdoor level of around 400ppm CO2, you should try to keep your indoor level below 1000ppm to prevent fatigue and reduced cognitive performance.
Exceeding 4000ppm can show significant negative effects on concentration and feeling comfortable.
Devices that measure and display the CO2 concentration (just like we are used to from countless thermometers displaying humidity) are still rare and very expensive, also they would require a little more power than a humidity meter running for years on two AA batteries.
Among the first devices bringing a low-cost solution to the mass market was the weather station from netatmo: The pack contains several sensors connected to a base station that will collect all CO2 measurements and transmit them over WiFi – directly to the netatmo cloud servers. The only way to see your local air quality in the same room is by touching the top of the base, which will then illuminate in one of three colours to give a vague estimate of the situation. Everything else works exclusively through the cloud service: They will monitor human activity (and even sound levels!) in your house and show you fancy-looking graphs on the app or website in return.
If that does not sound like the solution you would want to be running and connected to the internet 24/7, you will quickly stumble upon those MH-Z14 / MH-Z14A sensors that sell on AliExpress for less than $20 USD, shipping included.
They come with their own microcontroller and several interface options like Analog, PWM and UART.
The UART interface is somewhat straightforward, especially if you keep sending the same command over and over to query the current value (so there’s no need to calculate the checksum but rather use the static one that matches the command, as given in the datasheet): Send 9 bytes at 9600 baud and receive 9 bytes with the measurement. There’s not even the need to verify the checksum of the received reply – if you’re lazy, you can just read the value from the third and fourth byte of the reply.
For the beginning, you can just wire it up to some USB CP2102 UART module – the sensor module is perfectly fine with using the 5V from your USB port.
Which results in this little proof-of-concpet Python script that will write the current value to the console each five seconds (make sure to
pip install pyserial first and adjust your /dev/ttyUSB or COM port number):
import serial, time z14 = serial.Serial(port="COM13",baudrate=9600) while True: z14.write(bytearray.fromhex("FF 01 86 00 00 00 00 00 79")) result = z14.read(size=9) print(result * 256 + result) time.sleep(5)
You will notice it takes a few minutes for the sensor to warm up and deliver plausible results.
Also, there seems to be a difference between ZH14 and ZH-14A modules regarding calibration: The 14A variant performs automatic calibration within each interval of 24 hours – in other words, you have to make sure to let in some fresh air from outside at least once per day, so it can calibrate the lowest reading within the previous 24 hours to be around 400ppm! This seems quite annoying, but also makes using the sensor a lot easier on the other hand.
Now a console window scrolling the latest sensor reading may seem quite appealing to the average nerd, but if you want to go for a more netatmo-ish style of standalone device, how about adding a microcontroller with an oled display and a tocuh sensor? The display would show a tiny version of the CO2 graph, similar to the netatmo apps, on the device itself after you activate it with a touch.
And to make it even cooler, I used one of those new-fangled VL53L0X Time-of-flight laser distance sensors from ST – those are basically the successor of the tiny sensors you have in smartphones to detect user presence (to disable the touch screen while holding the phone close to the head) and allow for range measurements of up to two meters. So you don’t even need to touch the device itself, but could rather hover your hand over the device to switch on the measurements display, or use gesture recognition (upwards or downwards motion) for additional functions.
Here’s some example using an STM32F103C8T6 board (mostly since those are less than $2 on AliExpress for an ARM Cortex MCU and also code for VL53L0x is readily available from ST; also there are Chinese clones of the ST-Link / v2 debugger availably for very little money, compared to e.g. JTAGICE debuggers for AVR controllers, or platforms like ESP8266 or Arduino where you have no live debugging by default at all). Also it has built-in USB that you can use for USB to serial or even mass storage (how about donwloading your measurements from a dynamically created .csv-file for example?).
Using STM32 CubeMX from ST you can graphically set your desired pin functions and generate all the hardware-related code, similar to the web-based tool availbale from Microchip / Atmel at http://start.atmel.com for the AVR and ATSAM MCUs).
Here’s my CubeMX file (I actually used both I2C cores for separating the OLED and the ToF sensor for mere simplicity in wiring).
But after all, you should of course use the platform you feel most experienced with, considering that SSD1306 OLED drivers are available almost everywhere, and you can check the VL53L0x library from Pololu for a more lightweight alternative to ST’s bloated C library to control the laser distance sensor.
Next thing would be to make decent case for it: currently my prototype resides in a 3D-printed cylinder without any bottom plate, and I still need to figure out how to fit a battery in there and how to charge it (or probably just run a cable to the nearest usb port).
The final goal would be connecting it to the USB port of some OpenWRT router for data logging and network access, but then again you might as well go for ESP8266 in the first place… And after all, summer is coming closer, making this the ideal project for some spare time in next winter…
pictures will follow 🙂
One thought on “Interfacing the MH-Z14A carbon dioxide sensor with OLED display and ToF Laser distance sensor on STM32”
Thank you for talking about the different versions of the MH-Z14 sensor. It’s challenging to find that information anywhere else but here. Glad I found your post 🙂