【前言】
非常感谢得捷电子与EEPW论坛,让我拥有试用NRF54L15-DK的机会。本文将分享如何使用Zephyr RTOS快速驱动SHT30,采集温湿度计。
【硬件准备】
开发板开发板: nRF54L15-DK (PCA10156)芯片: nRF54L15 (Arm Cortex-M33)传感器型号: SHT30-DIS-B (Sensirion 温湿度传感器)接口: I2C
I2C 地址: 0x44
【硬件连接】
| SHT30 引脚 | nRF54L15-DK 引脚 | 说明 |
| VDD | 外接 3.3V 电源 | 需要外接电源 |
| GND | GND | 地 |
| SDA | P1.12 | I2C 数据线 |
| SCL | P1.11 | I2C 时钟线 |
项目结构
app/sht30_sensor/ ├── CMakeLists.txt ├── prj.conf ├── boards/ │ └── nrf54l15dk_nrf54l15_cpuapp.overlay └── src/ └── main.c1. CMakeLists.txt
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(sht30_sensor)
target_sources(app PRIVATE src/main.c)2. prj.conf 配置
# Enable SENSOR subsystem CONFIG_SENSOR=y # Enable SHT3XD sensor driver CONFIG_SHT3XD=y # Enable I2C CONFIG_I2C=y # Enable GPIO CONFIG_GPIO=y # Enable console for printf CONFIG_CONSOLE=y CONFIG_UART_CONSOLE=y # Enable printf CONFIG_PRINTK=y3. 设备树 Overlay (boards/nrf54l15dk_nrf54l15_cpuapp.overlay)
/ {
aliases {
console = &uart20;
};
};
&uart20 {
status = "okay";
};
&gpio1 {
status = "okay";
};
&gpio2 {
status = "okay";
};
&i2c21 {
status = "okay";
pinctrl-0 = <&i2c21_default>;
pinctrl-1 = <&i2c21_sleep>;
pinctrl-names = "default", "sleep";
sht3xd@44 {
compatible = "sensirion,sht3xd";
reg = <0x44>;
status = "okay";
};
};
&pinctrl {
i2c21_default: i2c21_default {
group1 {
psels = <NRF_PSEL(TWIM_SDA, 1, 12)>,
<NRF_PSEL(TWIM_SCL, 1, 11)>;
bias-pull-up;
};
};
i2c21_sleep: i2c21_sleep {
group1 {
psels = <NRF_PSEL(TWIM_SDA, 1, 12)>,
<NRF_PSEL(TWIM_SCL, 1, 11)>;
low-power-enable;
bias-pull-up;
};
};
};4. 主程序 (main.c)/**
* @brief SHT30 Temperature and Humidity Sensor Driver Sample
*
* Hardware connections:
* - SHT30 VDD -> External 3.3V power
* - SHT30 GND -> GND
* - SHT30 SDA -> P1.12 (I2C SDA)
* - SHT30 SCL -> P1.11 (I2C SCL)
*/
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <stdio.h>
int main(void)
{
const struct device *const dev = DEVICE_DT_GET_ONE(sensirion_sht3xd);
int rc;
if (!device_is_ready(dev)) {
printf("Error: SHT3XD device is not ready\n");
return 0;
}
printf("SHT30 Sensor Driver Initialized\n");
printf("Reading temperature and humidity every 2 seconds...\n\n");
while (true) {
struct sensor_value temp, hum;
rc = sensor_sample_fetch(dev);
if (rc != 0) {
printf("Error: sensor_sample_fetch failed: %d\n", rc);
k_sleep(K_MSEC(1000));
continue;
}
rc = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
if (rc != 0) {
printf("Error: sensor_channel_get(temp) failed: %d\n", rc);
k_sleep(K_MSEC(1000));
continue;
}
rc = sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY, &hum);
if (rc != 0) {
printf("Error: sensor_channel_get(humidity) failed: %d\n", rc);
k_sleep(K_MSEC(1000));
continue;
}
/* Print results - split into integer and fractional parts */
printf("Temperature: %d.%02d C\n", temp.val1, (temp.val2 < 0 ? -temp.val2 : temp.val2) / 10000);
printf("Humidity: %d.%02d %%RH\n", hum.val1, (hum.val2 < 0 ? -hum.val2 : hum.val2) / 10000);
printf("---\n");
k_sleep(K_MSEC(2000));
}
return 0;
}编译与烧录编译
cd D:/zephyr_w71/app/sht30_sensor west build -b nrf54l15dk/nrf54l15/cpuapp烧录
west flash
【实验现象】
打开串口,波特率为115200,可以看到打印来了数据:

【总结】
NORDIC的 nRf54L15 ,作为Zephyr的社区最大的贡献者,生态支持非常的好。只需要开启一些必要的宏开关,指定对应的IO就可以快速的实现补外设的驱动,快速稳定。
我要赚赏金
