Water Leak Detector

I 'am new to Arduino. I would like to modify this sketch to have the buzzer activated by mutable water detectors on mutable inputs and turn on output for each input.

/*
Edited Code from:
Rain Detector with Water level sensor
by Hanie kiani
Learn Electronics at ElectroPeak - Step by Step Guides & Tutorials
*/
const int sensorMin = 0; // sensor minimum
const int sensorMax = 1024; // sensor maximum
const int buzzer = 9;
void setup() {
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
}
void loop() {
int sensorReading = analogRead(A0);
Serial.println(sensorReading);

if (sensorReading >= 300) { // Sensor is wet
Serial.println("Wet!");
tone(buzzer, 5000);
}
else if (sensorReading >= 250) { // Sensor getting wet
Serial.println("Warning!");
tone(buzzer, 1000, 5);
}
else { // Sensor dry
Serial.println("Dry");
noTone(buzzer);
}
delay(1000); // delay between reads
}

More information ?

Before modifying any program, did you test it to be sure it works as it was designed?

Probably the best thing you can do is read the guidelines on how to use this forum. Post your code properly and also post an annotated schematic. I do not do good with frizzies or word problems.

| gilshultz
February 22 |

  • | - |

Probably the best thing you can do is read the guidelines on how to use this forum. Post your code properly and also post an annotated schematic. I do not do good with frizzies or word problems.


How do I post it properly .Like I said lam

new to this.

Please do read it.

To post code, open a reply, click on the </> button at the top of the message window, then paste your code where prompted.

To get the code to post, in the Arduino Development Environment (a.k.a. IDE) find the menu item that says "copy for forum" and click on it. But before you do that, press ctrl-T to reformat the code.

Thanks

I think you're saying you want to have a variable list of inputs and an output for each, and the list should be "mutable".

Mutable when - as the program is running, or before you compile it? The latter is easy, the former will be a lot of work.

Yes it worked here is the code.

const int sensorMin = 0;   // sensor minimum

const int sensorMax = 1024; // sensor maximum

const int buzzer = 9;

void setup() {

 Serial.begin(9600);  

 pinMode(buzzer, OUTPUT);

}

void loop() {

int sensorReading = analogRead(A0);

Serial.println(sensorReading);



if (sensorReading>=300){ // Sensor is wet

 Serial.println("Wet!");

  tone(buzzer, 5000);

}

else if (sensorReading>=250){ // Sensor getting wet

 Serial.println("Warning!");

  tone(buzzer, 1000,5);

}

 else  { // Sensor dry 

  Serial.println("Dry");

  noTone(buzzer); 

 } 

 delay(10); // delay between reads

} 

How many mutations do you want to plan for?

Would like to do six inputs with an output to power an LED and a common buzzer.

                                                                 Thanks I appreciate any help you can give me.

something like this..

const int sensorMin = 0;   // sensor minimum

const int sensorMax = 1024; // sensor maximum

const int buzzer = 9;

const byte SensorPins[] = {A0, A1, A2, A3};
int lastReading = 0;

unsigned long lastSample = 0;
int intervalSample = 100;


void setup() {

  Serial.begin(9600);

  pinMode(buzzer, OUTPUT);

}

void loop() {

  if (millis() - lastSample >= intervalSample) {
    lastSample = millis();
    for (int i = 0; i < sizeof(SensorPins); i++) {
      int newRead = analogRead(SensorPins[i]);
      if (newRead > lastReading)lastReading = newRead;
    }
  }
  //should be highest reading..
  int sensorReading = lastReading;

  Serial.println(sensorReading);



  if (sensorReading >= 300) { // Sensor is wet

    Serial.println("Wet!");

    tone(buzzer, 5000);

  }

  else if (sensorReading >= 250) { // Sensor getting wet

    Serial.println("Warning!");

    tone(buzzer, 1000, 5);

  }

  else  { // Sensor dry

    Serial.println("Dry");

    noTone(buzzer);

  }

  delay(10); // delay between reads

}

FYR for two channels,

#define buzzer 9

const uint8_t maxDetector = 2;
const uint8_t waterDetectors[] = {A0, A1};  // Define Analog Input Pins
const uint8_t alarmLamps[] = {2, 3};        // Define Alarm Lamp Pins
int detectorValues [maxDetector];           // Define Read Value Buffers for future use 

bool warning = false;
bool alarm = false;
bool currWarning = false;
bool currAlarm = false;

String statusMessage = "";
unsigned long prevTime = millis();
unsigned long interval = 1000;

void setup() {
  Serial.begin(9600);
  pinMode(buzzer, OUTPUT);
  for (uint8_t index = 0; index < maxDetector; index++) {
    pinMode(alarmLamps[index], OUTPUT);
  }
}
void loop() {
  warning = false;
  alarm = false;
  statusMessage = "";
  for (uint8_t index = 0; index < maxDetector; index++) {
    detectorValues[index] = analogRead(waterDetectors[index]);  // Read Water Level
    if (detectorValues[index] >= 300) {                         // Water Alarm Level
      currAlarm = true;
      digitalWrite(alarmLamps[index], HIGH);
    } else if (detectorValues[index] >= 250) {                  // Water Warning Level
      currWarning = true;
    } else {
      currAlarm = false;
      currWarning = false;
      digitalWrite(alarmLamps[index], LOW);
    }

    warning |= currWarning; // Common Warning
    alarm |= currAlarm;     // Common Alarm

    if (index > 0) {
      statusMessage = statusMessage + ", " +  + "Detector" + String(index + 1) + " Value:= " + String(detectorValues[index]);
    } else {
      statusMessage = "Detector1 Value:= " + String(detectorValues[index]);
    }

  }

  if (alarm) {          // Alarm
    tone(buzzer, 5000);
  }
  else if (warning) {   // Warning
    tone(buzzer, 1000, 5);
  }
  else {                // Normal
    noTone(buzzer);
  }

  if (millis() - prevTime >= interval) {
    prevTime = millis();
    Serial.println(statusMessage);
  }
}

Wokwi Simulator: Water Leak Detector

Works great, I added A2-A5 inputs now I need and output to turn on a LED that corresponds to each input.
I want to use this to monitor for water leaks it the laundry room and under sinks.

Thanks again I really appreciate your help.
1 Like

Sounds familiar. See post number 7. How come you're getting to this now?

To add more detectors,

  1. Change the number of sensors/detectors.
const uint8_t maxDetector = 6;  // <-- the number of sensors/detectors
  1. Add the PIN of the detector and the alarm-Lamp.
const uint8_t waterDetectors[] = {A0, A1, A2, A3, A4, A5};  // <-- the PIN of the detector
const uint8_t alarmLamps[] = {2, 3, 4, 5, 6, 7};        // <-- the PIN of the alarm lamp

Finally, please follow the forum role to easily to people to help you. I am also new to Arduino and Forum :sweat_smile:.

Enjoy Coding, Enjoy Arduino.

This works good, but what I need is the LEDS to be turned like
A2 turns on output 2
A3 turns on out put 3
A4 turns on out put 4
A5 turns on output 5
A6 turns on output 6
A7 turns on output 7
Each input corresponds to one output.

Tanks

1 Like

You change one line as below,

const uint8_t waterDetectors[] = {A2, A3, A4, A5, A6, A7};  // <-- the PIN of the detector

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.