Skip to content

Instantly share code, notes, and snippets.

@PleahMaCaka
Last active June 20, 2025 08:34
Show Gist options
  • Save PleahMaCaka/096ece7634830f3bd4bf82104a787d7f to your computer and use it in GitHub Desktop.
Save PleahMaCaka/096ece7634830f3bd4bf82104a787d7f to your computer and use it in GitHub Desktop.
Zephyr scd4x measure and print sampling data
west flash
-- west flash: rebuilding
ninja: no work to do.
-- west flash: using runner stm32cubeprogrammer
-------------------------------------------------------------------
STM32CubeProgrammer v2.19.0
-------------------------------------------------------------------
ST-LINK SN : 066FFF554869774867243943
ST-LINK FW : V2J33M25
Board : NUCLEO-F411RE
Voltage : 3.25V
SWD freq : 4000 KHz
Connect mode: Under Reset
Reset mode : Hardware reset
Device ID : 0x431
Revision ID : Rev A
Device name : STM32F411xC/E
Flash size : 512 KBytes
Device type : MCU
Device CPU : Cortex-M4
BL Version : 0xD0
Opening and parsing file: zephyr.hex
Memory Programming ...
File : zephyr.hex
Size : 52.21 KB
Address : 0x08000000
Erasing memory corresponding to sector 0:
Erasing internal memory sectors [0 3]
Download in Progress:
[==================================================] 100%
File download complete
Time elapsed during download operation: 00:00:01.341
RUNNING Program ...
Address: : 0x8000000
Application is running, Please Hold on...
Start operation achieved successfully
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/sys/printk.h>
int main(void)
{
const struct device *scd41 = DEVICE_DT_GET(DT_NODELABEL(scd4x));
if (!device_is_ready(scd41)) {
printk("SCD41 device not ready\n");
return 1;
}
struct sensor_value co2, temp, humidity;
while (1) {
if (sensor_sample_fetch(scd41) < 0) {
printk("Failed to fetch sample from SCD41\n");
k_sleep(K_SECONDS(1));
continue;
}
if (sensor_channel_get(scd41, SENSOR_CHAN_CO2, &co2) < 0 ||
sensor_channel_get(scd41, SENSOR_CHAN_AMBIENT_TEMP, &temp) < 0 ||
sensor_channel_get(scd41, SENSOR_CHAN_HUMIDITY, &humidity) < 0) {
printk("Failed to get SCD41 channels\n");
k_sleep(K_SECONDS(1));
continue;
}
printk("scd41: CO2: %d ppm, Temp: %.2f C, Humidity: %.2f %%\n",
co2.val1,
sensor_value_to_float(&temp),
sensor_value_to_float(&humidity));
k_sleep(K_SECONDS(2));
}
return 0;
}
CONFIG_SHELL=y
CONFIG_SHELL_BACKEND_SERIAL=y
CONFIG_SHELL_HISTORY=y
CONFIG_SHELL_CMDS_RESIZE=y
CONFIG_I2C=y
CONFIG_I2C_SHELL=y
CONFIG_SENSOR=y
CONFIG_SCD4X=y
&i2c1 {
status = "okay";
scd4x: scd4x@62 {
status = "okay";
compatible = "sensirion,scd41";
reg = <0x62>;
mode = <0>;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment