# Water-level measuring Author: Maria Engshagen Student credentials: mm224wk Approximate time to do this tutorial: 3-4 hours In this tutorial I show step by step how to build this water sensor that measure 3 different water-levels. Then how to connect it to your microcontroller and through wifi send the measurement to internet and visualize the data in Adafruit. [toc] ## Objective I did this project as a part of the summer course at Linnaeus University, 1DT305 - Applied Internet of Things. With the purpose of getting the knowledge how to build a IoT devise. As this is my first time building a device and visualizing data I think this will be an eye opener for what you can do and build in the future. I wanted to build this device for the purpose of measuring the waterlevel in a waterbutt in wood. We are going to collect rain water in the butt and have a wateringsystem from it. In the future I want the microcontroller to react when the water level is high to a motor that close the drainpipe and if it is lower it opens the drainpipe to refill when it rains. From the start it would make a warning when the waterlevel is low as the wood do not want to be out of water for too long. That way we can refill when needed. I chose this project as it would be reliving that it could take care of the waterbutt if we are not home and it rains a lot or it need refill when we are gone. I could also see a good purpose of measuring waterlevel if you have your owe wellspring and want to see the waterlevels as the summers get hotter. ## Material Here comes a list of the material I used Things to the microcontroller, bord and sensor | Product | Where to buy | Short description |Price | Picture | | -------- | -------- | -------- |--------|--------| | Start Kit with FiPy: FiPy with header, expansion bord , Antennae, Micro USB cable, Jumper wire, Breadbord, variety of resisters, variety of LED, tilt switch, | [electrokit.com](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-fipy-and-sensors-bundle/) | Text | 1499 SEK | | FiPy | From startkit | FiPy, - Is the part that you program and have the possibility to connect to networks (such as wifi, LoRa, LTE CAT M1, NB1 and BLE ) and can run micropython | - |![](https://i.imgur.com/QkekrrR.png)| | Pysense (from problems with delivery I got an Pysense instead of the ordinary expansion bord ) | From startkit | This part have some bult in sensors, but I am using it to power the devise and get in data to the Fipy | - |![](https://i.imgur.com/yhLvisH.png) | | Breadbord | From startkit | Make it possible to connect everything to the microcontroller | - |![](https://i.imgur.com/dWgfWZA.png)| | Resistors 330 ohm 3st | From startkit | helps to take down the voltage, how much is presented in Ohm | - |![](https://i.imgur.com/O3ZDNZL.png)| | Jumper wire (male) | From startkit | Use to connect the parts on bord to your microcontroller | - |![](https://i.imgur.com/tmCM46i.png)| | Micro USB cable | From startkit | Is to instal the fipy and power it during installation. You may have one make sure that you can transfer data with it as well | - |![](https://i.imgur.com/1TrlAcG.png)| | Water-level sensor, FK902 | [electrokit.com](https://www.electrokit.com/produkt/vattennivaindikator-3-nivaer/) | This water-level sensor is normally detached and measure the water level in three levels | 39 SEK |![](https://i.imgur.com/EfqKh2r.png)| | Battery LiPo 3.7V 4000m | [electrokit.com](https://www.electrokit.com/produkt/batteri-lipo-3-7v-4400mah/) | Battery that is possible to connect to the microcontroller | 249 SEK |![](https://i.imgur.com/lPfa0tE.png)| | Cable RK 0.5mm2 brown/m | [electrokit.com](https://www.electrokit.com/produkt/rk-0-5mm2-brun-m/) | These cables are used to the sensor to go into the water. You need to have cable to the four different levels for the sensor (the cables come in one whole piece) | 9.5 SEK/m I used 5m, price 47.5 SEK, but order as long cable you need | ![](https://i.imgur.com/BmOixNZ.png)| | Battery 9V | [electrokit.com](https://www.electrokit.com/produkt/batteri-9v-gp-ultra/) | To power the water sensor | 39 SEK |![](https://i.imgur.com/VHXs8vx.png)| | Battery contact | [electrokit.com](https://www.electrokit.com/produkt/batterikontakt-9v-rigid-150mm/) | For connecting the external 9V battery to the sensor | 9 SEK |![](https://i.imgur.com/b4L0z0E.png)| | Mini Tamiya kit 10 male/female | [kjell.com](https://www.kjell.com/se/produkter/el-verktyg/elektronik/rc-tillbehor/kontakter-till-rc/luxorparts-kontaktset-mini-tamiya-med-krympslang-10-par-p35452) | Connection to connect the adapter and the wires for power the sensor | 99.9 SEK |![](https://i.imgur.com/DIdo14E.png)| | Switched net-adapter 9V (DC) / 1900mA | [kjell.com](https://www.kjell.com/se/produkter/el-verktyg/stromforsorjning/natadaptrar/acdc-natadaptrar/fast-utspanning/luxorparts-switchad-natadapter-9-v-dc-17-w-p45181) | To make sure that the sensor get a stable 9V during testing and set up | 249.9 SEK |![](https://i.imgur.com/paTMAAS.png)| To make sure the voltage was stable I used the adapter and connected with Tamiya-connection. There for I put a Tamiya-connection to the battery contact too as I soldered Tamiya-connection on the wires from the sensor circuit-card to get power. That way I did not need to solder again when I start to use an ordinary 9V battery. If you do not want to do that measuring, skip to by adapter and Tamiya-connections. If you do not have equipment for soldering at home: | Product | Where to buy | Short description |Price | Text | | -------- | -------- | -------- |--------|--------| | Soldering iron set | [kjell.com](https://www.kjell.com/se/produkter/el-verktyg/verktyg/lodning/lodkolvar/dibotech-lodkolvset-30-w-p40060) | Tool to build up the sensor | 199.9 SEK | ![](https://i.imgur.com/R65eJSl.png) | | Nippers | [kjell.com](https://www.kjell.com/se/produkter/el-verktyg/verktyg/tanger-kabelskalare/dibotech-tangset-3-delar-p40544) | Tools to cut ex wires and resistors legs | 99.9 SEK | ![](https://i.imgur.com/N3CJvtB.png)| | Solder suck wire | [kjell.com](https://www.kjell.com/se/produkter/el-verktyg/verktyg/lodning/avlodning/avlodningsflata-2-mm-p40040) or [electrokit.com](https://www.electrokit.com/produkt/lodflata-2-7mm/) (Second link is calvary) | Helping you to suck up unnecessary tin when you solder | 69.9 SEK | ![](https://i.imgur.com/oZvYGo9.png) | If you do not want to by an soldering iron set and live in Sweden you can rent set here [hygglo.se]( https://www.hygglo.se/category/1836-lodkolv/knivsoder). I need some extra tin for the soldering and got it with the soldering iron set. If you need only the tin you can find it here [elektrokit.com]( https://www.electrokit.com/produkt/lodtenn-1-00mm-blyfritt-12-5g/) Good to have | Product | Where to bay | Short description |Price | Text | | -------- | -------- | -------- |--------|--------| | Multimeter UT131D |[jula.se](https://www.jula.se/catalog/el-och-belysning/elinstallation/testinstrument/multimetrar/multimeter-006281/) | Tool to help you measure Volt, Ohm and Amper | 129 SEK |![](https://i.imgur.com/iUymq62.png) | ## Computer setup To be able to talk to your microcontroller, you need to have a special environment. I used the recommendations that we were given in the course as I did not have any preferences (I used IDE Atom). I use a laptop with Windows 11 and the set up can look different if you use another computer. Step 1: Connect your FiPy to the Pysense and plug in the micro USB cable from Pysense to your computer. Make sure it looks the like this ![](https://i.imgur.com/cAdMVZd.png) Then you have the pins in the right way so the FiPy and the Pysense can work with each other (to learn more about the pins you can read here [FiPy]( https://docs.pycom.io/datasheets/development/fipy/) and [Pysense]( https://docs.pycom.io/datasheets/expansionboards/pysense/)). Step 2: I installed Node.js (it is needed to plug-in the software, when using Atom and the Pycom board) and Atom (an IDE which makes it possible to write the code to talk to your microcontroller). But you can use other IDE:s if you prefer to do that. I followed this [tutorial](https://hackmd.io/@lnu-iot/SydH7MTcw) to make the installations. In the same link you find links to download Node js and Atom. OBS: Do not forget to install Pymaker in Atom, the last part in the tutorial Step 3: To get the microcontroller to listen to micropython you need install a Firmware update, connect your device to Atom and flash the board. To do so I followed this [tutorial](https://hackmd.io/@lnu-iot/SJ91R_jSO). OBS: in the tutorial that is linked on this part (Advanced Settings), fill in the blue marks otherwise it will not work. ![](https://i.imgur.com/fEHPSu4.png) Extra info to the part about Atom: Under project right click and choose a new folder, name it and press enter to create it. Then create a new file in your folder (right click under project and choose new file) name it to main.py press enter to create it (the first file is always a main). When creating a file named “main.py” that is going to be the programs starting point for executing code. The .py is to make it as a Pyhton file. OBS: Sometimes Atom complains that is has no file or folder open. Then right click on the folder and choose “Remove Project Folder” ![](https://i.imgur.com/dh7V2S1.png) Then choose “Reopen a project” ![](https://i.imgur.com/FjszLtZ.png) Choose the folder you want to reopen ![](https://i.imgur.com/uBtu12n.png) Then choose “Add to this window and discard state” ![](https://i.imgur.com/XVuUcsu.png) :::success Other good staff to know: * You can’t have two Atom open and try to talk to your devise. * When you add stuff to your file (or more files) always save it before pressing “Upload project to device” ![](https://i.imgur.com/RKHrCPa.png) * When you have added stuff to a file and not saved it you can see a blue dot next to the opened file ![](https://i.imgur.com/4uu7bxU.png) * If you have an infinite loop as the flashing on the board to exit that or other running code press Ctrl+ c to stop it. ::: After this we know that the microcontroller reads micropython, and thus is working and reads your commands. ## Putting everything together To put together the sensor and the board we start with the sensor that needs to be soldered. Step 1. Get familiar with the parts that come with the water sensor and sort the different resistors by what Ohm they are. If you do not have any previous experience of resistors I recommend to see this short [video]( https://www.youtube.com/watch?v=lKwjouNT4C8). The resistors help to take down the voltage. ![](https://i.imgur.com/XY196XQ.png) On the paper that follows with the sensor you can find an explanation what Ohm the different resistors have. ![](https://i.imgur.com/xh2i059.png) On the circuit that comes with the water sensor packed you have some helping notes where to put the parts. ![](https://i.imgur.com/2FHeJWX.png) LED 3 is the green, LED 2 is yellow and LED 1 is the red one. Step 2: Start to put all the components in place. The top part of the circuit is the part with the text on top and there you have the top part of the LED, resistors, transistors. When putting down one part on the back side of the circuit bend out the legs a little to get it stuck. :::info OBS: When putting done the LED be aware of the leg length! The long leg is to the + side of the circuit (the top part of the circuit) and you can find some symbol as on the picture that shows how to put it. A stand for Anod (the + connection) and K stands for cathode (the – connection). Transistor is a part that is like a switch where to lead the electricity. So here as well think extra to put it correct. The transistor has one flat part that would be turned to the right side of the circuit. And look at how the circuit it has some marks the make it easier to put it correctly. ::: ![](https://i.imgur.com/WzWt0UV.png) When all the resistors are connected ![](https://i.imgur.com/lOskjfu.png) ![](https://i.imgur.com/Gqjwf7h.png) When LED: s, Transistors and resistors are connected. ![](https://i.imgur.com/bSgKF5U.png) Step 4: Then prepare your cables that are going into the water and mark them with 3,2,1 and P ![](https://i.imgur.com/ThDLalY.png) Cable 3 would be the shortest one, close to the top level of the water. Cable 2 would be at mid-height of the butt/ water container. Cable 1 would almost reach the level when you want to have a reaction on the lowest part. Cable P would be little above the tank bottom. ![](https://i.imgur.com/fkIIoCI.png) Step 5: Shorten the legs of all the components ![](https://i.imgur.com/x1FBz8l.png) To at least ↓ ![](https://i.imgur.com/uLm5bsp.png) And prepare the top parts to the cables, you want to have ca. 0.5 cm clean from the plastic ![](https://i.imgur.com/TAirs3W.png) Step 6: Prepare your solder station and get the solder hot. Test to melt a lite tin and see if the solder is hot enough. ![](https://i.imgur.com/KbWhL8t.png) ![](https://i.imgur.com/BFPhcbB.png) ![](https://i.imgur.com/0H1FDXm.png) I soldered all components before the cables ![](https://i.imgur.com/KatF6RU.png) ![](https://i.imgur.com/ctvuSCR.png) ![](https://i.imgur.com/6a89Coz.png) :::success Tips: * Be on a place with good ventilation then you solder. * Do not try to make the soldered part cool down quicker. That can cause problems with the devise. * Make sure that the end parts (the soldered parts) do not go into each other * If you want to shorten the legs on the back side even more after soldering, heat up the tin again to the leg that you shorten. Otherwise you can get cracks in the tin that can cause problems to the connection. * Make sure that the legs on the circuit do not go into each other. * Make sure that the tin has been so heated that the surface is nice and “flat”. ![](https://i.imgur.com/4F4gqeu.png) ::: Step 7: To the development I did some extra solutions to get a stable voltage with the adapter to the sensor. If you want to skip that, solder the battery contact direct to your bord. :::info OBS: The red cable shall go to the + part at the top of the right side on the circuit and the black cable is to the – part and mark with a G on the top of the circuit. It is a good practice to always use red wires or cables only for anod (+) giver and black wires or cables for cathode (-) giver. ::: To solder my solution: chop of the end connection on the adapter to get to the cables. The adapter I have did not have red and black cables inside the plastic. Normally if both cables are black the one with the white strip is the anod (+) cable. I dubbed-checked with my multimeter. A good practice I got to know was always use the square part to put the anod cable. Otherwise I followed how to solder a Tamiya from this link [Tamiya soldering](https://www.youtube.com/watch?v=UezUI-CpTks). ![](https://i.imgur.com/ZhK1QzW.png) ![](https://i.imgur.com/FqNIN7S.png) ![](https://i.imgur.com/FJb8lSJ.png)![](https://i.imgur.com/JRH8L3n.png) ![](https://i.imgur.com/QSAzKNP.png) I soldered on a female contact (the Tamiya with hole in the middle and the female plastic part) on the adapter. From the sensor I soldered a male Tamiya. To the battery contact I soldered a female contact (so it is easier when I want to start use ordinary battery to the sensor). ![](https://i.imgur.com/D2JWfRQ.png) :::info OBS: think twice so you get all the anodes to the right side. When you put together the male and female Tamiya- contacts red would go to red and black to black. If you need to switch the place you put your Tamiya you need a special tool to get them out as the [Tamiya Pin Extractor Tool]( https://www.amazon.com/flite-Tamiya-Extractor-Tool-EFLA215/dp/B000BRPQJC), if you need to and do not have the tool follow [this link](https://www.youtube.com/watch?v=-Qmy3-yaSvM). ::: ![](https://i.imgur.com/eqDufln.png) ![](https://i.imgur.com/aoinMsY.png) ![](https://i.imgur.com/9gv1zwj.png) Red to Red or Red to black with white strip to get the anods together. Step 8: Before soldering the last parts, so you can connect the sensor to the board, try if everything works. So put the cables in water and connect the sensor with power (9V). ![](https://i.imgur.com/LCiB5j0.png) When all the cables are in the water all the lights would be on. Take out one by one to see if everything works. When only the P cable is in water no light should be on. Here I tested with the multimeter how strong the voltage was from a LED anod leg to the in part of the cathode. And got out that voltage to those parts was ca 2V when the cables is in the water. This measure is important, as the microcontroller can’t take in too high voltage to the pins. The microcontroller can’t take more then 12mA each and recommended is 6mA to a pin ([FiPy datasheet]( https://docs.pycom.io/datasheets/development/fipy/)). If we calculate on that (A = V / Ohm [calculations](https://www.rapidtables.com/calc/electric/watt-volt-amp-calculator.html)) then we get 2V/330 Ohm = 0.006A = 6mA. Then we can see that we get the recommended mA to a pin on the microcontroller with that resistor (330 Ohm). Step 9: Take a yellow, green, white and black jumper wire those we are going to solder in from the back to the anod leg to each LED. I prepare one of the ends like a hook, you can see in the picture. Do that to one of the sides to the wires. ![](https://i.imgur.com/yh7ESm0.png) Start with the wire to the red light with the white wire (as red is “only” for power). ![](https://i.imgur.com/6Ifnv9T.png) Which is if you have the water cables towards you the second blob in you left corner is where to solder the white wire. When soldering in the wire melt the tin that is already on and put in the side with the hook in the soft tin. Make sure the tin having a nice flat soldering after. Continue doing the yellow and green jumper wire as in the picture. The black jumper wire would you solder to the back of the cathode. Which is if you have the water cables towards you the fourth from the top on the left side. ![](https://i.imgur.com/keJazUd.png) Now the water sensor is ready to connect to the board. Step 10: Follow the picture below to put the things together. ![](https://i.imgur.com/GPSeCfV.png) The resistor on the board is 330 Ohm and need to go over the gap in the middle. The resistor takes down the voltage before it come to the jumper wire that is connected to the FiPy. Make sure you use the same line to connect one line. Take the same colour on a new jumper wire in the other end of the line from the sensor. Put the green jumper wire in P13 pin -, yellow – in P14 pin and the white in P16 pin on the FiPy (it is important to use the same pins to the colour I have so the code that is done works the same way). We put the wires there as pin P13-20 can read a analog signal ([githud with other exampels](https://github.com/iot-lnu/applied-iot/blob/master/sensor-examples/Photoresistor/main.py ), [FiPy]( https://docs.pycom.io/datasheets/development/fipy/) and [Pysense]( https://docs.pycom.io/datasheets/expansionboards/pysense/). We put the black wire to the boards GND pin which is for the cathode. The wires from the Pysense red and black gives the microcontroller power. The green goes from and to TX and yellow goes from and to RX. The TX is the transmit pin, when you send data and the RX is the receive pin is when you get data. Now your set up is complete. ## Platform In my case I hoped to be able to use LoRa or Helium which take less power and can reach longer distances. As that would make the devise is more flexible were to put it, easier to scale it and less power dependant. Sadly in my neighbourhood we did not have connection to any of these two. Which made me go with wifi as my devise thankfully will reach the wifi connection where I am going to use it. Wifi requires more battery consumption, but in this starting point I feel this was the best option. For presenting the data I used Adafruit, as I did not get everything to work properly with Datacake. Datacake has more functionalities and have another system for messaging you if a special case occurs. With Adafruit I can still get notified by my Discord. In this scope of time that I had I did not prioritize to go in the solutions with self-hosting (ex TIG-stack or Nod-Red). Instead, I needed to be satisfied with the low code solutions that Adafruit or Datacake provides. ## The Code Before I started with my code, I tried my wifi connection by following this [tutorial](https://hackmd.io/@lnu-iot/SJ8TGsUd5) for Pycom. I created a file named keys to save my passwords to the wifi and later added the passwords for connecting to Adafruit. Make sure to import that file where you want to use the passwords. From this test I only saved my passwords as later we use another code for connecting to the wifi. To get this sensor to talk to the devise I needed to make some code as there was no library as it normally works detached. First step was to use the correct Pins on the bord to get in analogue signal. I had a hard time to get it to work. I got help from Davie Nilsson and in the end I took inspiration from the library at Github for Photoresistor [Link]( https://github.com/iot-lnu/applied-iot/blob/master/sensor-examples/Photoresistor/main.py) to succeed to read in from the Pins. This class is a mix from my coding and parts from the Github link: In the top I import the classes that is needed for the code. Then creating a class to the sensor. In the comment in the photo I declare what the parts are doing and where I got different parts. ![](https://i.imgur.com/bZV3QXf.png) ![](https://i.imgur.com/mIZTSVE.png) ![](https://i.imgur.com/McnZLNz.png) Important part from my main: I import the class for the water sensor and others that is needed. ![](https://i.imgur.com/poTPkX4.png) Now I tried out to connect with Adafruit by following this [tutorial]( https://hackmd.io/@lnu-iot/ByPRkQTF9). In the end I only used one feed address to Adafruit to my sensor the one I call keys.green and have the “address” in my file keys. To get that I created a dashboard and feed to the water sensor as doing the same as in the tutorial above. OBS: If you use other variable names you need to change the variable names in main then trying to call for them. I reconstructed parts that is given in the example code to try out Adafruit and used same MQTT to send up data to Adafruit from my sensor in my mqtt.py. This is a function from main that is based on code to talk to Adafruit there I present my data. I could reuse some of the code from the example coding as I made my value to return a number. This is a reconstructed part where the def send_rendom() function was. OBS make sure to change all the names where this function is used in the main. And put your Adafruit feed address to your sensor where I have keys.green ![](https://i.imgur.com/XLQG5yX.png) This is the part is reconstructed that send the data to Adafruit ![](https://i.imgur.com/ymKa9cX.png) ## Transmitting data/connectivity I used wifi protocols as I did not have coverage with LoRa or Helium. When more development is done, I want the water sensor to send data every second hour. But for testing as it is now I do that every 5 seconds. Adafruit is using MQTT or REST protocols for sending and recieving data. I choose to use MQTT and I used webhook as well to be able to send messages to my Discord account. You can find how to get webhook in the same link as trying out Adafruit. I use the settings: less than 1 as I want to get notification when the battery is out for the sensor. With further development I want to have a message when the water-level is red too. ![](https://i.imgur.com/0WxwIuz.png) ## Presenting the data I choose to present the water-level with the “line graph” after seeing which blocks that could be good for presenting the water-levels, [here](https://learn.adafruit.com/adafruit-io-basics-dashboards/adding-blocks) you can read more about what blocks is for in Adafruit. ![](https://i.imgur.com/VULe2b0.png) In the picture you can see when battery is not completely in when it is 0 and when it is on and I take out one by one of the cables to demonstrate the different levels. The code is not perfect yet and not completely reliably. Especially it seems to be some bug with the message with the warnings to Discord. As I get out 1 sometimes and sometimes it does not get out the warning of 0 as Adafruit turn of the actions sometimes ![](https://i.imgur.com/WM2zDPW.png) I need do to some more testing before I use it for its purpose in the waterbutt. For the moment the data is saved shortly in the variable “green” and when the sensor read of the level again it saves it over the previous data. So when testing is completed it will save the data for 2 hours and then overwrite it. It is more important that I can read what happened I Adafruit for a longer period and get the warnings correctly. For the moment the only trigger is when a warning is sent to my Discord, but later there will be a trigger to start the battery to the sensor and to the motor to open and close the drainpipe. I choice the platform as it was free and in the scope of time we had. When using Adafruit their database solution is an time-series databases called feeds. Every feed contains time-stamped data points [link](https://learn.adafruit.com/all-the-internet-of-things-episode-four-adafruit-io/how-adafruit-io-works). So I use their databases solution and did not thinking of do some thing else. ## Finalizing the design This is how my set up looks like for now. I use the adapter with the Tamiya-contacts to power the sensor and to the microcontroller I use power from the computer as I need to do further improvements. ![](https://i.imgur.com/JwI6Kki.png) As I did not have any previous experiences of building IoT-devises I did not know what to expect. It has been harder through both delivery delays and my lack of knowledge from when I ordered all the components. It might have been better to order an other water-level sensor that from the beginning is compatible with a board. But through all this I have learnt so much, both to better plan an IoT-devise build, what I need to read about the sensor I beforehand, soldering, electricity, the importance of what battery to use, coding to microcontrollers and so much more. It has been challenging and a lot of fun doing this. The project went well as I got the water sensor to work! Yeyy! But further improvements is can be done for the sensor I have mentioned: make a solution to turn on and off the battery to the sensor, make the code the battery swich, make a case to have the sensor outdoors, make adjustments to the code in Adafruit and more. This journey have as I hoped open my eyes how to build IoT solutions, having fun and I am looking forward to making more IoT-devises.