This circuit is designed to control a water level system using various sensors, actuators, and an Arduino UNO microcontroller. The system includes red and green LEDs for status indication, a servo motor for mechanical control, a buzzer for audio alerts, pushbuttons for user input, non-contact water level sensors for detecting water levels, relays for controlling power to the servo and a solenoid valve, and a power supply to provide the necessary voltage for the components.
#include <Servo.h>
#define Liquid_Detection_Pin 2 // Output pin on liquid detection sensor
#define Overflow_Sensor_Pin 3 // Output pin on overflow sensor
#define Buzzer_Pin 6 // Pin for piezo buzzer
#define Servo_Pin 10 // Pin for servo motor
#define Start_Button_Pin 8 // Pin for start/stop button
#define Red_LED_Pin 13 // Pin for red LED (buzzer indicator)
#define Green_LED_Pin 12 // Pin for green LED (liquid detection indicator)
#define Solenoid_Pin 5 // Pin for solenoid relay
#define Motor_Relay_Pin 7 // Pin for motor relay
Servo myServo; // Create a servo object
bool systemEnabled = false; // Flag to indicate system status
bool buttonState = false; // Flag to keep track of button state
unsigned long motorTimer = 0; // Timer for motor run time
bool motorRunning = false; // Flag to indicate motor running
bool waterDetected = false; // Flag to indicate water detection
bool overflowDetected = false; // Flag to indicate overflow detection
void setup() {
Serial.begin(9600);
pinMode(Liquid_Detection_Pin, INPUT);
pinMode(Overflow_Sensor_Pin, INPUT); // Initialize overflow sensor pin
pinMode(Buzzer_Pin, OUTPUT); // Set buzzer pin as output
pinMode(Start_Button_Pin, INPUT_PULLUP); // Use internal pull-up resistor
pinMode(Red_LED_Pin, OUTPUT); // Set red LED pin as output
pinMode(Green_LED_Pin, OUTPUT); // Set green LED pin as output
pinMode(Solenoid_Pin, OUTPUT); // Set solenoid relay pin as output
pinMode(Motor_Relay_Pin, OUTPUT); // Set motor relay pin as output
myServo.attach(Servo_Pin); // Attach the servo to the pin
myServo.write(0); // Initialize the servo to 0 degrees
}
void buzz() {
tone(Buzzer_Pin, 1000, 200); // Produce a 1 kHz tone for 200 ms
}
void buzzForOneSecond() {
tone(Buzzer_Pin, 1000, 1000); // Produce a 1 kHz tone for 1 second
}
void shutdownSystem() {
// Turn off everything
digitalWrite(Solenoid_Pin, LOW); // Turn off solenoid relay
digitalWrite(Motor_Relay_Pin, LOW); // Turn off motor relay
digitalWrite(Green_LED_Pin, LOW); // Turn off green LED
myServo.write(0); // Stop the servo motor
if (motorRunning) {
motorRunning = false; // Set motor running flag to false
}
systemEnabled = false; // Disable the system
waterDetected = false; // Reset water detection flag
overflowDetected = true; // Mark the system as having overflowed
Serial.println("System Shutdown Triggered.");
// Buzzer beeps for 1 second and Red LED blinks for 5 seconds
for (int i = 0; i < 5; i++) {
digitalWrite(Red_LED_Pin, HIGH); // Turn on red LED
buzzForOneSecond(); // Beep the buzzer for 1 second
delay(1000); // Wait for 1 second
digitalWrite(Red_LED_Pin, LOW); // Turn off red LED
delay(1000); // Wait for 1 second
}
}
void systemStartup() {
// Blink green LED and sound buzzer for system startup
for (int i = 0; i < 6; i++) { // Blink for 3 seconds (500ms on/off cycle)
digitalWrite(Green_LED_Pin, HIGH); // Turn on green LED
buzz(); // Buzz for 200ms
delay(500); // Wait 500ms
digitalWrite(Green_LED_Pin, LOW); // Turn off green LED
delay(500); // Wait 500ms
}
}
void loop() {
int currentState = digitalRead(Start_Button_Pin);
int overflowState = digitalRead(Overflow_Sensor_Pin); // Read overflow sensor state
if (overflowState == LOW) { // Overflow sensor triggered
shutdownSystem(); // Shut down everything
return; // Exit loop to prevent any further operations
}
if (currentState == LOW && buttonState == true) { // Button is pressed
overflowDetected = false; // Reset overflow detection flag
systemEnabled = true; // Enable system
buttonState = false; // Set button state to false
waterDetected = false; // Reset water detection flag
motorRunning = false; // Reset motor running flag
systemStartup(); // Start up sequence: LED blink and buzzer
}
if (currentState == HIGH) { // Button is released
buttonState = true; // Set button state to true
}
if (systemEnabled) {
if (!waterDetected) {
// Open solenoid valve
digitalWrite(Solenoid_Pin, HIGH); // Turn ON the solenoid relay
// Wait for water level to reach desired level
while (digitalRead(Liquid_Detection_Pin) == HIGH) {
// Check for overflow during water filling
if (digitalRead(Overflow_Sensor_Pin) == LOW) { // Check overflow sensor
shutdown