Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions samples/sensor/stts22h/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
47 changes: 47 additions & 0 deletions samples/sensor/stts22h/README.rst
Original file line number Diff line number Diff line change
@@ -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

<repeats endlessly every 2 seconds>
11 changes: 11 additions & 0 deletions samples/sensor/stts22h/prj.conf
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions samples/sensor/stts22h/sample.yaml
Original file line number Diff line number Diff line change
@@ -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
64 changes: 64 additions & 0 deletions samples/sensor/stts22h/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2025 Charles Dias
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>

#include <zephyr/drivers/sensor.h>

#include <stdio.h>

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);
}
}
Loading