Contents
  1. 1. Wiring
  2. 2. Parts
  3. 3. Sketch
  4. 4. Water activated switch
  5. 5. Conclusion
  6. 6. Water level measurement

I thought that it would be cool to create a switch, which is turned on by water contact. This means when the switch is closed, it indicates that the device - e.g. DiveIno - is in the water.

Wiring

I took one of my Arduino Mega boards and hooked up the following components with each other based on this layout:

The two green wires should go into a water filled bowl depicted by a blue wire on the right hand side.

Parts

Based on this diagram you need the following parts:

  1. Arduino Mega microcontroller board
  2. BC547 NPN transistor
  3. Blue LED
  4. 250 kilo Ohm resistor
  5. 100 kilo ohm resistor
  6. 2x 220 Ohm resistor
  7. Breadboard wires

Sketch

In order to make the prototype circuit work I wrote the following Arduino sketch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
int LED_PIN = 3;
int WATER_SWITCH_PIN = 9;

int previousValueWaterSwitch = 0;
long counterWaterSwitch = 0;
bool waterSwitchActivated = false;

void setup() {
pinMode(WATER_SWITCH_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);

Serial.begin(115200);
Serial.println("Water activated switch");
}

void loop() {
monitorWaterSwitch();
}

void monitorWaterSwitch()
{
if (!waterSwitchActivated) {
int currentValueWaterSwitch = digitalRead(WATER_SWITCH_PIN);

if (currentValueWaterSwitch == LOW) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}

if (currentValueWaterSwitch == LOW && previousValueWaterSwitch == HIGH) {
Serial.println(F("WATER SWITCH - ON"));
counterWaterSwitch = 1; // Turned ON
}
if (currentValueWaterSwitch == HIGH && previousValueWaterSwitch == LOW) {
Serial.println(F("WATER SWITCH - OFF"));
counterWaterSwitch = 0; // Turned OFF
}
if (currentValueWaterSwitch == LOW && previousValueWaterSwitch == LOW) {
counterWaterSwitch++; // Still ON
if (counterWaterSwitch % 100 == 0) {
Serial.print(F("WATER SWITCH - "));
Serial.println(counterWaterSwitch);
}
}
previousValueWaterSwitch = currentValueWaterSwitch;

long ACTIVATION_LEVEL = 200000;

if (counterWaterSwitch > ACTIVATION_LEVEL) { // Switch is ON for some time
counterWaterSwitch = 0;
waterSwitchActivated = true;
activatedWaterSwitch();
}
}
}

void activatedWaterSwitch()
{
Serial.println(F("WATER SWITCH - Activated"));
delay(5000); // Wait 5 seconds
waterSwitchActivated = false;
}

The LED is connected to the digital 3 pin, the transistor collector is to the digital 9 pin of the Arduino Mega board.

Water activated switch

If you put the two green wires into a water filled bowl then the WATER SWITCH - ON message will be printed to the Serial Monitor and the blue LED will turn on.

If one of the wires are removed from the water, the connection gets broken. In this case the WATER SWITCH - OFF message gets printed to the Serial Monitor and the LED is turned off.

After some time - governed by the ACTIVATION_LEVEL value - the activatedWaterSwitch() method in the sketch gets called and it prints the WATER SWITCH - Activated message to the Serial Monitor.

Conclusion

I played a bit with this prototype circuit. I submerged the wires into sweet and salt water. Salt water is more conductive than sweet water, therefore the wire tips can be at the other side of the bowl, the connection is still established. However in sweet water the distance between the tips do matter! The closer is the better! If the tips are too far away from each other, there will be no connection at all.

If you would like to activite your device by water contact make sure that the wire tips be close to each other!

Water level measurement

Another option is to use this circuit as a basis to measure the water level in a tank. The Arduino Water Level Indicator blog post contains an excelent description about how this can be done.

Contents
  1. 1. Wiring
  2. 2. Parts
  3. 3. Sketch
  4. 4. Water activated switch
  5. 5. Conclusion
  6. 6. Water level measurement