Skip to content

Commit

Permalink
sensor: sample: Add sample for Panasonic SN-GCJA5 laser particle sensor
Browse files Browse the repository at this point in the history
Added sample for Panasonic's "particulate matters (PM) sensor" laser type.
The sample detects values for PM1, PM2.5, PM10 and the particle counts
periodically and prints them once a second to the console.

Signed-off-by: Steffen Jahnke <[email protected]>
  • Loading branch information
pideu-sj committed Mar 14, 2023
1 parent 4c70de5 commit 7d1f0e5
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
8 changes: 8 additions & 0 deletions samples/sensor/sngcja5/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(sngcja5)

target_sources(app PRIVATE src/main.c)
59 changes: 59 additions & 0 deletions samples/sensor/sngcja5/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
.. _sngcja5:

SN-GCJA5 Laser Particle Sensor
##############################

Overview
********

This sample shows how to use the Zephyr Sensors API driver for the Panasonic
Particulate Matter (PM) sensor SN-GCJA5.

The sample detects values for PM1, PM2.5, PM10 and the particle counts
periodically and prints them once a second to the console.

Please see detailed sensor information for `SN-GCJA5 laser particle sensor`_.

Building and Running
********************

SN-GCJA5 via Arduino I2C pins
=============================

If you wire the sensor to I2C on an Arduino header, build and flash with:

.. zephyr-app-commands::
:zephyr-app: samples/sensor/sngcja5
:goals: build flash
:board: pan1781_evb
:gen-args: -DDTC_OVERLAY_FILE=arduino_i2c.overlay

The devicetree overlay :zephyr_file:`samples/sensor/sngcja/arduino_i2c.overlay`
works on any board with a properly configured Arduino pin-compatible I2C
peripheral. To build for another board, change "pan1781_evb" above to that
board's name.

Sample Output
=============

The SN-GCJA5 sensor, samples and prints out the values to the serial console
once a second. Following output shows some example values.

.. code-block:: console
*** Booting Zephyr OS build zephyr-v3.2.0-3320-g30fd0a5a3b7f ***
Found device "sngcja5", getting sensor data
new sample:
pc0_5: 5
pc1_0: 29
pc2_5: 3
pc5_0: 0
pc7_5: 0
pc10_0: 0
pm1_0: 3.127000
pm2_5: 3.310000
pm10_0: 3.723000
.. target-notes::

.. _`SN-GCJA5 laser particle sensor`: https://industry.panasonic.eu/products/components/sensors/particulate-matter-sensor
16 changes: 16 additions & 0 deletions samples/sensor/sngcja5/arduino_i2c.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) 2023 Panasonic Industrial Devices Europe GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/

&arduino_i2c {
status = "okay";

sngcja5: sngcja5@33 {
label = "sngcja5";
compatible = "panasonic,sngcja5";
reg = <0x33>;
status = "okay";
};
};
2 changes: 2 additions & 0 deletions samples/sensor/sngcja5/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_I2C=y
CONFIG_SENSOR=y
10 changes: 10 additions & 0 deletions samples/sensor/sngcja5/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sample:
name: sngcja5 sensor sample
tests:
sample.sensor.sngcja5:
harness: console
tags: sensors
integration_platforms:
- pan1781_evb
depends_on: arduino_i2c
extra_args: "DTC_OVERLAY_FILE=arduino_i2c.overlay"
53 changes: 53 additions & 0 deletions samples/sensor/sngcja5/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023 Panasonic Industrial Devices Europe GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/sensor.h>

static struct sensor_value sensor_values[9];

void main(void)
{
int ret;
const struct device *const dev = DEVICE_DT_GET_ONE(panasonic_sngcja5);

if (!device_is_ready(dev)) {
printk("Device %s is not ready.\n", dev->name);
return;
}

printk("Found device \"%s\", getting sensor data\n", dev->name);

while (42)
{
ret = sensor_sample_fetch(dev);
if (ret) {
printk("Failed to fetch a sample, %d\n", ret);
return;
}

ret = sensor_channel_get(dev, SENSOR_CHAN_ALL, &sensor_values[0]);
if (ret) {
printk("Failed to get sensor values, %d\n", ret);
return;
}

printk("new sample:\n");
printk("pc0_5: %i\n", sensor_values[0].val1);
printk("pc1_0: %i\n", sensor_values[1].val1);
printk("pc2_5: %i\n", sensor_values[2].val1);
printk("pc5_0: %i\n", sensor_values[3].val1);
printk("pc7_5: %i\n", sensor_values[4].val1);
printk("pc10_0: %i\n", sensor_values[5].val1);
printk("pm1_0: %i.%i\n", sensor_values[6].val1, sensor_values[6].val2);
printk("pm2_5: %i.%i\n", sensor_values[7].val1, sensor_values[7].val2);
printk("pm10_0: %i.%i\n\n", sensor_values[8].val1, sensor_values[8].val2);

k_sleep(K_MSEC(1000));
}
}

0 comments on commit 7d1f0e5

Please sign in to comment.