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.
Step 1 - Startup Screen
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!
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!
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?
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
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...
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
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
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
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?