Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-Based Ultrasonic Water Level Indicator with Multicolor LED Feedback

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Ultrasonic Water Level Indicator with Multicolor LED Feedback

Circuit Documentation

Summary of the Circuit

This circuit is designed to measure the level of water in a tank using an ultrasonic sensor and provide visual feedback through a series of LEDs. The Arduino UNO serves as the central processing unit, controlling the ultrasonic sensor and LEDs. The ultrasonic sensor measures the distance to the water surface, and based on this distance, the Arduino activates different LEDs to indicate the water level. The circuit is powered by a 2x 18650 battery pack.

Component List

Arduino UNO

Ultrasonic Sensor

2x 18650 Battery Pack

LED: Two Pin (white)

LED: Two Pin (red)

LED: Two Pin (blue)

LED: Two Pin (green)

Wiring Details

Arduino UNO

Ultrasonic Sensor

2x 18650 Battery Pack

LEDs (White, Red, Blue, Green)

Documented Code

// Define ultrasonic sensor pins
const int ultraTrigPin = 9;
const int ultraEchoPin = A0;

// Define variables
long duration;
int distance;

// LED light pins
int redBulb = 2;
int yellowBulb = 3;
int whiteBulb = 4;

// Motor pin
int waterMotor = 7;

void setup() {
  // Start serial communication
  Serial.begin(9600);

  // Set up the pins for the ultrasonic sensor
  pinMode(ultraTrigPin, OUTPUT);
  pinMode(ultraEchoPin, INPUT);
  
  // Set up LED bulb pins
  pinMode(redBulb, OUTPUT);
  pinMode(yellowBulb, OUTPUT);
  pinMode(whiteBulb, OUTPUT);

  // Set up motor pin
  pinMode(waterMotor, OUTPUT);
}

void loop() {
  distance = calculate();

  // Control LEDs and motor based on the distance
  if (distance <= 5) {
    // Tank is full
    //LED indication
    //LED placed vertically to the TANK to know the measurement of water
    digitalWrite(redBulb, HIGH);//at top of the tank
    digitalWrite(yellowBulb, HIGH);//middle of the tank
    digitalWrite(whiteBulb, HIGH);//bottom of the tank
    //Motor at rest
    digitalWrite(waterMotor, LOW); // Stop filling the tank(motor stopped)
    Serial.println("Tank 100% filled");
  } 
  else if (distance <= 20) {
    // Tank is partially filled (below 20 cm)
    digitalWrite(redBulb, LOW);
    digitalWrite(yellowBulb, HIGH);
    digitalWrite(whiteBulb, HIGH);
    digitalWrite(waterMotor, HIGH); // Continues filling the tank
    Serial.println("Tank 50% filled");
  } 
  else if (distance <= 30) {
    // Tank is low (between 20 cm and 30 cm)
    digitalWrite(redBulb, LOW);
    digitalWrite(yellowBulb, LOW);
    digitalWrite(whiteBulb, HIGH);
    digitalWrite(waterMotor, HIGH); // Continue filling the tank
    Serial.println("TANK is empty");
  } 
  else {
    // Tank is empty (greater than 30 cm)
    digitalWrite(redBulb, LOW);
    digitalWrite(yellowBulb, LOW);
    digitalWrite(whiteBulb, LOW);
    digitalWrite(waterMotor, HIGH); // Start filling the tank
    Serial.println("TANK is empty - filling started");
  }

  delay(1000); // Delay to reduce the frequency of updates
}

int calculate() {
  // Clear the TRIG pin
  digitalWrite(ultraTrigPin, LOW);
  delayMicroseconds(2);

  // Send a pulse to the TRIG pin
  digitalWrite(ultraTrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultraTrigPin, LOW);

  // Read the ECHO pin
  duration = pulseIn(ultraEchoPin, HIGH);

  // Calculate distance in cm
  distance = (duration / 2) / 29.1;

  return distance;
}

This code is responsible for reading the distance from the ultrasonic sensor and controlling the LEDs and a motor based on the measured distance. The comments within the code provide additional context and explanation for each section of the code.