Skip to content

Commit dcda808

Browse files
wasilewskiJfabiobaltieri
authored andcommitted
samples: sensor: add sample for SHT4X
Add a new sample specifically for the SHT4X sensor. Signed-off-by: Jakub Wasilewski <jwasilewski@internships.antmicro.com> Signed-off-by: Filip Kokosinski <fkokosinski@antmicro.com>
1 parent 011f2bc commit dcda808

File tree

7 files changed

+217
-0
lines changed

7 files changed

+217
-0
lines changed

samples/sensor/sht4x/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 Antmicro <www.antmicro.com>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(sht4x)
8+
9+
FILE(GLOB app_sources src/*.c)
10+
target_sources(app PRIVATE ${app_sources})

samples/sensor/sht4x/Kconfig

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) 2025 Antmicro <www.antmicro.com>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
mainmenu "SHT4X sample application"
5+
6+
config APP_USE_HEATER
7+
bool "Use the Heater on the SHT4X"
8+
help
9+
Maximum duty cycle for using the heater is 5%
10+
11+
config APP_HEATER_HUMIDITY_THRESH
12+
int "RH [%] threshold above which the heater will be activated"
13+
range 0 99
14+
default 65
15+
depends on APP_USE_HEATER
16+
17+
config APP_HEATER_PULSE_POWER
18+
int "Heater Power Setting"
19+
range 0 2
20+
default 2
21+
depends on APP_USE_HEATER
22+
help
23+
0 -> High power heater pulse -> ~200 mW @3.3V
24+
1 -> Medium power heater pulse -> ~110 mW @3.3V
25+
2 -> Low power heater pulse -> ~20 mW @3.3V
26+
27+
config APP_HEATER_PULSE_DURATION_LONG
28+
bool "Use long pulse duration Heater setting"
29+
default y
30+
depends on APP_USE_HEATER
31+
help
32+
Say 'Y' if you want to use the long pulse duration setting. This sets the pulse time to
33+
1.1s. Say 'N' if you want to use the short pulse duration setting, which sets the pulse
34+
time to 0.11s.
35+
36+
source "Kconfig.zephyr"

samples/sensor/sht4x/README.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
.. _sht4x:
2+
3+
SHT4X: High Accuracy Digital I2C Humidity Sensor
4+
#################################################
5+
6+
Description
7+
***********
8+
9+
This sample application periodically reads the ambient temperature and humidity
10+
from an SHT4X device. The result is written to the console.
11+
12+
The SHT4X sensor has an optional heater that can be useful for specific
13+
environments or applications (refer to the datasheet for more information).
14+
To utilize the heater, check the Kconfig options for this application.
15+
16+
References
17+
**********
18+
19+
- `SHT4X sensor <https://sensirion.com/products/catalog/SHT45>`_
20+
21+
Wiring
22+
******
23+
24+
This sample uses the SHT4X sensor controlled via the I2C interface.
25+
Connect Supply: **VDD**, **GND** and Interface: **SDA**, **SCL**.
26+
The supply voltage can be in the 1.7V to 3.6V range.
27+
Depending on the baseboard used, the **SDA** and **SCL** lines may require Pull-Up
28+
resistors.
29+
30+
Building and Running
31+
********************
32+
33+
This project outputs sensor data to the console. It requires an SHT4X
34+
sensor. It should work with any platform featuring an I2C peripheral
35+
interface. This example includes a device tree overlay
36+
for the :zephyr:board:`blackpill_f411ce` board.
37+
38+
.. zephyr-app-commands::
39+
:zephyr-app: samples/sensor/sht4x
40+
:board: blackpill_f411ce
41+
:goals: build flash
42+
43+
Sample Output
44+
=============
45+
46+
.. code-block:: console
47+
48+
*** Booting Zephyr OS build v2.6.0-rc1-315-g50d8d1187138 ***
49+
SHT4X: 23.64 Temp. [C] ; 30.74 RH [%]
50+
SHT4X: 23.66 Temp. [C] ; 32.16 RH [%]
51+
SHT4X: 23.63 Temp. [C] ; 30.83 RH [%]
52+
53+
The datasheet states that the sensor measures temperature and humidity in degrees Celsius
54+
and percent relative humidity respectively.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2025 Antmicro <www.antmicro.com>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&i2c1 {
8+
status = "okay";
9+
clock-frequency = <I2C_BITRATE_FAST>;
10+
sht4x@44 {
11+
status = "okay";
12+
compatible = "sensirion,sht4x";
13+
reg = <0x44>;
14+
repeatability = <2>;
15+
};
16+
};

samples/sensor/sht4x/prj.conf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2025 Antmicro <www.antmicro.com>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_CBPRINTF_FP_SUPPORT=y
5+
CONFIG_I2C=y
6+
CONFIG_SENSOR=y
7+
CONFIG_PM_DEVICE=y
8+
CONFIG_LOG=y
9+
CONFIG_SENSOR_LOG_LEVEL_DBG=y

samples/sensor/sht4x/sample.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2025 Antmicro <www.antmicro.com>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
sample:
5+
name: SHT4X Sensor Sample
6+
tests:
7+
sample.sensor.sht4x:
8+
build_only: true
9+
platform_allow:
10+
- blackpill_f411ce
11+
- myra_sip_baseboard
12+
tags:
13+
- sensors

samples/sensor/sht4x/src/main.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2025 Antmicro <www.antmicro.com>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/device.h>
9+
#include <zephyr/drivers/sensor.h>
10+
#include <stdio.h>
11+
12+
#include <zephyr/drivers/sensor/sht4x.h>
13+
14+
#if !DT_HAS_COMPAT_STATUS_OKAY(sensirion_sht4x)
15+
#error "No sensirion,sht4x compatible node found in the device tree"
16+
#endif
17+
18+
int main(void)
19+
{
20+
const struct device *const sht = DEVICE_DT_GET_ANY(sensirion_sht4x);
21+
struct sensor_value temp, hum;
22+
23+
if (!device_is_ready(sht)) {
24+
printf("Device %s is not ready.\n", sht->name);
25+
return 0;
26+
}
27+
28+
#if CONFIG_APP_USE_HEATER
29+
struct sensor_value heater_p;
30+
struct sensor_value heater_d;
31+
32+
heater_p.val1 = CONFIG_APP_HEATER_PULSE_POWER;
33+
heater_d.val1 = !!CONFIG_APP_HEATER_PULSE_DURATION_LONG;
34+
sensor_attr_set(sht, SENSOR_CHAN_ALL, SENSOR_ATTR_SHT4X_HEATER_POWER, &heater_p);
35+
sensor_attr_set(sht, SENSOR_CHAN_ALL, SENSOR_ATTR_SHT4X_HEATER_DURATION, &heater_d);
36+
#endif
37+
38+
while (true) {
39+
if (sensor_sample_fetch(sht)) {
40+
printf("Failed to fetch sample from SHT4X device\n");
41+
return 0;
42+
}
43+
44+
sensor_channel_get(sht, SENSOR_CHAN_AMBIENT_TEMP, &temp);
45+
sensor_channel_get(sht, SENSOR_CHAN_HUMIDITY, &hum);
46+
47+
#if CONFIG_APP_USE_HEATER
48+
/*
49+
* Conditions in which it makes sense to activate the heater
50+
* are application/environment specific.
51+
*
52+
* The heater should not be used above SHT4X_HEATER_MAX_TEMP (65 °C)
53+
* as stated in the datasheet.
54+
*
55+
* The temperature data will not be updated here for obvious reasons.
56+
**/
57+
if (hum.val1 > CONFIG_APP_HEATER_HUMIDITY_THRESH &&
58+
temp.val1 < SHT4X_HEATER_MAX_TEMP) {
59+
printf("Activating heater.\n");
60+
61+
if (sht4x_fetch_with_heater(sht)) {
62+
printf("Failed to fetch sample from SHT4X device\n");
63+
return 0;
64+
}
65+
66+
sensor_channel_get(sht, SENSOR_CHAN_HUMIDITY, &hum);
67+
}
68+
#endif
69+
70+
printf("SHT4X: %.2f Temp. [C] ; %0.2f RH [%%]\n", sensor_value_to_double(&temp),
71+
sensor_value_to_double(&hum));
72+
73+
#if CONFIG_APP_USE_HEATER && !CONFIG_APP_HEATER_PULSE_DURATION
74+
k_sleep(K_MSEC(20000));
75+
#else
76+
k_sleep(K_MSEC(2000));
77+
#endif
78+
}
79+
}

0 commit comments

Comments
 (0)