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

Arduino Nano-Based Automatic Water Level Controller with Ultrasonic Sensor and L298N Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino Nano-Based Automatic Water Level Controller with Ultrasonic Sensor and L298N Motor Driver

Circuit Documentation

Summary

This circuit is designed to control a water tank system using an Arduino Nano, an L298N DC motor driver, an HC-SR04 ultrasonic sensor, a rocker switch, and two 18650 Li-ion batteries. The system measures the water level in the tank using the ultrasonic sensor and controls a pump and a valve based on the water level.

Component List

Arduino Nano

L298N DC Motor Driver

HC-SR04 Ultrasonic Sensor

Rocker Switch

18650 Li-ion Battery x 2

Wiring Details

Arduino Nano

L298N DC Motor Driver

HC-SR04 Ultrasonic Sensor

Rocker Switch

18650 Li-ion Battery x 2

Code Documentation

// Define pins for the ultrasonic sensor
const int trigPin = 2;  // Ultrasonic sensor TRIG pin
const int echoPin = 3;  // Ultrasonic sensor ECHO pin

// Define pins for the L298N motor driver
const int pumpIN1 = 11; // L298N IN1 for the pump
const int pumpIN2 = 10; // L298N IN2 for the pump
const int valveIN1 = 9; // L298N IN3 for the valve motor
const int valveIN2 = 8; // L298N IN4 for the valve moto

// Variable to store distance
long duration;
int distance;

// Water level thresholds
const int fullThreshold = 5;   // Distance (in cm) when the tank is full
const int emptyThreshold = 15; // Distance (in cm) when the tank is empty

void setup() {
  // Initialize the serial monitor
  Serial.begin(9600);

  // Initialize the ultrasonic sensor pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Initialize motor driver pins
  pinMode(pumpIN1, OUTPUT);
  pinMode(pumpIN2, OUTPUT);
  pinMode(valveIN1, OUTPUT);
  pinMode(valveIN2, OUTPUT);

  // Start with both the valve and pump off
  digitalWrite(pumpIN1, LOW);
  digitalWrite(pumpIN2, LOW);
  digitalWrite(valveIN1, LOW);
  digitalWrite(valveIN2, LOW);
}

void loop() {
  // Measure the distance
  distance = getDistance();

  // Print the distance for debugging
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Control the valve and pump based on tank level
  if (distance > emptyThreshold) {
    // Tank is empty; open the valve and turn on the pump
    Serial.println("Tank is empty. Opening valve and turning on pump...");
    openValve();           // Open valve
    turnOnPump();          // Turn on pump
  } else if (distance <= fullThreshold) {
    // Tank is full; close the valve and turn off the pump
    Serial.println("Tank is full. Closing valve and turning off pump...");
    closeValve();          // Close valve
    turnOffPump();         // Turn off pump
  }

  // Small delay before the next measurement
  delay(1000);
}

// Function to get the distance from the ultrasonic sensor
int getDistance() {
  // Send a 10-microsecond pulse to the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure the duration of the echo signal
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance in centimeters
  int distance = duration * 0.034 / 2;

  // Return the calculated distance
  return distance;
}

// Function to turn on the pump
void turnOnPump() {
  digitalWrite(pumpIN1, HIGH);
  digitalWrite(pumpIN2, LOW); // Forward motion
}

// Function to turn off the pump
void turnOffPump() {
  digitalWrite(pumpIN1, LOW);
  digitalWrite(pumpIN2, LOW); // Stop motion
}

// Function to open the valve
void openValve() {
  digitalWrite(valveIN1, HIGH);
  digitalWrite(valveIN2, LOW); // Forward motion
  // delay(3000);                // Assume it takes 3 seconds to fully open
  digitalWrite(valveIN1, LOW);
  digitalWrite(valveIN2, LOW); // Stop motion
}

// Function to close the valve
void closeValve() {
  digitalWrite(valveIN1, LOW);
  digitalWrite(valveIN2, HIGH); // Reverse motion
  // delay(3000);                  // Assume it takes 3 seconds to fully close
  digitalWrite(valveIN1, LOW);
  digitalWrite(valveIN2, LOW);  // Stop motion
}

This code initializes the pins for the ultrasonic sensor and the L298N motor driver, measures the distance to the water level, and controls the pump and valve based on the measured distance. The system prints the distance to the serial monitor for debugging purposes.