#define TRIG_PIN 19 // Ultrasonic sensor TRIG pin
#define ECHO_PIN 18 // Ultrasonic sensor ECHO pin
#define LED_PIN 12 // LED indicator pin
long duration;
int distance;
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Trigger the ultrasonic sensor
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo time
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance in cm
distance = duration * 0.034 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check if the distance is below a certain threshold (e.g., 10 cm)
if (distance < 10) {
digitalWrite(LED_PIN, HIGH); // Turn on the LED if water is low
} else {
digitalWrite(LED_PIN, LOW); // Turn off the LED if water level is okay
}
delay(500); // Wait before the next measurement
}