Hello all,
I am trying to write some code to read the sensor values from two VL53L0X tof sensors, but I'm having some trouble.
My current code:
#include <zephyr/kernel.h> #include <zephyr/device.h> #include <zephyr/drivers/sensor.h> #include <zephyr/sys/printk.h> #include <zephyr/drivers/gpio.h> static const struct device *const leftSensor = DEVICE_DT_GET(DT_NODELABEL(right_st_vl53l0x)); static const struct device *const rightSensor = DEVICE_DT_GET(DT_NODELABEL(left_st_vl53l0x)); int main(void) { struct sensor_value leftSensorValue; struct sensor_value rightSensorValue; int ret; k_sleep(K_MSEC(1000)); if (!device_is_ready(leftSensor)) { printk("Left Sensor: device not ready.\n"); return 0; } if (!device_is_ready(rightSensor)) { printk("Right Sensor: device not ready.\n"); return 0; } while (1) { ret = sensor_sample_fetch(leftSensor); if (ret) { printk("sensor_sample_fetch failed ret %d\n", ret); return 0; } ret = sensor_channel_get(leftSensor, SENSOR_CHAN_PROX, &leftSensorValue); printk("prox is %d\n", leftSensorValue.val1); ret = sensor_channel_get(leftSensor, SENSOR_CHAN_DISTANCE, &leftSensorValue); printf("distance is %.3fm\n", sensor_value_to_double(&leftSensorValue)); k_sleep(K_MSEC(1000)); } return 0; }
My devicetree:
&i2c1 { right_st_vl53l0x: right_st_vl53l0x@29{ compatible = "st,vl53l0x"; status = "okay"; reg = < 0x29 >; xshut-gpios = < &gpio0 15 (GPIO_ACTIVE_LOW)>; }; left_st_vl53l0x: left_st_vl53l0@52{ compatible = "st,vl53l0x"; status = "okay"; reg = < 0x52 >; xshut-gpios = < &gpio0 16 (GPIO_ACTIVE_LOW)>; }; }; &pinctrl { i2c1_default: i2c1_default{ group1 { psels = <NRF_PSEL(TWIM_SDA, 0, 5)>, <NRF_PSEL(TWIM_SCL, 0, 4)>; }; }; i2c1_sleep: i2c1_sleep{ group1 { psels = <NRF_PSEL(TWIM_SDA, 0, 5)>, <NRF_PSEL(TWIM_SCL, 0, 4)>; low-power-enable; }; }; };
And Kconfig:
CONFIG_STDOUT_CONSOLE=y CONFIG_I2C=y CONFIG_GPIO=y CONFIG_SENSOR=y CONFIG_VL53L0X_PROXIMITY_THRESHOLD=100 CONFIG_TFM_SECURE_UART=n CONFIG_TFM_LOG_LEVEL_SILENCE=y CONFIG_VL53L0X_RECONFIGURE_ADDRESS=y #debug #CONFIG_DEBUG=y #CONFIG_LOG=y #CONFIG_SENSOR_LOG_LEVEL_DBG=y CONFIG_CBPRINTF_FP_SUPPORT=y
Whenever I run this code I get a ret error (-19) or sometimes (-5).
Any help is much appreciated!