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 dual H-Bridge motor driver that allows control of two DC motors or one stepper motor.
- 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 that uses sonar to determine the distance to an object.
- Pins: VCC, TRIG, ECHO, GND
Rocker Switch
- Description: A switch that can be used to control the flow of current in a circuit.
- Pins: output, input
18650 Li-ion Battery x 2
- Description: Rechargeable lithium-ion batteries.
- Pins: +, -
Wiring Details
Arduino Nano
- D2 connected to TRIG of HC-SR04 Ultrasonic Sensor
- D3 connected to ECHO of HC-SR04 Ultrasonic Sensor
- D10 connected to IN1 of L298N DC Motor Driver
- D11/MOSI connected to IN2 of L298N DC Motor Driver
- VIN connected to input of Rocker Switch
- GND connected to GND of HC-SR04 Ultrasonic Sensor
- 5V connected to VCC of HC-SR04 Ultrasonic Sensor
L298N DC Motor Driver
- IN1 connected to D10 of Arduino Nano
- IN2 connected to D11/MOSI of Arduino Nano
- 12V connected to input of Rocker Switch
- GND connected to + of 18650 Li-ion Battery x 2
HC-SR04 Ultrasonic Sensor
- TRIG connected to D2 of Arduino Nano
- ECHO connected to D3 of Arduino Nano
- GND connected to GND of Arduino Nano
- VCC connected to 5V of Arduino Nano
Rocker Switch
- input connected to VIN of Arduino Nano
- output connected to - of 18650 Li-ion Battery x 2
18650 Li-ion Battery x 2
- + connected to GND of L298N DC Motor Driver
- - connected to output of Rocker Switch
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.