Page 1 of 1
VL53L1X Example code?
Posted: Thu Aug 20, 2020 4:25 pm
by ZanderSTM
Hi guys,
Anyone have some example code using the
STM32duino_VL53L1X library? I'm new to programming and am having difficulty making sense of the .CPP and .H files.
Thanks!
Re: VL53L1X Example code?
Posted: Thu Aug 20, 2020 4:33 pm
by fpiSTM
Hi,
you can have a look at this library which use it:
https://github.com/stm32duino/X-NUCLEO-53L1A1
Re: VL53L1X Example code?
Posted: Mon Aug 31, 2020 4:58 pm
by ZanderSTM
Thanks, I managed to figure it out. For future reference the code below is a simple setup with one VL53L1X (based off the example given in the X-NUCLEO-53L1A1 libaray):
Code: Select all
#include <Wire.h>
#include <ComponentObject.h>
#include <RangeSensor.h>
#include <vl53l1x_class.h>
#include <vl53l1_error_codes.h>
VL53L1X *sensor1;
#define DEV_I2C Wire
void setup() {
Serial.begin(115200);
Serial.println("Starting...");
Wire.begin();
sensor1 = new VL53L1X(&DEV_I2C, PA12, PA15);
sensor1 ->VL53L1_Off();
sensor1 -> InitSensor(0x10);
}
void loop() {
int status;
uint8_t ready = 0;
uint16_t distance;
sensor1 -> VL53L1X_StartRanging();
do {
sensor1 -> VL53L1X_CheckForDataReady(&ready);
} while (!ready);
status = sensor1 -> VL53L1X_GetDistance(&distance);
if (status == VL53L1_ERROR_NONE) {
Serial.print(0); // To freeze the lower limit
Serial.print(" ");
Serial.print(4000); // To freeze the upper limit
Serial.print(" ");
char report[64];
snprintf(report, sizeof(report), "Distace top [mm]: %d ", distance);
Serial.println(report);
}
status = sensor1 -> VL53L1X_ClearInterrupt();
}
Cheers, ZanderSTM