Read Data from the VL53L0X ToF Sensor Mounted on the Zumo 32U4

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.

Another item to note is that the datasheet does not specify the beamwidth of the sensor system (combined VCSEL and detection APD).  However, one of the prints shows a 25 degree cone.  This would make it not usable for a robot close to the ground like a Zumo.  I took some measurements and I’m seeing about 3-4 degrees, which is quite narrow.
This entry was posted in Robotics and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *