Using a sensor
In this part, we will use the VL53L0X time-of-flight sensor which can detect a distance using laser-ranging.
Using the board documentation (or the image of the sensor), locate the sensor on the board.
Referencing it as a sensor
Zephyr has a sensor
class of devices. In its simplest mode of operation, the user:
- requests that a measure be initiated;
- then retrieves the result of the measure in channels (for example
SENSOR_CHAN_HUMIDITY
for a humidity sensor).
All values are returned as a pair of integers, avoiding the need for floating point calculations in many cases. For some sensors, only the first value will be filled. The Zephyr documentation on sensors describes those interfaces.
In order to know what channels are available for the vl53l0x driver, locate its source files in Zephyr source tree (starting from $ZEPHYR_BASE
) and look for symbols starting with SENSOR_CHAN_
.
Locate and use the device
In your source code, you might want to use the DEVICE_DT_GET_ANY()
macro in order to locate any vl53l0x sensor on your board. In this case you know there is only one, so you don't have to look for its node label or its alias.
💡 In a device tree, you might encounter names such as
"st,vl53l0x"
: the first part designates the vendor (ST Microelectronics) and the second part designates the device (VL53L0X time of flight sensor). You cannot directory use those names in C macros, as the comma will be understood as a separator. The non-alphanumeric characters must be replaced by an underscore. For example,"st,vl53l0x"
will becomest_vl53l0x
as inDEVICE_DT_GET_ANY(st_vl53l0x)
.
In a loop, print repeatedly (for example every tenth of seconds) the distance between the sensor and the obstacle above it (for example your hand) using the sensor interface.
Note on displaying floating-point values
By default, Zephyr standard C library does not implement printing of floating-point values. If you want to use %f
or %g
formatters in functions of the printf
family, you can request for it to be included, by using:
CONFIG_CBPRINTF_FP_SUPPORT=y
However, it is best not to use floating point if you don't need to.
Make the led blink at a rate depending on the distance
Using several threads and communicating through a Message queue, make one of the green leds blink fast when your hand is near the board and much more slowly when it is far from the board.