Arduino Nano-Based Automatic Water Level Controller with Ultrasonic Sensor and L298N Motor Driver
View and Edit Project In Cirkit Designer
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
- Description: A small, complete, and breadboard-friendly board based on the ATmega328P.
- Pins: D1/TX, D0/RX, RESET, GND, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11/MOSI, D12/MISO, VIN, 5V, A7, A6, A5, A4, A3, A2, A1, A0, AREF, 3V3, D13/SCK
L298N DC Motor Driver
- Description: A high voltage, high current dual full-bridge driver designed to accept standard TTL logic levels and drive inductive loads such as relays, solenoids, DC and stepping motors.
- Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
HC-SR04 Ultrasonic Sensor
- Description: A sensor used to measure distance by using ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND
Rocker Switch
- Description: A switch that can be toggled to connect or disconnect a circuit.
- Pins: output, input
18650 Li-ion Battery x 2
- Description: Rechargeable lithium-ion batteries commonly used in portable electronics.
- Pins: +, -
Wiring Details
Arduino Nano
- D2 connected to HC-SR04 Ultrasonic Sensor TRIG
- D3 connected to HC-SR04 Ultrasonic Sensor ECHO
- D10 connected to L298N DC Motor Driver IN1
- D11/MOSI connected to L298N DC Motor Driver IN2
- VIN connected to Rocker Switch input
- GND connected to HC-SR04 Ultrasonic Sensor GND
- 5V connected to HC-SR04 Ultrasonic Sensor VCC
L298N DC Motor Driver
- IN1 connected to Arduino Nano D10
- IN2 connected to Arduino Nano D11/MOSI
- 12V connected to Rocker Switch input
- GND connected to 18650 Li-ion Battery x 2 +
HC-SR04 Ultrasonic Sensor
- TRIG connected to Arduino Nano D2
- ECHO connected to Arduino Nano D3
- GND connected to Arduino Nano GND
- VCC connected to Arduino Nano 5V
Rocker Switch
- input connected to Arduino Nano VIN
- input connected to L298N DC Motor Driver 12V
- output connected to 18650 Li-ion Battery x 2 -
18650 Li-ion Battery x 2
- + connected to L298N DC Motor Driver GND
- - connected to Rocker Switch output
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 motor
// 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 water level. The system prints the distance to the serial monitor for debugging purposes.