VL53L1X Example code?

Post here first, or if you can't find a relevant section!
Post Reply
ZanderSTM
Posts: 2
Joined: Thu Aug 20, 2020 3:41 pm

VL53L1X Example code?

Post 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!
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: VL53L1X Example code?

Post by fpiSTM »

Hi,

you can have a look at this library which use it:
https://github.com/stm32duino/X-NUCLEO-53L1A1
ZanderSTM
Posts: 2
Joined: Thu Aug 20, 2020 3:41 pm

Re: VL53L1X Example code?

Post by ZanderSTM »

Hi,

you can have a look at this library which use it:
https://github.com/stm32duino/X-NUCLEO-53L1A1
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
Post Reply

Return to “General discussion”