-
Notifications
You must be signed in to change notification settings - Fork 8.2k
samples: sensor: stts22h: Minimal polling sample #98739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
CharlesDias
wants to merge
1
commit into
zephyrproject-rtos:main
Choose a base branch
from
CharlesDias:sample_sensor_stts22h
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
JarmouniA marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - stm32u5g9j_dk1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.