Skip to content

Commit 1e32d44

Browse files
ycsincarlescufi
authored andcommitted
samples: sensor: Add sample for MH-Z19B
Add sample for MH-Z19B CO2 sensor. Signed-off-by: Yong Cong Sin <[email protected]>
1 parent 215571a commit 1e32d44

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(mhz19b)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})

samples/sensor/mhz19b/README.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
.. _mhz19b-sample:
2+
3+
MH-Z19B: CO2 Sensor Sample
4+
##########################
5+
6+
Description
7+
***********
8+
9+
This sample application demonstrate the configurations of the MH-Z19B CO2
10+
sensor, then periodically reads CO2 data from the sensor. The driver
11+
currently only support uart interrupt APIs.
12+
13+
Requirements
14+
************
15+
16+
The baudrate of the UART must be configured to 9600, and must support
17+
interrupt driven API.
18+
19+
Building and Running
20+
********************
21+
22+
To build the application, a board with UART interface
23+
has to be chosen, or a custom devicetree overlay has to be provided.
24+
Here Nucleo G0B1RE board is used.
25+
Then, connect the RX and TX from your Zephyr target board to the
26+
MH-Z19B CO2 sensor and power the sensor according to the datasheet.
27+
28+
.. zephyr-app-commands::
29+
:zephyr-app: samples/sensor/mhz19b
30+
:board: nucleo_g0b1re
31+
:goals: build
32+
:compact:
33+
34+
Sample Output
35+
=============
36+
37+
The application will perform runtime configuration of the sensor, then
38+
read back the sensor configuration data. After that it will read the CO2
39+
data every 2 seconds.
40+
41+
.. code-block:: console
42+
43+
Winsen MH-Z19B CO2 sensor application
44+
Configuring sensor - OK
45+
Reading configurations from sensor:
46+
Sensor range is set to 5000ppm
47+
Sensor ABC is enabled
48+
sensor: co2 reading: 758
49+
sensor: co2 reading: 759
50+
sensor: co2 reading: 762
51+
52+
<repeats endlessly every 2 seconds>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2021, G-Technologies Sdn. Bhd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&usart1 {
8+
current-speed = <9600>;
9+
mhz19b {
10+
compatible = "winsen,mhz19b";
11+
maximum-range = <10000>;
12+
abc-on;
13+
label = "MHZ19B_2";
14+
};
15+
};

samples/sensor/mhz19b/prj.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_STDOUT_CONSOLE=y
2+
CONFIG_SERIAL=y
3+
CONFIG_UART_INTERRUPT_DRIVEN=y
4+
CONFIG_SENSOR=y
5+
CONFIG_MHZ19B=y

samples/sensor/mhz19b/sample.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#
2+
# Copyright (c) 2021 G-Technologies Sdn. Bhd.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
sample:
7+
name: MH-Z19B CO2 sensor
8+
tests:
9+
sample.sensor.mhz19b:
10+
build_only: true
11+
harness: sensor
12+
tags: sensors
13+
depends_on: serial
14+
filter: dt_compat_enabled("winsen,mhz19b")

samples/sensor/mhz19b/src/main.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2021 G-Technologies Sdn. Bhd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <sys/printk.h>
9+
#include <drivers/sensor.h>
10+
#include <drivers/sensor/mhz19b.h>
11+
12+
void main(void)
13+
{
14+
const struct device *dev;
15+
struct sensor_value val;
16+
int ret;
17+
18+
printk("Winsen MH-Z19B CO2 sensor application\n");
19+
20+
dev = DEVICE_DT_GET_ONE(winsen_mhz19b);
21+
if (!device_is_ready(dev)) {
22+
printk("sensor: device not found.\n");
23+
return;
24+
}
25+
26+
printk("Configuring sensor - ");
27+
28+
val.val1 = 5000;
29+
ret = sensor_attr_set(dev, SENSOR_CHAN_CO2, SENSOR_ATTR_FULL_SCALE, &val);
30+
if (ret != 0) {
31+
printk("failed to set range to %d\n", val.val1);
32+
return;
33+
}
34+
35+
val.val1 = 1;
36+
ret = sensor_attr_set(dev, SENSOR_CHAN_CO2, SENSOR_ATTR_MHZ19B_ABC, &val);
37+
if (ret != 0) {
38+
printk("failed to set ABC to %d\n", val.val1);
39+
return;
40+
}
41+
42+
printk("OK\n");
43+
44+
printk("Reading configurations from sensor:\n");
45+
46+
ret = sensor_attr_get(dev, SENSOR_CHAN_CO2, SENSOR_ATTR_FULL_SCALE, &val);
47+
if (ret != 0) {
48+
printk("failed to get range\n");
49+
return;
50+
}
51+
52+
printk("Sensor range is set to %dppm\n", val.val1);
53+
54+
ret = sensor_attr_get(dev, SENSOR_CHAN_CO2, SENSOR_ATTR_MHZ19B_ABC, &val);
55+
if (ret != 0) {
56+
printk("failed to get ABC\n");
57+
return;
58+
}
59+
60+
printk("Sensor ABC is %s\n", val.val1 == 1 ? "enabled" : "disabled");
61+
62+
while (1) {
63+
if (sensor_sample_fetch(dev) != 0) {
64+
printk("sensor: sample fetch fail.\n");
65+
return;
66+
}
67+
68+
if (sensor_channel_get(dev, SENSOR_CHAN_CO2, &val) != 0) {
69+
printk("sensor: channel get fail.\n");
70+
return;
71+
}
72+
73+
printk("sensor: co2 reading: %d\n", val.val1);
74+
75+
k_msleep(2000);
76+
}
77+
}

0 commit comments

Comments
 (0)