Skip to content

Commit c248ebe

Browse files
psvtrajanabhinavnxp
authored andcommitted
samples: sensor: Add distance measurement sensor sample
Added sample to measure distance Signed-off-by: Thiyagarajan Pandiyan <psvthiyagarajan@gmail.com>
1 parent f8cd882 commit c248ebe

File tree

6 files changed

+136
-0
lines changed

6 files changed

+136
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2025 Thiyagarajan Pandiyan <psvthiyagarajan@gmail.com>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(distance_polling)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.. zephyr:code-sample:: distance_polling
2+
:name: Generic distance measurement
3+
:relevant-api: sensor_interface
4+
5+
Measure distance to an object using a distance sensor
6+
7+
Overview
8+
********
9+
10+
This sample application periodically measures the distance of an object and
11+
display it, via the console.
12+
13+
Building and Running
14+
********************
15+
16+
This sample supports up to 5 distance sensors. Each sensor needs to be aliased
17+
as ``distanceN`` where ``N`` goes from ``0`` to ``4``. For example:
18+
19+
.. code-block:: devicetree
20+
21+
/ {
22+
aliases {
23+
distance0 = &vl53l1x;
24+
};
25+
};
26+
27+
Make sure the aliases are in devicetree, then build and run with:
28+
29+
.. zephyr-app-commands::
30+
:zephyr-app: samples/sensor/distance_polling
31+
:board: <board to use>
32+
:goals: build flash
33+
:compact:
34+
35+
Sample Output
36+
=============
37+
38+
.. code-block:: console
39+
40+
vl53l1x: 0.153m
41+
vl53l1x: 0.154m
42+
vl53l1x: 0.154m
43+
vl53l1x: 0.153m
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2025 Thiyagarajan Pandiyan <psvthiyagarajan@gmail.com>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
aliases {
9+
distance0 = &vl53l1x;
10+
};
11+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_LOG=y
2+
CONFIG_GPIO=y
3+
CONFIG_SENSOR=y
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sample:
2+
name: Distance measurement sample
3+
common:
4+
tags: sensors
5+
harness: console
6+
harness_config:
7+
type: one_line
8+
regex:
9+
- "^[a-zA-Z0-9_,+-.]*: [0-9\\.]+m$"
10+
tests:
11+
sample.sensor.distance_polling:
12+
filter: dt_alias_exists("distance0")
13+
integration_platforms:
14+
- arduino_nicla_vision/stm32h747xx/m7 # vl53l1x
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2025 Thiyagarajan Pandiyan <psvthiyagarajan@gmail.com>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdio.h>
8+
#include <zephyr/kernel.h>
9+
#include <zephyr/device.h>
10+
#include <zephyr/drivers/sensor.h>
11+
12+
#define DISTANCE_ALIAS(i) DT_ALIAS(_CONCAT(distance, i))
13+
#define DISTANCE_SENSOR(i, _) \
14+
IF_ENABLED(DT_NODE_EXISTS(DISTANCE_ALIAS(i)), (DEVICE_DT_GET(DISTANCE_ALIAS(i)),))
15+
16+
/* support up to 5 distance sensors */
17+
static const struct device *const sensors[] = {LISTIFY(5, DISTANCE_SENSOR, ())};
18+
19+
static void fetch_and_display(const struct device *sensor)
20+
{
21+
struct sensor_value distance;
22+
int rc = sensor_sample_fetch(sensor);
23+
24+
if (rc < 0) {
25+
printf("ERROR: Fetch failed: %d\n", rc);
26+
return;
27+
}
28+
29+
rc = sensor_channel_get(sensor, SENSOR_CHAN_DISTANCE, &distance);
30+
if (rc < 0) {
31+
printf("ERROR: get failed: %d\n", rc);
32+
} else {
33+
printf("%s: %d.%03dm\n", sensor->name, distance.val1, distance.val2);
34+
}
35+
}
36+
37+
int main(void)
38+
{
39+
size_t i = 0;
40+
41+
for (i = 0; i < ARRAY_SIZE(sensors); i++) {
42+
if (!device_is_ready(sensors[i])) {
43+
printf("Error: Device \"%s\" is not ready\n", sensors[i]->name);
44+
return 0;
45+
}
46+
}
47+
48+
while (true) {
49+
for (i = 0; i < ARRAY_SIZE(sensors); i++) {
50+
fetch_and_display(sensors[i]);
51+
k_sleep(K_MSEC(500));
52+
}
53+
}
54+
55+
return 0;
56+
}

0 commit comments

Comments
 (0)