This circuit is designed to monitor water levels and pH values, providing visual and auditory alerts when water levels exceed a certain threshold. It utilizes an Arduino UNO as the central microcontroller to interface with a water level sensor, a pH sensor module (ph4502c), a buzzer, a servo motor, and a Bluetooth module for potential wireless communication. An LED is also included to provide a visual indication of the water level status.
#include <Servo.h>
#define BUZZER_PIN 10 // Pin for the buzzer
#define POWER_PIN 7
#define SIGNAL_PIN A5
#define THRESHOLD 500
#define LED_PIN 2
#define SERVO_PIN 9 // Pin for the servo motor
Servo servo; // create servo object to control a servo
int value = 0; // variable to store the water level sensor value
int angle = 0; // the current angle of servo motor
const int pHSensorPin = A0;
// Calibration values for pH sensor
const float neutralVoltage = 2.5; // Voltage corresponding to pH 7.0 (adjust based on your measurement)
const float scaleFactor = 3.5; // pH sensitivity (adjust if necessary)
void setup() {
Serial.begin(9600);
pinMode(BUZZER_PIN, OUTPUT); // set buzzer pin as output
pinMode(LED_PIN, OUTPUT); // configure D2 pin as an OUTPUT
pinMode(POWER_PIN, OUTPUT); // configure D7 pin as an OUTPUT
digitalWrite(POWER_PIN, LOW); // turn the sensor OFF
digitalWrite(LED_PIN, LOW); // turn LED OFF
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(angle); // initialize the servo at 0 degrees
}
void loop() {
digitalWrite(POWER_PIN, HIGH); // turn the water level sensor ON
value = analogRead(SIGNAL_PIN); // read the analog value from the water level sensor
digitalWrite(POWER_PIN, LOW); // turn the sensor OFF
// Display the water level value on the serial monitor
Serial.print("Water Level: ");
Serial.println(value); // Print the actual water level value
// Read and display the pH sensor value
int sensorValue = analogRead(pHSensorPin); // Read the analog value from the pH sensor
float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog value to voltage
// Adjust the formula to invert pH scaling
float pHValue = 7.0 - ((voltage - neutralVoltage) * scaleFactor);
pHValue = constrain(pHValue, 0.0, 14.0); // Constrain pH value to a valid range
Serial.print("pH Value: "); // Show the pH value on the serial monitor
Serial.println(pHValue);
delay(100); // wait 100 milliseconds
// Water level detection and actions when the value is greater than THRESHOLD (500)
if (value > THRESHOLD) {
Serial.println("AMARAN BANJIR! Air melebihi Paras Normal"); // Custom alert message
digitalWrite(LED_PIN, HIGH); // Turn LED ON
// Calculate the siren speed and frequency range based on the water level
int beepDelay = map(value, 500, 700, 500, 100); // Faster beeps as value increases
int freqMin = map(value, 500, 700, 500, 800); // Lower frequency increases with water level
int freqMax = map(value, 500, 700, 1000, 1500); // Higher frequency increases with water level
// Create a sweeping siren effect between freqMin and freqMax
for (int freq = freqMin; freq <= freqMax; freq += 50) {
tone(BUZZER_PIN, freq); // Start with low frequency and increase
delay(beepDelay / 10); // Shorter delay for smooth frequency sweep
}
for (int freq = freqMax; freq >= freqMin; freq -= 50) {
tone(BUZZER_PIN, freq); // Decrease frequency to create the siren sweep
delay(beepDelay / 10); // Same shorter delay
}
// Control the servo to rotate to 180 degrees, then back to 0 degrees
servo.write(180);
delay(500); // Wait for the servo to reach the position
servo.write(0);
// Turn off LED after the sequence
digitalWrite(LED_PIN, LOW);
}
else {
// Turn off LED and stop the buzzer when the water level is below the threshold
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN); // Stop the buzzer
servo.write(0); // Keep the servo at 0 degrees
}
}
This code is designed to run on the Arduino UNO microcontroller. It initializes the connected devices and enters a loop where it reads the water level and pH values, providing alerts and actuating a servo motor if the water level exceeds a predefined threshold. The code includes comments for clarity and can be adjusted for calibration and sensitivity of the pH sensor.