Skip to content

Commit 0e56796

Browse files
committed
samples: sensor: stts22h: Minimal polling sample
Add a simple STTS22H sample that utilizes the Zephyr sensor API in polling mode to fetch and print the ambient temperature every 2 seconds periodically. Signed-off-by: Charles Dias <[email protected]>
1 parent 65d6bcc commit 0e56796

File tree

5 files changed

+149
-0
lines changed

5 files changed

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

samples/sensor/stts22h/README.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.. zephyr:code-sample:: stts22h
2+
:name: STTS22H Temperature sensor
3+
:relevant-api: sensor_interface
4+
5+
Get ambient temperature from the STTS22H sensor.
6+
7+
Overview
8+
********
9+
10+
This sample reads the onboard STTS22H ambient temperature sensor using the Zephyr
11+
sensor API and prints the value periodically to the console.
12+
13+
Requirements
14+
************
15+
16+
This sample uses the STTS22H sensor controlled using the I2C interface.
17+
18+
References
19+
**********
20+
21+
- STTS22H: https://www.st.com/en/mems-and-sensors/stts22h.html
22+
23+
Building and Running
24+
********************
25+
26+
This project outputs STTS22H temperature data to the console. It requires a
27+
board that has an STTS22H connected over I2C and enabled in its devicetree.
28+
29+
Examples of supported boards include STM32U5G9J-DK1, where the sensor is
30+
enabled at I2C3 address 0x3F in the board DTS.
31+
32+
.. zephyr-app-commands::
33+
:zephyr-app: samples/sensor/stts22h/
34+
:board: stm32u5g9j_dk1
35+
:goals: build flash
36+
37+
38+
Sample Output
39+
=============
40+
41+
.. code-block:: console
42+
Zephyr STTS22H sensor sample. Board: stm32u5g9j_dk1
43+
[ 10 ms] Temperature: 27.7 C
44+
[ 2013 ms] Temperature: 27.6 C
45+
[ 4016 ms] Temperature: 27.6 C
46+
[ 6019 ms] Temperature: 27.6 C
47+
48+
<repeats endlessly every 2 seconds>

samples/sensor/stts22h/prj.conf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CONFIG_STDOUT_CONSOLE=y
2+
CONFIG_I2C=y
3+
CONFIG_GPIO=y
4+
CONFIG_SENSOR=y
5+
6+
CONFIG_STTS22H_TRIGGER_NONE=y
7+
8+
#debug
9+
#CONFIG_DEBUG=y
10+
#CONFIG_LOG=y
11+
#CONFIG_SENSOR_LOG_LEVEL_DBG=y
12+
13+
CONFIG_CBPRINTF_FP_SUPPORT=y

samples/sensor/stts22h/sample.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sample:
2+
name: STTS22H temperature sensor
3+
tests:
4+
sample.sensor.stts22h:
5+
depends_on:
6+
- i2c
7+
filter: dt_compat_enabled("st,stts22h")
8+
tags:
9+
- sensors
10+
integration_platforms:
11+
- stm32u5g9j_dk1
12+
harness: console
13+
harness_config:
14+
type: multi_line
15+
regex:
16+
- "Temperature: (.*)"

samples/sensor/stts22h/src/main.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2025 Charles Dias
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/sys/printk.h>
9+
10+
#include <zephyr/drivers/sensor.h>
11+
12+
#include <stdio.h>
13+
14+
static void stts22h_config(const struct device *stts22h)
15+
{
16+
struct sensor_value odr_attr;
17+
18+
/* Set STTS22H ODR to 100 Hz (or as close as supported) */
19+
odr_attr.val1 = 100;
20+
odr_attr.val2 = 0;
21+
if (sensor_attr_set(stts22h, SENSOR_CHAN_AMBIENT_TEMP,
22+
SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
23+
printk("Cannot set sampling frequency for STTS22H\n");
24+
return;
25+
}
26+
}
27+
28+
int main(void)
29+
{
30+
const struct device *const stts22h = DEVICE_DT_GET_ONE(st_stts22h);
31+
int ret;
32+
33+
printf("Zephyr STTS22H sensor sample. Board: %s\n", CONFIG_BOARD);
34+
35+
if (!device_is_ready(stts22h)) {
36+
printk("%s: device not ready.\n", stts22h->name);
37+
return 0;
38+
}
39+
40+
stts22h_config(stts22h);
41+
42+
while (1) {
43+
struct sensor_value temp;
44+
45+
ret = sensor_sample_fetch(stts22h);
46+
if (ret < 0) {
47+
printk("STTS22H sample fetch error (%d)\n", ret);
48+
k_msleep(1000);
49+
continue;
50+
}
51+
52+
ret = sensor_channel_get(stts22h, SENSOR_CHAN_AMBIENT_TEMP, &temp);
53+
if (ret < 0) {
54+
printk("STTS22H channel read error (%d)\n", ret);
55+
k_msleep(1000);
56+
continue;
57+
}
58+
59+
printf("[%6lld ms] Temperature: %.1f C\n", k_uptime_get(),
60+
sensor_value_to_double(&temp));
61+
62+
k_msleep(2000);
63+
}
64+
}

0 commit comments

Comments
 (0)