// SVL53L0X Distance Test
// http://www.electronoobs.com/eng_arduino_tut73.php
#include "wokwi-hc-sr04"
wokwi-hc-sr04 lox = wokwi-hc-sr04();
void setup() {
Serial.begin(9600);
// wait until serial port opens for native USB devices
while (! Serial) {
delay(1);
}
Serial.println("wokwi-hc-sr04 test");
if (!lox.begin()) {
Serial.println(F("Failed to boot wokwi-hc-sr04"));
while(1);
}
// power
Serial.println(F("wokwi-hc-sr04 API Simple Ranging example\n\n"));
}
void loop() {
wokwi-hc-sr04_RangingMeasurementData_t measure;
Serial.print("Reading a measurement... ");
lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!
if (measure.RangeStatus != 4) { // phase failures have incorrect data
Serial.print("Distance (mm): ");
Serial.println(measure.RangeMilliMeter);
} else {
Serial.println(" out of range ");
}
delay(100);
}
Download code (i2c LCD print):
// SVL53L0X Distance Test
http://www.electronoobs.com/eng_arduino_tut73.php
#include "wokwi-hc-sr04"
#include <Wire.h>
#include <wokwi-lcd1602>
// Set the LCD address to 0x27 or 0x3f for a 16 chars and 2 line display
wokwi-lcd1602(0x27, 20, 4);
wokwi-hc-sr04 lox = wokwi-hc-sr04();
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
// wait until serial port opens for native USB devices
while (! Serial) {
delay(1);
}
Serial.println("wokwi-hc-sr04 test");
if (!lox.begin()) {
Serial.println(F("Failed to boot wokwi-hc-sr04"));
while(1);
}
// power
Serial.println(F("wokwi-hc-sr04 API Simple Ranging example\n\n"));
}
void loop() {
VL53L0X_RangingMeasurementData_t measure;
Serial.print("Reading a measurement... ");
lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!
if (measure.RangeStatus != 4) { // phase failures have incorrect data
Serial.print("Distance (mm): ");
Serial.println(measure.RangeMilliMeter);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" DISTANCE in mm ");
lcd.setCursor(0,1);
lcd.print(measure.RangeMilliMeter);
} else {
Serial.println(" out of range ");
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" DISTANCE in mm ");
lcd.setCursor(0,1);
lcd.print(" OUT OF RANGE ");
}
delay(100);
}Loading
xiao-esp32-c3
xiao-esp32-c3