diff --git a/samples/sensor/stts22h/CMakeLists.txt b/samples/sensor/stts22h/CMakeLists.txt new file mode 100644 index 0000000000000..19173bc22a853 --- /dev/null +++ b/samples/sensor/stts22h/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright (c) 2025 Charles Dias +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(stts22h) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/sensor/stts22h/README.rst b/samples/sensor/stts22h/README.rst new file mode 100644 index 0000000000000..015b6d30c073b --- /dev/null +++ b/samples/sensor/stts22h/README.rst @@ -0,0 +1,47 @@ +.. zephyr:code-sample:: stts22h + :name: STTS22H Temperature sensor + :relevant-api: sensor_interface + + Get ambient temperature from the STTS22H sensor. + +Overview +******** + +This sample reads the onboard STTS22H ambient temperature sensor using the Zephyr +sensor API and prints the value periodically to the console. + +Requirements +************ + +This sample uses the STTS22H sensor controlled using the I2C interface. + +References +********** + + - STTS22H: https://www.st.com/en/mems-and-sensors/stts22h.html + +Building and Running +******************** + +This sample outputs STTS22H temperature data to the console. + +Supported boards: STM32U5G9J-DK1. + +.. zephyr-app-commands:: + :zephyr-app: samples/sensor/stts22h/ + :board: stm32u5g9j_dk1 + :goals: build flash + + +Sample Output +============= + +.. code-block:: console + + Zephyr STTS22H sensor sample. Board: stm32u5g9j_dk1 + [ 10 ms] Temperature: 27.7 C + [ 2013 ms] Temperature: 27.6 C + [ 4016 ms] Temperature: 27.6 C + [ 6019 ms] Temperature: 27.6 C + + diff --git a/samples/sensor/stts22h/prj.conf b/samples/sensor/stts22h/prj.conf new file mode 100644 index 0000000000000..c8b3144c40dce --- /dev/null +++ b/samples/sensor/stts22h/prj.conf @@ -0,0 +1,11 @@ +CONFIG_STDOUT_CONSOLE=y +CONFIG_SENSOR=y + +CONFIG_STTS22H_TRIGGER_NONE=y + +#debug +#CONFIG_DEBUG=y +#CONFIG_LOG=y +#CONFIG_SENSOR_LOG_LEVEL_DBG=y + +CONFIG_CBPRINTF_FP_SUPPORT=y diff --git a/samples/sensor/stts22h/sample.yaml b/samples/sensor/stts22h/sample.yaml new file mode 100644 index 0000000000000..b1dd714ae5a22 --- /dev/null +++ b/samples/sensor/stts22h/sample.yaml @@ -0,0 +1,11 @@ +sample: + name: STTS22H temperature sensor +tests: + sample.sensor.stts22h: + depends_on: + - i2c + filter: dt_compat_enabled("st,stts22h") + tags: + - sensors + integration_platforms: + - stm32u5g9j_dk1 diff --git a/samples/sensor/stts22h/src/main.c b/samples/sensor/stts22h/src/main.c new file mode 100644 index 0000000000000..8fc74608b30d5 --- /dev/null +++ b/samples/sensor/stts22h/src/main.c @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025 Charles Dias + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include + +#include + +static void stts22h_config(const struct device *stts22h) +{ + struct sensor_value odr_attr; + + /* Set STTS22H ODR to 100 Hz (or as close as supported) */ + odr_attr.val1 = 100; + odr_attr.val2 = 0; + if (sensor_attr_set(stts22h, SENSOR_CHAN_AMBIENT_TEMP, + SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) { + printk("Cannot set sampling frequency for STTS22H\n"); + return; + } +} + +int main(void) +{ + const struct device *const stts22h = DEVICE_DT_GET_ONE(st_stts22h); + int ret; + + printf("Zephyr STTS22H sensor sample. Board: %s\n", CONFIG_BOARD); + + if (!device_is_ready(stts22h)) { + printk("%s: device not ready.\n", stts22h->name); + return 0; + } + + stts22h_config(stts22h); + + while (1) { + struct sensor_value temp; + + ret = sensor_sample_fetch(stts22h); + if (ret < 0) { + printk("STTS22H sample fetch error (%d)\n", ret); + k_msleep(1000); + continue; + } + + ret = sensor_channel_get(stts22h, SENSOR_CHAN_AMBIENT_TEMP, &temp); + if (ret < 0) { + printk("STTS22H channel read error (%d)\n", ret); + k_msleep(1000); + continue; + } + + printf("[%6lld ms] Temperature: %.1f C\n", k_uptime_get(), + sensor_value_to_double(&temp)); + + k_msleep(2000); + } +}