#mh-z19 #embedded #sensor

no-std mh-zx-driver

MH-Z* family (MH-Z14/Z19/Z19B) CO2 sensor driver built on top of embedded-hal primitives

2 unstable releases

0.2.0 Jul 20, 2020
0.1.0 Apr 15, 2020

#52 in #sensor

Apache-2.0

19KB
301 lines

mh-zx-driver

MH-Z* family CO2 sensor driver built on top of embedded-hal primitives. This is a no_std crate suitable for use on bare-metal. The API is non-blocking (in a way compatible with the nb crate).

Supported devices

The code has been tested with MH-Z19B sensor, other sensors in MH-Z* family support the same UART protocol.

Datasheets

More resources


lib.rs:

MH-Z* CO2 sensor driver

MH-Z* family CO2 sensor driver built on top of embedded-hal primitives. This is a no_std crate suitable for use on bare-metal.

Usage

The Sensor struct exposes methods to send commands (write_packet_op) to the sensor and to read the response(read_packet_op).

Example

use mh_zx_driver::{commands, Sensor, Measurement};
use nb::block;
use core::convert::TryInto;
# use embedded_hal_mock::serial::{Mock, Transaction};

# let mut uart = Mock::new(&[
#   Transaction::write_many(commands::READ_CO2.as_slice()),
#   Transaction::flush(),
#   Transaction::read_many(&[
#     0xFF, 0x86, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79,
#   ]),
# ]);
let mut sensor = Sensor::new(uart);
// Send command to the sensor.
{
  let mut op = sensor.write_packet_op(commands::READ_CO2);
  block!(op()).unwrap();
};
// Read the response.
let mut packet = Default::default();
{
  let mut op = sensor.read_packet_op(&mut packet);
  block!(op()).unwrap();
}
let meas: Measurement = packet.try_into().unwrap();
println!("CO2 concentration: {}", meas.co2);

Future support

The experimental support for async/await can be activated by enabling the async feature in Cargo.toml. It adds async methods to the Sensor struct. Constructs the Sensor interface from 2 'halves' of UART.

Dependencies

~0–1.7MB
~35K SLoC