# Tutorial on how to make a plant moisture sensor By William Rittfeldt, wr222bk ###### tags: `IoT,LNU,LoPy4,Pycom` This project is an easy one to make, but it also provides an understanding for IoT. Despite being easy to make it serves its purpose to notify when your plant needs to be watered. It also has a lot of improvement potential, making it a good project when continuing to learn IoT. Estimated time: 1-3 hours ## Objective I have chosen to create this project because, as a beginner, I want to get some knowledge of IoT without getting in over my head. I also wanted to make something I could have use for in my daily life. As a infamous plant killer this is something for me. The purpose of the project is to obtain the level of moisture in the soil and using that data to see how the moist changes over time. Then, with the collected data, tell when the plant needs watering. It hopefully gives the insight that it is possible to make a useful IoT system without too much experience. ### Material | Material |Used for | | -------------------- |:--------------------- | | lopy4 |A MicroPython enabled development board | | Expansion board |Can connect the lopy4 and pins with wires | | Jumper Wires |The "bridge" between pins on the expansion board and sensors. | | Micro usb cable |Charge the expansion board | | Jordfuktighetssensor | A sensor that get moister levels in soil | Everything can be bought from electrokit.se | Bill of materials | Cost | | ----------------- |:----------------------- | | LoPy4 and sensors bundle|949 kr | | Jordfuktighetssensor |29 kr | |Total | 978 kr | ### Computer setup For this project I used VsCode (Visual Studio Code) as the IDE, with python as the programming language. First things first, download VsCode through the following link https://code.visualstudio.com/ , you also need the latest version of node.js: https://nodejs.org/en/. Then start up VSCode, in there you can search for pymkr and then hit install to get the pymkr plugin needed for programming in your lopy4. Before starting to programme it's a good idea to update the firmware of the lopy4. Connect the lopy4 to the expansion board. Then connect the expansion board to the computer with the micro USB cable. Then follow the instuctions from Pycom: https://docs.pycom.io/updatefirmware/device/ Another good thing to do is to sign up at https://pybytes.pycom.io/ and setup your device to be able to send the collected data from the sensor over wi-fi to easily see how it changes over time. ## Putting everything together ![](https://i.imgur.com/uqXVoPy.png) The red lines are the wires between the pins. It's a very easy setup, the ground pin (GND) of the expansionboard is connected to the GND of the sensors middle part. The output pin is connected from P4 on the expansionboard so that the output can be on/off, and is connected to AO on the sensors middle part. Then the input pin P20 is connected to VCC. From the middle part two Female/Female wires are connected to the sensor that is burried in the soil of the plant. ## Platform I used pybytes as my platform. It´s wi-fi based and free to use. The data is saved for 30 days, but this project isn't really in need of data that is saved for longer. It's interesting to see how the level of moisture change over time. That ensures that everything works, but other than that it doesn't provide much more use. ## The code ```python= import time #So we can use the sleep function import machine from machine import Pin # Pin object to configure pins from machine import ADC # To make a ADC object import pycom # ``` - Things we initialize before the loop: ```python=6 pycom.heartbeat(False) # Just make the LED not being light up ao_pin = Pin('P20', mode=Pin.IN) # Pin we want to read the data from p_out = Pin('P4', mode=Pin.OUT, pull=Pin.PULL_DOWN) # Output Pin we want to turn on/off ``` - The loop: ```python=11 #A loop that never stops while True: p_out.value(1) #Turn on the output time.sleep(5) #Make sure the output is on adc = ADC() apin = adc.channel(pin=ao_pin, attn=ADC.ATTN_11DB) #So we can read the value from the sensor volts = apin.value() #Save the read value p_out.value(0) #Turn of the output print(volts) #Just to see that we get a value pybytes.send_signal(1, volts) # Send the data to pybytes ``` - Also in the loop: ```python=22 #Deciding when the LED should be Red, orange or Green if volts > 3000: pycom.rgbled(0xFF0000) elif volts <= 3000 and volts >= 2000: pycom.rgbled(0xFF3300) elif volts < 2000: pycom.rgbled(0x00FF00) time.sleep(600) #The loop is repeating every 10 minutes ``` ## Transmitting the data / connectivity I am sending data to pybytes every 10 minutes, although the rate to send data doesn't matter that much. The soil moisture level doesn't change fast, it can take some time between the reading of the values without missing some major changes, but it should at least be sending the data a few couple of times a day. The transmission to pybytes use a wi-fi connection and MQTT as transport protocol. ## Presenting the data ![](https://i.imgur.com/q8MAOsl.png) The data is gathered over a few couple of days. Even though it's a very small change, it can be seen that the value increases a little bit over time which means the soil is drying up. Gathering data over a longer time would make it easier to see. The drop in the graph is when I did water my plant, the lower the value the more moist it is. This is a good indicator that this project works. As explained in the code, the data is collected every 10 minutes. As seen in the graph there is two big gaps when data wasn't collected due to the usb-cable being unplugged:) ## Finalizing the design ![](https://i.imgur.com/KMH3H8A.jpg) I feel like despite doing something kind of easy I did still learn a lot from this project. I was a complete beginner in this field so I had no idea what to expect. I am therefore glad that I did this project, because if I would have gotten ahead of myself, I may never have completed it and wouldn't have learned anything. ![](https://i.imgur.com/Whph8tH.jpg) There is a lot of things I could have done in another way, and to expand the project, but that's what I like about it, that I can work on it and improve the project. For example, I will improve the infrastructure by making a cage for all the components to protect them and making it look better. Also having longer wires so that everything doesn't have to be close to the plant. I want to change from the LED lights to something that is a better indicator of when to water the plant, like a display or maybe even a notification on the mobile. I really do think that the LED serves its purpose though. The most crucial part would also be to adjust the values to the correct moisture levels. For this it's a good idea to buy a better sensor. Finally I want to expand this to all my plants and for that I would consider choosing another platform to monitor all the plants.