Sensor Reading Example - Zephyr-rtos Typing CST Test
Loading…
Sensor Reading Example — Zephyr-rtos Code
Read data from a sensor and print values periodically.
#include <zephyr.h>
#include <device.h>
#include <drivers/sensor.h>
void main(void)
{
struct device *dev = device_get_binding("SENSOR_0");
struct sensor_value val;
while(1) {
sensor_sample_fetch(dev);
sensor_channel_get(dev, SENSOR_CHAN_ALL, &val);
printk("Sensor value: %d\n", val.val1);
k_sleep(K_SECONDS(2));
}
}Zephyr-rtos Language Guide
Zephyr RTOS is a scalable, open-source real-time operating system designed for resource-constrained embedded devices. It provides a small, configurable kernel, drivers, and networking stacks to enable IoT, wearable, and sensor-based applications with predictable timing and low memory footprint.
Primary Use Cases
- ▸Real-time task scheduling for embedded applications
- ▸Low-power IoT devices and wearables
- ▸Sensor data acquisition and processing
- ▸Networking-enabled devices with MQTT, CoAP, or BLE
- ▸Industrial automation and edge computing
Notable Features
- ▸Modular and configurable kernel
- ▸Support for multiple architectures and boards
- ▸Extensive device driver library
- ▸Real-time thread management and synchronization
- ▸Networking stack including Bluetooth, IPv4/IPv6, and 6LoWPAN
Origin & Creator
Initially developed by Wind River and now governed by the Linux Foundation, Zephyr was created to provide a lightweight, open-source RTOS for embedded devices and IoT applications.
Industrial Note
Critical for embedded developers who require a deterministic, low-memory RTOS for safety-critical, battery-powered, or connected devices, enabling rapid development and robust device behavior.
Quick Explain
- ▸Zephyr RTOS provides real-time task scheduling and deterministic performance for embedded systems.
- ▸It supports multi-threading, inter-thread communication, and hardware abstraction.
- ▸Used in IoT devices, wearables, industrial sensors, and edge computing applications.
- ▸Offers modular components including networking, file systems, and device drivers.
- ▸Supports a wide range of architectures and platforms including ARM, RISC-V, x86, and more.
Core Features
- ▸Preemptive and cooperative multitasking
- ▸Inter-thread communication (semaphores, queues, pipes)
- ▸Timers, work queues, and delayed execution
- ▸Memory management and heap management
- ▸Hardware abstraction layer for portability
Learning Path
- ▸Learn embedded C programming
- ▸Understand real-time OS concepts
- ▸Explore Zephyr kernel APIs and threading
- ▸Practice peripheral interfacing and drivers
- ▸Build complex IoT applications with Zephyr
Practical Examples
- ▸Blinking LED and reading GPIO buttons
- ▸Sensor data acquisition and processing in threads
- ▸BLE-based wearable communication
- ▸MQTT client sending sensor data to cloud
- ▸Low-power sleep modes with scheduled wake-ups
Comparisons
- ▸Zephyr vs FreeRTOS: Zephyr has modular features and POSIX APIs
- ▸Zephyr vs RIOT OS: Zephyr supports more architectures and enterprise backing
- ▸Zephyr vs Mbed OS: Zephyr has smaller footprint and strong open-source community
- ▸Zephyr vs Linux (embedded): Linux heavier, non-deterministic for hard real-time
- ▸Zephyr vs ThreadX: Zephyr open-source, ThreadX is commercial
Strengths
- ▸Small memory footprint suitable for constrained devices
- ▸Highly configurable and modular to optimize resource usage
- ▸Strong community and open-source ecosystem
- ▸Supports real-time deterministic behavior
- ▸Cross-platform portability across multiple MCUs
Limitations
- ▸Limited to embedded and resource-constrained platforms
- ▸Complex for beginners without RTOS experience
- ▸Networking and advanced features require configuration knowledge
- ▸Debugging multi-threaded real-time applications can be challenging
- ▸Smaller ecosystem compared to Linux or FreeRTOS in certain areas
When NOT to Use
- ▸For applications requiring full-fledged OS features
- ▸If hardware has abundant memory and Linux is preferred
- ▸When deterministic real-time is not required
- ▸For extremely simple, single-threaded microcontroller apps
- ▸If team lacks embedded RTOS expertise
Cheat Sheet
- ▸K_THREAD_DEFINE(name, stack_size, entry_fn, ...) - define thread
- ▸k_sleep(ms) - delay thread execution
- ▸k_sem_init/ k_sem_take/ k_sem_give - semaphore usage
- ▸k_fifo_put / k_fifo_get - FIFO communication
- ▸DEVICE_DT_GET(node) - access device from device tree
FAQ
- ▸Does Zephyr support multiple architectures? -> Yes, including ARM, RISC-V, x86.
- ▸Can Zephyr be used for battery-powered devices? -> Yes, supports low-power modes.
- ▸Is Zephyr open-source? -> Yes, governed by the Linux Foundation.
- ▸Does Zephyr have networking support? -> Yes, includes IPv4/IPv6, BLE, MQTT, CoAP.
- ▸Is Zephyr suitable for industrial applications? -> Yes, used in safety-critical and IoT devices.
30-Day Skill Plan
- ▸Week 1: RTOS basics and threading in Zephyr
- ▸Week 2: GPIO, UART, and sensor integration
- ▸Week 3: Multi-threaded applications and synchronization
- ▸Week 4: Networking and IoT protocol integration
- ▸Week 5: Optimization, debugging, and deployment
Final Summary
- ▸Zephyr RTOS is a lightweight, real-time OS for embedded and IoT devices.
- ▸Provides modular kernel, multi-threading, and peripheral support.
- ▸Supports networking, low-power operation, and cross-platform deployment.
- ▸Ideal for deterministic and scalable embedded applications.
- ▸Backed by open-source community with active ecosystem and tooling.
Project Structure
- ▸Zephyr project directory with source and include files
- ▸CMakeLists.txt for build configuration
- ▸prj.conf for kernel and feature settings
- ▸boards/ directory with board definitions
- ▸modules/ directory for optional subsystems
Monetization
- ▸Consulting for embedded IoT systems
- ▸Custom Zephyr firmware development
- ▸Training in real-time embedded development
- ▸Industrial sensor and automation solutions
- ▸Edge device deployment services
Productivity Tips
- ▸Reuse existing Zephyr modules and sample apps
- ▸Leverage device tree for peripheral management
- ▸Use west for dependency management and builds
- ▸Enable only necessary kernel features
- ▸Profile and optimize threads and memory usage
Basic Concepts
- ▸Thread - basic execution unit with priority and stack
- ▸Scheduler - manages thread execution order
- ▸Kernel Objects - semaphores, mutexes, queues for synchronization
- ▸Device Driver - interface to hardware peripherals
- ▸Work Queue - deferred task execution mechanism
Official Docs
- ▸https://www.zephyrproject.org/documentation/
- ▸Zephyr API and Kernel Guides
- ▸Board and peripheral support documentation