In this post, we will write a short program to read the data from the VL53L0X and display the data to the Serial Monitor. As usual, we will need the Zumo32U4 library, but we will also need the I2C library (Wire.h) and the VL53L0X library (VL53L0X.h). Wire should be built included with the Arduino distribution. However, you will probably need to download and install VL53L0X from the Manage Libraries menu. In addition to including the libraries, we need to create an instance of the time of flight sensor, similar to how we do the same for the Zumo buzzer, buttons, motors, etc. The code for the libraries and creating an instance of the ToF sensor object are shown below:
#include <Zumo32U4.h>
#include <Wire.h>
#include <VL53L0X.h>
VL53L0X tofsensor;
Zumo32U4ButtonA buttonA;
Zumo32U4Buzzer buzzer;
Within the setup(), we need to initialize a few things. The first thing we need to do is to initialize the I2C bus. Without telling the Zumo that we are going to use the I2C bus, the microcontroller won’t know how to communicate with the sensor. This is simply done with:
Wire.begin();
The next step is to initialize the sensor, and tell the sensor that we want to use it in continuous operation (sensor stays active and doesn’t restart and calibrate every measurement).
tofsensor.init();
tofsensor.startContinuous();
At this point, the sensor is ready and we just need to issue the command to take the measurement with:
tofsensor.readRangeContinuousMillimeters();
Putting everything together and outputting the data to the serial monitor:
#include <Zumo32U4.h>
#include <Wire.h>
#include <VL53L0X.h>
VL53L0X tofsensor;
Zumo32U4ButtonA buttonA;
Zumo32U4Buzzer buzzer;
void setup()
{
Serial.begin(115200);
Wire.begin();
tofsensor.init();
buttonA.waitForButton();
buzzer.playFrequency(440, 200, 15);
tofsensor.startContinuous();
}
void loop()
{
int distance;
distance=tofsensor.readRangeContinuousMillimeters();
Serial.println(distance);
delay(20);
}
Note that this uses the default settings for the ToF sensor. There are a lot of settings that can be tweaked to either maximize range, accuracy, or measurement time. In default mode, I get about 800 mm reliably with consistent accuracy and 33 ms read times. I’ll try to write a post soon regarding the various options you can choose to configure the sensor.
I’m starting my tests with this sensor and got good measurement results up to 1250mm, however, for distances above this value the result is always the same: 8190mm. I think I should tinker with the sensor.setTimeout (); and sensor.startContinuous (); would you have any suggestion of the values to be placed and if this would solve my problem? I’m using the library . See my program:
#include
#include
VL53L0X sensor;
long DISTANCIA =0;
void setup()
{
Serial.begin(9600);
Wire.begin();
sensor.init();
sensor.setTimeout(500);
sensor.startContinuous(100);
}
void loop ()
{
DISTANCIA = sensor.readRangeContinuousMillimeters();
Serial.print(“DISTANCIA = “);
Serial.print(DISTANCIA);
Serial.println(” mm”);
delay(100);
}
It’s been a while since I looked at this sensor. I’ll try to look at it over the next couple of days. If I recall, there are some settings with the pulse period that the VCSL transmits that can improve the range. What type of target are you attempting to detect?
Thank you for your attention, my friend. I did a test here in LONG_RANGE mode and I’m getting better answers. I think I solved the problems. I have not tested changes in Vcsel but I have seen that you have options for changes. I was a long time away from programming and I’m a bit slow, but I really appreciate the attention. ah, could you tell the meaning of MCPS and PCLKs?