Vl53L0X Problem

I have to connect four Vl53L0X with an Arduino Uno and read from all the sensors the corresponding measured data. All this is supposed to be done via an I2C bus. This is my previous code:

#include <Wire.h>
#include <VL53L0X.h>
//////////////////////////////////////////////////////////////////////////////////////////
#define XSHUT_pin4 8
#define XSHUT_pin3 7      // Definition of the connection pins 
#define XSHUT_pin2 6
#define XSHUT_pin1 5
//////////////////////////////////////////////////////////////////////////////////////////
#define Sensor2_newAddress 42
#define Sensor3_newAddress 43     // default address: 0b0101001 => 41
#define Sensor4_newAddress 44     // Sensors get new addresses
//////////////////////////////////////////////////////////////////////////////////////////
VL53L0X Sensor1;
VL53L0X Sensor2;
VL53L0X Sensor3;
VL53L0X Sensor4;
//////////////////////////////////////////////////////////////////////////////////////////
void setup() { 

  pinMode(XSHUT_pin1, OUTPUT);
  pinMode(XSHUT_pin2, OUTPUT);      // The pins on the sensors must not be connected to 5V 
  pinMode(XSHUT_pin3, OUTPUT);         
  pinMode(XSHUT_pin4, OUTPUT);
//////////////////////////////////////////////////////////////////////////////////////////
  Serial.begin(9600);             // Start serial communication with PC
//////////////////////////////////////////////////////////////////////////////////////////  
  Wire.begin();
  Sensor4.setAddress(Sensor4_newAddress);
    pinMode(XSHUT_pin3, INPUT);
  delay(10);                                   // Sensors get new addresses
  Sensor3.setAddress(Sensor3_newAddress);
    pinMode(XSHUT_pin2, INPUT);
  delay(10);
  Sensor2.setAddress(Sensor2_newAddress);
    pinMode(XSHUT_pin1, INPUT);
  delay(10);
//////////////////////////////////////////////////////////////////////////////////////////  
  Sensor1.init();
  Sensor2.init();
  Sensor3.init();
  Sensor4.init();
////////////////////////////////////////////////////////////////////////////////////////// 
  Sensor1.setTimeout(500);
  Sensor2.setTimeout(500);
  Sensor3.setTimeout(500);
  Sensor4.setTimeout(500);
//////////////////////////////////////////////////////////////////////////////////////////
  Sensor1.startContinuous();
  Sensor2.startContinuous();      
  Sensor3.startContinuous();
  Sensor4.startContinuous();
//////////////////////////////////////////////////////////////////////////////////////////
pinMode(13, OUTPUT);
}
void loop()
{  
//////////////////////////////////////////////////////////////////////////////////////////
  Serial.print(Sensor1.readRangeContinuousMillimeters());
  Serial.print(',');
  Serial.print(Sensor2.readRangeContinuousMillimeters());
  Serial.print(',');                                        // Reading out the sensors
  Serial.print(Sensor3.readRangeContinuousMillimeters());
  Serial.print(','); 
  Serial.print(Sensor4.readRangeContinuousMillimeters());
  Serial.print(',');
//////////////////////////////////////////////////////////////////////////////////////////
  if (Sensor1.readRangeContinuousMillimeters() <= 100) {
        digitalWrite(13, HIGH);
        delay(200);
        digitalWrite(13, LOW);
        delay(200);
        } else {
        digitalWrite(13, LOW);
        }
   
  if (Sensor2.readRangeContinuousMillimeters() <= 100) {
      digitalWrite(13, HIGH);
      }  else {
      digitalWrite(13, LOW);
      }
      
  if (Sensor3.readRangeContinuousMillimeters() <= 100) {
        digitalWrite(13, HIGH);
        delay(500);
        digitalWrite(13, LOW);
        delay(500);
        } else {
        digitalWrite(13, LOW);
        }
        
  if (Sensor4.readRangeContinuousMillimeters() <= 100) {
        digitalWrite(13, HIGH);
        delay(200);
        digitalWrite(13, LOW);
        delay(200);
        } else {
        digitalWrite(13, LOW);
        }      
       
 } 

The XSHUT pins of the sensors are connected to the Arduino because with each restart (thus also turning on) the sensors must be assigned a new address so that the I2C bus can keep the sensors apart.

The problem is that the code works well so far and the sensors work so far but only up to a certain point. If the distance between the sensor and the wall is too large, the sensors will break off and give a value of between 30 and 70, although the value is actually over 200. This then turns the led on pin 13, but it should only blink if the distance is really below 40 inches. Can you help me pleas its really important?

I wouldn’t expect it to measure 200 inches, since the datasheet says up to 2 meters, so about 78 inches at the most.

https://www.st.com/en/embedded-software/stsw-img005.html

1 Like

The demo shows checking status, and rejecting out-of-range, which with their library is indicated by .RangeStatus == 4

Looks like you are using a different library but it needs to have the same information. Go ahead and provide a link.

1 Like

First of all thank you a lot for your help :slight_smile: but I don’t understand what do you mean with provide a link :sweat_smile:

That would be because it was an incomplete thought. :confused:

I probably intended something like “If you can’t work out how to find the status codes, provide a link to the VL53L0X library you used, since it wasn’t the adafruit version I linked to”

Do read the datasheet, because there are different ranges, and you have to configure it for the sensitivity and range you want if it’s not the default. (I’ve never used these components myself, just glanced through the datasheet after you posted.)

I am using the <VL53L0X.h> library because the normal <Adafruit_VL53L0X.h> is taking too much memory. Here is a link to mriner used library: https://github.com/pololu/vl53l0x-arduino
I’ve never used such components before too, so I need help so badly :grimacing:

Looks like their examples don’t check for errors, unlike the Adafruit one. I’m afraid that this means reading code to find out how to check for errors with that library. The library you linked to says in the readme that it follows the user manual that I linked to, so you’ll really want to read both.

Maybe timeoutOccurred will help?