Rohan Barnwal
Published © GPL3+

Build a Smart Ultrasonic Distance Scale with Arduino

Build a Smart Distance Scale using Arduino Nano, OLED & Ultrasonic Sensor - perfect for DIY, automation & prototyping with JUSTWAY!

BeginnerFull instructions provided20
Build a Smart Ultrasonic Distance Scale with Arduino

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Grove - OLED Display 1.12'' V2
Seeed Studio Grove - OLED Display 1.12'' V2
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code

C/C++
// Ultrasonic → SH110x OLED (I2C) Demo
// Works with SH1106/SH1107 128x64 modules using Adafruit SH110X + GFX

#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <Wire.h>

// ===== OLED CONFIG =====
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C          // Common: 0x3C (sometimes 0x3D)

// For SH1106 (most 128x64 I2C modules):
Adafruit_SH1106G display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// If your panel is SH1107, use:
// Adafruit_SH1107 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);

// ===== ULTRASONIC CONFIG =====
// HC-SR04 pins
const int PIN_TRIG = 9;
const int PIN_ECHO = 10;

// Measurement params
const unsigned long ECHO_TIMEOUT_US = 30000UL;  // ~5m max (sound one-way ~0.0343 cm/us)
const uint8_t SAMPLES = 5;                      // simple moving average

// ===== HELPERS =====
float measureDistanceCm() {
  // Trigger a 10 µs pulse
  digitalWrite(PIN_TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(PIN_TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(PIN_TRIG, LOW);

  // Read echo pulse width
  unsigned long duration = pulseIn(PIN_ECHO, HIGH, ECHO_TIMEOUT_US);
  if (duration == 0) return NAN; // timeout/no object

  // distance = (duration_us * speed_of_sound_cm_per_us) / 2
  // speed of sound ≈ 0.0343 cm/us at ~20°C
  return (duration * 0.0343f) / 2.0f;
}

float cmToIn(float cm) {
  return cm * 0.393700787f;
}

float avgDistanceCm(uint8_t n = SAMPLES) {
  float sum = 0;
  uint8_t good = 0;
  for (uint8_t i = 0; i < n; i++) {
    float d = measureDistanceCm();
    if (!isnan(d)) { sum += d; good++; }
    delay(20);
  }
  if (good == 0) return NAN;
  return sum / good;
}

// Draws a horizontal bar (0–maxVal) across the width
void drawBar(float value, float maxVal) {
  if (isnan(value) || maxVal <= 0) return;
  int w = map((int)(value * 100), 0, (int)(maxVal * 100), 0, SCREEN_WIDTH - 4);
  w = constrain(w, 0, SCREEN_WIDTH - 4);
  // outline
  display.drawRect(2, 48, SCREEN_WIDTH - 4, 12, SH110X_WHITE);
  // fill
  if (w > 0) display.fillRect(3, 49, w, 10, SH110X_WHITE);
}

void setup() {
  pinMode(PIN_TRIG, OUTPUT);
  pinMode(PIN_ECHO, INPUT);

  Wire.begin();
  // For some boards, you may need Wire.setClock(400000);

  display.begin(OLED_ADDR, true);  // true = reset
  display.clearDisplay();

  display.setTextColor(SH110X_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println(F("SH110x Ultrasonic"));
  display.println(F("Initializing..."));
  display.display();
  delay(800);
}

void loop() {
  float cm = avgDistanceCm();
  float in_ = cmToIn(cm);

  display.clearDisplay();

  // Title
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print(F("Ultrasonic Distance"));

  if (isnan(cm)) {
    display.setCursor(0, 20);
    display.setTextSize(2);
    display.println(F("No echo"));
    display.setTextSize(1);
    display.setCursor(0, 40);
    display.println(F("Check wiring/aim"));
  } else {
    // Big number (cm)
    display.setTextSize(2);
    display.setCursor(0, 18);
    display.print(cm, 1);
    display.print(F(" cm"));

    // Smaller line (inches)
    display.setTextSize(1);
    display.setCursor(0, 40);
    display.print(in_, 2);
    display.println(F(" in"));

    // Bar graph up to a chosen max (e.g., 200 cm)
    drawBar(cm, 200.0f);
  }

  display.display();
  delay(120);
}

Credits

Rohan Barnwal
32 projects • 35 followers
Rohan Barnwal - maker, hacker, tech enthusiast. I explore new tech & find innovative solutions. See my projects on hackster.io!

Comments