SKN Craft
Published © MPL-2.0

HydroView: Ultrasonic Water Level Display with MAX7219

HydroView is a water level indicator that uses an HC-SR04 ultrasonic sensor to measure the distance between the water surface and the sensor

BeginnerFull instructions providedOver 1 day38
HydroView: Ultrasonic Water Level Display with MAX7219

Things used in this project

Hardware components

Seeed Studio XIAO ESP32S3 Sense
Seeed Studio XIAO ESP32S3 Sense
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

circuit

Code

code

C/C++
#include <MD_MAX72xx.h>
#include <SPI.h>

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4  // 4 modules of 8x8 = 32 columns

#define DATA_PIN D10
#define CLK_PIN D8
#define CS_PIN D9

#define TRIG_PIN D2
#define ECHO_PIN D3

// Tank dimensions
#define TANK_HEIGHT_CM 12 // distance from sensor to bottom of tank

// Software SPI constructor (lets you use any pins)
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);

void setup() {
  Serial.begin(115200);
  Serial.println("Water Level Indicator Starting...");

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  mx.begin();
  mx.clear();
}

long getDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH, 30000); // Timeout 30ms
  return duration * 0.034 / 2; // cm
}

void displayLevel(int filledColumns) {
  mx.clear();
  for (int col = 0; col < filledColumns; col++) {
    int actualCol = 31 - col; // Reverse fill
    for (int row = 0; row < 8; row++) {
      mx.setPoint(row, actualCol, true);
    }
  }
}

void loop() {
  long distance = getDistance();
  
  Serial.print("Raw Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  if (distance > 0 && distance <= TANK_HEIGHT_CM) {
    int waterDepth = TANK_HEIGHT_CM - distance; // water height from bottom
    int levelCols = map(waterDepth, 0, TANK_HEIGHT_CM, 0, 32);
    
    Serial.print("Water Depth: ");
    Serial.print(waterDepth);
    Serial.print(" cm, Columns: ");
    Serial.println(levelCols);

    displayLevel(levelCols);
  } else {
    Serial.println("Out of range or no water detected.");
    displayLevel(0); // No water or out of range
  }
  delay(500);
}

Credits

SKN Craft
2 projects • 1 follower

Comments