Skip to content

re-ws.pl

ReverseEngineering WorkStation

  • Home
  • Tutorials
  • Random
  • About

mhz14a – program for managing MH-Z14/MH-Z14A CO2 sensors via UART

Posted on February 17, 2019 - May 30, 2019 by Kamil (aka. v3l0c1r4pt0r)
MH-Z14A CO2 sensor

When I have seen CO2 sensor for the first time, it was quite expensive device. Well, if one want to buy consumer device these days, it still could cost a lot. However in the days of cheap Chinese electronics sellers on biggest auction platforms, for makers, situation is quite different now. MH-Z14 is the cheapest CO2 sensor I was able to find. I costs about $19 and comes in few variants: MH-Z14 and MH-Z14A. Also it can measure up to 1000 ppm, up to 2000 ppm or up to 5000 ppm. However the range does not matter in practice, as it is possible to switch between them using UART.

The device interfaces are quite flexible for such a cheap device, as beside mentioned UART port it provides PWM and analog output. However, I was not able to measure valid value using analog and my cheap multimeter. Maybe some more sophisticated equipment is required for that.

I have to make one note here: device I bought is labeled as MH-Z14A and its range is 0-5000 ppm. Other variants might have different features. For mine, there is no UART protocol documentation. Yet, protocol documented under name MH-Z14 works, so be careful.

mhz14a – UART protocol implementation

As UART protocol of the device is quite complex and what is more important a binary one, it is required to communicate with the device programatically. It is impractical to use just a terminal application. For the purpose I created mhz14a program. It wraps all the internals with nice getopt-based interface, so can be used from both interactive shells and scripts. What user has to know is what is his/her UART device path and choose desired command (most likely read – -r).

At first program have to be compiled. For now installation in a system is not possible, so compilation is going to leave binary in build/ directory. Project uses cmake, so to compile one have to execute:

mkdir -p build && cd build
cmake ..
make
src/mhz14a --help

To sum up, basic usage of the program is like below (white are the commands I type):

$ src/mhz14a -r -d /dev/ttyUSB0
410
$

Device documentation

Some sellers provide some basic guide of what is where themselves, so digging for a datasheet might be unnecessary. Fortunately, even if they don’t provide anything, Winsen, who is their manufacturer, provides everything needed. In this case, what is nothing new in the world of Chinese manufacturers, it is worth to see Chinese language variant instead of the English one, as it contain more information.

Final words

For the moment, I don’t have commands described only in Chinese implemented, so it is not possible to switch supported ranges and turn off automatic calibration. However this should not be issue in most cases, as what most users need is just to read sensor data.

I might do some work with the sensor in future, as my initial idea was to switch to analog output just after setting up UART. Unfortunately I have problems with analog pin readings. If I decide to develop the project again, I am definitely going to implement these hidden commands, as well as do some refactoring, as code sometimes looks really bad (thankfully, I was writing some unit tests, thanks to one of my previous projects – cmocka/cmake template, which I used here).

Posted in UncategorizedTagged electronics, English, hardware, Linux, MH-Z14A, sensor, software

Post navigation

SADVE – tiny program for computing #define values
Printing pictures like its 1873 using Oki 3321 dot-matrix printer

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tags

Android assembly busybox C CAN can-hacking cc-factory cmake compiler docker Dreamspark electronics English gcc hacking hardware JavaCard JCOP kernel KiCAD library Linux PC PCB pinout PKI polski programming Python radio Raspberry Pi Reverse Engineering RTL-SDR SDC SDM SDR smart card software tor tty UART UEFi Windows X.509 Xperia Pro

Recent Posts

  • PHP build for use bundled in Android applications
  • Running graphical apps inside Docker containers
  • Plugin architecture demo for Python projects
  • Authorizing adb connections from Android command line (and making other service calls from cli)
  • How to recover torrent from rtorrent meta files

Recent Comments

  • pomi on Playing with GF-07 GPS device
  • pomi on Playing with GF-07 GPS device
  • Hamdy Abumgata on Playing with GF-07 GPS device
  • Mousum Gogoi on Playing with GF-07 GPS device
  • Eason on Sniffing USB traffic with DSLogic logic analyzer into pcap file

Categories

  • News
  • Random
  • Reversing LKV373A
  • Setting up new v3 Hidden Service with ultimate security
  • Tutorials
  • Uncategorized
  • Understanding JCOP

Links

  • Me @ github
  • LKV373A Wiki
  • DevTomek

Archives

  • December 2024
  • November 2024
  • May 2024
  • July 2023
  • October 2022
  • August 2022
  • July 2021
  • June 2021
  • May 2021
  • December 2020
  • November 2020
  • October 2020
  • August 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • February 2019
  • November 2018
  • October 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • September 2017

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
Proudly powered by WordPress | Theme: micro, developed by DevriX.