FluxBench.AIShared project · read-only

Sensor Duel: Ultrasonic vs Laser

ESP32 DevKit (WROOM-32)·Arduino C++·Updated Jul 27, 2026·by FluxBench TeamIncludes Lab + Schematic

Two distance sensors measure side by side so you can compare how they behave: an HC-SR04 ultrasonic sensor (NewPing library, TRIG GPIO 26 / ECHO GPIO 14) that times a sound echo in whole centimetres, and a VL53L0X time-of-flight laser (I2C) that times light in millimetres. A 128x64 OLED shows both readings, their difference, and a verdict - AGREE, SONIC FAR, or LASER FAR - and the green LED on GPIO 4 lights when the two sensors agree within 5 cm. Click each sensor on the bench and drag its own distance slider while the sketch runs.

sketch.ino
// Sensor Duel: Ultrasonic vs Laser - two ways to measure distance, side by side.
// The HC-SR04 shouts an ultrasonic chirp and times the echo (cheap, wide beam,
// ~1 cm steps). The VL53L0X fires an invisible laser and times the light
// (precise, narrow beam, millimetre steps). Click each sensor on the bench and
// drag its own distance slider - the OLED shows both readings and who reads
// farther, and the green LED lights when the two sensors AGREE within 5 cm.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <NewPing.h>
#include <VL53L0X.h>

#define TRIG_PIN 26
#define ECHO_PIN 14
#define AGREE_LED 4
#define MAX_CM 400

Adafruit_SSD1306 display(128, 64, &Wire, -1);
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_CM);
VL53L0X laser;

void setup() {
  Serial.begin(115200);
  pinMode(AGREE_LED, OUTPUT);
  Wire.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();
  laser.setTimeout(500);
  laser.init();
  laser.startContinuous();
  Serial.println("Sensor duel ready - drag each sensor's distance slider");
}

void loop() {
  int sonicCm = sonar.ping_cm();          // ultrasonic: whole centimetres
  uint16_t mm = laser.readRangeContinuousMillimeters();
  float laserCm = mm / 10.0;              // laser: millimetre resolution

  float diff = sonicCm - laserCm;
  if (diff < 0) diff = -diff;

  const char* verdict;
  if (diff <= 5.0) {
    verdict = "AGREE";
  } else if (sonicCm > laserCm) {
    verdict = "SONIC FAR";
  } else {
    verdict = "LASER FAR";
  }
  digitalWrite(AGREE_LED, diff <= 5.0 ? HIGH : LOW);

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("SONIC  vs  LASER");
  display.setCursor(0, 14);
  display.print("HC-SR04");
  display.setCursor(72, 14);
  display.print("VL53L0X");
  display.setTextSize(2);
  display.setCursor(0, 26);
  display.print(sonicCm);
  display.setCursor(72, 26);
  display.print(laserCm, 1);
  display.setTextSize(1);
  display.setCursor(0, 46);
  display.print("cm           cm");
  display.setCursor(0, 56);
  display.print("diff ");
  display.print(diff, 1);
  display.print(" cm  ");
  display.print(verdict);
  display.display();

  Serial.print("sonic=");
  Serial.print(sonicCm);
  Serial.print(" cm  laser=");
  Serial.print(laserCm, 1);
  Serial.print(" cm  diff=");
  Serial.print(diff, 1);
  Serial.print(" cm  ");
  Serial.println(verdict);
  delay(300);
}

Built with FluxBench.AI — the AI workbench for embedded projects.