Hi. Background: Using Uno, Adafruit vl53l1x, and Adafruit's library for the vl53l1x. I modified that program to send a beep if TOF is within a range. That's working ok. However, when tested outdoors, it will send spurious beeps when pointed to a distance object or the sky.
Are spurious signals normal? Or do I have a defective VL53L1X?
I tested changing vl53.setTimingBudget() without any improvement. Even added a "trap" to compare the current distance to the prior distance.
Here is my code:
/* Version D
2021-11-7
Adapted from program made by Adafruit
Added buzzer on start up
Added buzzer if Distance is within a range.
>200 mm so does not measure too close
and <1200 mm appoximate 4 feet
TEST: detected through car window. but spurious beeps, including when pointed to empty sky.
11/13/2021
Added line to set distance variable to 0, before new ping.
Changed: vl53.setTimingBudget();//originally was 50
11/16/2021
To stop spurious, added spurcount counter to only beep if >1
Change logic to simply compare old and new distances.
Added a range of values to the old distance.
PINS:
VL53L1X:
VIN Voltage In = RED = Arduin 5V
GRN ground = BLACK = Arduino GRN
SDA = BLUE = Arduino A4
SCL = YELLOW = Arduino A5
SPEAKER Arduino Pin 5 and Grn
*/
#include "Adafruit_VL53L1X.h"
#define IRQ_PIN 2
#define XSHUT_PIN 3
const int Speaker = 5; // speaker-buzzer is connected to pin 5
// uses Counter= spur_count to prevent single spurious signal triggering a beep
int spur_count = 0; // used to trap one-time spurious signals
int spur_past = 0; // prior distance measurement
Adafruit_VL53L1X vl53 = Adafruit_VL53L1X(XSHUT_PIN, IRQ_PIN);
void setup() {
// Beep on startup to tell driver that Lidar is turned on
tone(Speaker,1500); // 1000 = 1 KHz sound
delay(500); // fyi500 = 0.5 seconds
noTone(Speaker); // stop sound
tone(Speaker,500); // 1000 = 1 KHz sound
delay(800); // 500 = 0.5 seconds
noTone(Speaker); // stop sound
Serial.begin(9600); // originally Serial.begin(115200);
while (!Serial) delay(10);
Serial.println(F("Adafruit VL53L1X sensor demo"));
Wire.begin();
if (! vl53.begin(0x29, &Wire)) {
Serial.print(F("Error on init of VL sensor: "));
Serial.println(vl53.vl_status);
while (1) delay(10);
}
Serial.println(F("VL53L1X sensor OK!"));
Serial.print(F("Sensor ID: 0x"));
Serial.println(vl53.sensorID(), HEX);
if (! vl53.startRanging()) {
Serial.print(F("Couldn't start ranging: "));
Serial.println(vl53.vl_status);
while (1) delay(10);
}
Serial.println(F("Ranging started"));
vl53.setTimingBudget(100);//original program from Adafruit = 50 & said valid numbers are: 15, 20, 33, 50, 100, 200 and 500ms
Serial.print(F("Timing budget (ms): "));
Serial.println(vl53.getTimingBudget());
/*
vl.VL53L1X_SetDistanceThreshold(100, 300, 3, 1);
vl.VL53L1X_SetInterruptPolarity(0);
*/
}
void loop() {
int16_t distance;
if (vl53.dataReady()) {
distance =0; // added: set to zero to reduce spurious beeps
// new measurement for the taking!
distance = vl53.distance();
if (distance == -1) {
// something went wrong!
Serial.print(F("Couldn't get distance: "));
Serial.println(vl53.vl_status);
return;
}
// For testing...
Serial.print(F("Distance: "));
Serial.print(distance);
Serial.println(" mm");
Serial.print(F("Past distance: "));
Serial.print(spur_past);
Serial.println(" mm");
if (distance >= 300 and distance <1200){ // the range for warnings. mm=roughly 1 to 4 feet.
//Spurious trap: if new distance is near to old past,
if ((distance > spur_past - 20) and (distance < spur_past + 20)) {
spur_count = spur_count+ 1;
Serial.print(F("spur_count: "));
Serial.print(spur_count);
Serial.print("_______________");
}
// if (spur_count> 1){ // spurious trap: must be prior signal with similar distance
tone(Speaker,1200); // 1100 = 1 KHz sound
delay(100); // sets duration of tone. fyi 1000 = 1 seconds
noTone(Speaker); // stop sound
spur_count = 0;
// }
}
spur_past = (distance); // Spurious: store for next comparison
}
// data is read out, time for another reading!
vl53.clearInterrupt();
}