Good morning, afternoon or evening!
I am using the VL53L0X sensor in my measurement related TCC.
I'm carrying out some tests, but I couldn't get precision with the sensor, apparently after 100mm it loses precision and sometimes in measurements smaller than that there are variations.
I installed the following code:
#include "Adafruit_VL53L0X.h"
const byte VL53LOX_InterruptPin = 6;
const byte VL53LOX_ShutdownPin = 9;
volatile byte VL53LOX_State = LOW;
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
//-------- PINOS UNO -----//
// SDA ADC4
// SCL ADC5
const int numSamples = 10; // Número de amostras para o filtro de média móvel
int distances[numSamples]; // Array para armazenar as amostras
void setup() {
//Serial.begin(115200);
Serial.begin(9600);
// wait until serial port opens for native USB devices
while (!Serial) {
delay(1);
}
Serial.println(F("VL53L0X API Interrupt Ranging example\n\n"));
pinMode(VL53LOX_ShutdownPin, INPUT_PULLUP);
pinMode(VL53LOX_InterruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(VL53LOX_InterruptPin), VL53LOXISR,
CHANGE);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
// if lox.begin failes its becasue it was a warm boot and the VL53LOX is in
// continues mesurement mode we can use an IO pin to reset the device in case
// we get stuck in this mode
while (!lox.begin()) {
Serial.println(F("Failed to boot VL53L0X"));
Serial.println("Adafruit VL53L0X XShut set Low to Force HW Reset");
digitalWrite(VL53LOX_ShutdownPin, LOW);
delay(100);
digitalWrite(VL53LOX_ShutdownPin, HIGH);
Serial.println("Adafruit VL53L0X XShut set high to Allow Boot");
delay(100);
}
Serial.println("Set GPIO Config so if range is lower the LowThreshold "
"trigger Gpio Pin ");
lox.setGpioConfig(VL53L0X_DEVICEMODE_CONTINUOUS_RANGING,
VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW,
VL53L0X_INTERRUPTPOLARITY_LOW);
// Set Interrupt Treashholds
// Low reading set to 50mm High Set to 100mm
FixPoint1616_t LowThreashHold = (50 * 65536.0);
FixPoint1616_t HighThreashHold = (100 * 65536.0);
Serial.println("Set Interrupt Threasholds... ");
lox.setInterruptThresholds(LowThreashHold, HighThreashHold, true);
// Enable Continous Measurement Mode
Serial.println("Set Mode VL53L0X_DEVICEMODE_CONTINUOUS_RANGING... ");
lox.setDeviceMode(VL53L0X_DEVICEMODE_CONTINUOUS_RANGING, false);
Serial.println("StartMeasurement... ");
lox.startMeasurement();
}//---FINAL SETUP
void VL53LOXISR() {
// Read if we are high or low
VL53LOX_State = digitalRead(VL53LOX_InterruptPin);
// set the built in LED to reflect in range on for Out of range off for in
// range
digitalWrite(LED_BUILTIN, VL53LOX_State);
}
//---------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------//
void loop() {
if (VL53LOX_State == LOW) {
VL53L0X_RangingMeasurementData_t measure;
lox.getRangingMeasurement(
&measure, false); // pass in 'true' to get debug data printout!
lox.clearInterruptMask(false);
////////////////////////////////////////////////////////////////////////////////////////////
// Adicionar nova amostra no início do array
for (int i = numSamples - 1; i > 0; i--) {
distances[i] = distances[i - 1];
}
// Armazenar a nova medição na posição 0 do array
distances[0] = (measure.RangeMilliMeter);
// Calcular a média das amostras
int sum = 0;
for (int i = 0; i < numSamples; i++) {
sum += distances[i];
}
int averageDistance = sum / numSamples;
// Serial.print("Distancia suavizada: ");
Serial.println(averageDistance / 1 + (averageDistance % 100) * 0.01, 2); // Exibir em mm com duas casas decimais
//Serial.print("Distance (mm): ");
//Serial.println(averageDistance); // Exibir em mm com duas casas decimais
//Serial.println(" mm");
delay(80);
} else {
delay(1);
}
}//-------------//
//----------------------------//
Please, can anyone tell me to what extent this sensor can return accuracy, in this case the idea would be to measure pieces in up to a maximum of 30cm, I would position the sensor on a base where it would be at the top and the piece at the base bottom, returning me the height of the object.
I tried to implement two decimal places and make a moving average filter, but it's still not giving me a convincing result.