Les PounderCreate Your Own Flood Detector

About the Author

Les is a maker and trainer who has worked with the Raspberry Pi Foundation and the BBC to deliver computing training.

@biglesp bigl.es

We all love a cup of tea/coffee/juice/water. But what if we need to alert people to an oncoming flood? How can we detect when water reaches a certain level? Well, we can and all we need are a micro:bit, two crocodile clips and some aluminium foil. This simple project uses the conductivity of water to close a circuit when water touches the two foil sensors. For this, we read analog values, ranging from 0 to 1023 which relate to 0V to 3V. Anything over 1000 will trigger our project to life.

Wiring Diagram

For this project, we shall be using the Javascript Blocks Editor https://microbit.org/code/ and we should already be comfortable copying code to our micro:bit.

Step 1 - Startup Screen

Creating the start up screen using a checkmark icon inside the onstart block

When our micro:bit starts up, we need to see that it is ready to go and we use the “Show Icon” block from Basic. In this case, we chose to use a tick to indicate readiness.

Step 2 - Slow Down!

Adding a pause 1000ms block to the on start code

We need to slow down the code, using “pause” from the Basic menu we set the delay to 1000ms, which is 1 second. This enables us to see the tick before the main code runs.

Step 3 - Forever… and ever… and ever!

Adding an if else block to the forever loop

Inside our forever loop we need to place an “if true then..else” block found in Logic. This block handles decisions. Such as if the water is touching the foil.

Step 4 - Greater Than?

Adding Greater than logic to the if statement

From Logic, we need to grab the “0 < 0” comparison block and place that over the True of the if block. Then change the < to a >.

Step 5 - Checking Values

Checking the pin value is greater than 1000

From the Pin section, found in Advanced. Drag “analog read pin P0” and place it in the first zero. In the second change the value to 1000 which is the trigger for our sensor.

Step 6 - Repeat After Me…

Adding a repeat 4 times block to the if loop

So if the sensor is triggered, we need to alert the user, and using the “repeat 4 times” block from Loops we can start an animation loop.

Step 7 - Big Square

Adding the big square show icon block and a pause 100ms block to the repeat loop

These blocks from Basic are “Show Icon”, which we use to display a large square. Then we delay for 100 ms using “pause.”

Step 8 - Small Square

Adding a small square show icon block and pause 100ms block to the repeat block

Again from Basic, we use “Show Icon” but this time we have a small square, and another “pause” when the loop runs it will look animated!

Step 9 - Standby Mode

Adding the checkered board show icon block to the else block

Inside the else part of our condition, we add another “Show Icon” with a chessboard design. This is the default that shows when the water level is low.

That’s it, now download the code to your micro:bit and when it has finished, drop the foil sensors into a cup or bowl and slowly fill it up with water. When the water touches the sensors, the LED matrix on the micro:bit will change and alert you to high water.

Well done you have made your own flood sensor! Can you change the code to make a buzzer sound for a blind person? Or perhaps send a radio message to a remote micro:bit?