Skip to content

Commit ae87673

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 2a09063 commit ae87673

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 sample outputs STTS22H temperature data to the console.
27+
28+
Supported boards: STM32U5G9J-DK1.
29+
30+
.. zephyr-app-commands::
31+
:zephyr-app: samples/sensor/stts22h/
32+
:board: stm32u5g9j_dk1
33+
:goals: build flash
34+
35+
36+
Sample Output
37+
=============
38+
39+
.. code-block:: console
40+
41+
Zephyr STTS22H sensor sample. Board: stm32u5g9j_dk1
42+
[ 10 ms] Temperature: 27.7 C
43+
[ 2013 ms] Temperature: 27.6 C
44+
[ 4016 ms] Temperature: 27.6 C
45+
[ 6019 ms] Temperature: 27.6 C
46+
47+
<repeats endlessly every 2 seconds>

samples/sensor/stts22h/prj.conf

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

samples/sensor/stts22h/sample.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sample:
2+
name: STTS22H temperature sensor
3+
tests:
4+
sample.sensor.stts22h:
5+
depends_on:
6+
- i2c
7+
tags:
8+
- sensors
9+
integration_platforms:
10+
- sensortile_box_pro
11+
- steval_stwinbx1
12+
- stm32u5g9j_dk1

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)