---
# System prepended metadata

title: Soil Humidity project

---

# Soil Humidity project
Oscar Andersson, Oa222nc

In this project i have made a circuit that can measure the humidity in the soil of a plant so i know when the plant needs to be watered or not. It also has a RGB (red,green,blue) LED light to indicate the plant's needs.

If you have all of the components ready this project will not take more than a day to complete but since i had to order some it took a bit longer.

I choose this project because in my apartment i have a small tree that is now about 10 years old and during some periods i'm not home very much so the whole purpose of this project is so that i can keep an eye on my tree at all times.
I think the insight this will give is that plants do not need as much water as i believe, and also how much cool and smart stuff you can build with relativly cheap parts and limited knowledge.

# Materials
* Pycom LoPy4
* Pycom expanssion board v3.1
* Breadboard
* Wires
* Soil moisture sensor
* RGB LED light

# specifications
 
 The Pycom **LoPy4** is a smart microcontroller programmed in Micropython and has many options for connectivity such as WiFi, LoRa, Sigfox and Bluetooth which makes it perfect for IoT projects.
 ![](https://i.imgur.com/q9gTaOj.png)

Through the Pycom **expansionboard v3.1** you connect your computer to the LoPy4 and are able to program it and also power it. It makes connecting sensors and wires much easier. It also makes it possible to power your microcontroller with a battery.
![](https://i.imgur.com/IPwOedf.png)

The **breadboard** is used to easier connect wires and sensors and especially if you are using many sensors at the same time.
![](https://i.imgur.com/0PQ0PWx.png)
The **soil moisture sensor** has two legs and sends a current from one leg to the other and measures the resistance between them, wet soil offers lower resistance and dry soil higher resistance. My sensor can give both an analog (**how** dry or wet the soil is) and digital (if the soil is wet **or** dry) value. i Choose to use the digital value.
![](https://i.imgur.com/ppuwJ8j.png)

The **RGB LED** can show red green and blue but i only use red and green to show when the plant needs water and not.
![](https://i.imgur.com/8Q4Exhf.png)

|Component|Price|
|--------|--------|
|LoPy4|949Kr (Included in LoPy4 and sensor Bundle)|
|Expanssion board| 949Kr (Included in LoPy4 and sensor Bundle)|
|Breadboard|949Kr (Included in LoPy4 and sensor Bundle)|
|Wires|33Kr|
|Soil moisture sensor|29Kr|
|RGB LED light|19Kr| 

**All components is from Electrokit.com**

# Computer setup
**Visual Studio Code on Windows 10**
For this project i have chosen to use Microsoft Visual Studio Code as my editor, i chose this mainly because it was already installed on my computer and i was more familiar with it then the other available editor which is Atom. After you have installed VSCode from https://code.visualstudio.com/ you also need to install Node.js from https://nodejs.org/ to be able to program your LoPy.  Before you use your LoPy4 and expansion board make sure they are updated, all information and instructions on how to update your modules can be found at https://docs.pycom.io/. Now it is time to open VScode and install the Pymakr plugin and that will make uploading, downloading and running your micropython code super easy. In the menu on the left side, you should find **extensions** or press ctrl+shift+X, search for Pymakr and install it.

When writing your code, it is important to understand the order in which the computer will be reading your files. First you can create a library folder, here you can import all the necessary libraries you need for your sensors to make the coding much easier. In my case i didnt need any librarys. Second you have your boot.py file and this is the file that the computer reads first upon start. The boot file can for example be used to connect to WiFi every time you start your device. Lastly is the main file and it is here you write your code and giving the microcontroller the main instructions of what to do. 

Now you are ready to write some code and upload it to your microcontroller via the micro-USB cable that you connect to the expansionboard. This is very easy thanks to the blue bar at the bottom of your screen that we got from installing pymakr.

# Putting everything together
Here is the show circuit diagram for my project.
![](https://i.imgur.com/MPxxO7a.jpg)
I am running both the sensor and the LED on the regular 3V that you get from the expansion board and it works just fine. 

Begin with unplugging the power source for the expansionboard and connect one wire from **3V3** on the expansionboard to the + row on the breadboard and one wire from **GND** to the - row on the breadboard i also put two wires from the +/- row out onto the breadboard to make it more spacious. The RGB LED is very simple to connect, it has four pins R, G, B and -. Connect the colours you want to use to the expansionboard and in my case i connected R to PIN 10 and G to PIN 9 and the - pin is connected to the - row on the breadboard. The soil moisture sensor is also very easy to connect and it is done in two parts. First you have to connect the resistance to voltage transformer. This has four pins on one side and two pins on the other side, the four pins are labeled AO,DO,GND and VCC and we want to connect the GND pin to - on the breadboard and VCC to + on the breadboard. I used the DO (digital output) pin and connected that to PIN 16 on the expansionboard. Lastly we connect the two remaining pins which are labeled + and - to the actual fork looking sensor that we later put in the soil. The transformer has a small dial on the top and here you can adjust the threshold of when the sensor should indicate wet or dry.

Currently i do not have a battery for the device but for a more portable soulution a LiPo battery could be used, this will also give you a long batterytime.

**Things to consider**
The red light on the LED has a working voltage off 1.8V to 2.1V so running it on 3V will possibly shorten its lifetime some. The soil sensor can be used on 5V but works very good on 3V as well.

# Platform
For this project i am using pybytes as my platform, pybytes is a free cloud-based service where you can register your pycom devices and manage them in a easy way. You can read sensor data sent from your device, visualize data in charts and diagrams, update firmware and much more. Pybytes also offers an app where you can manage all your devices quick and easy. For smaller amounts of data (5MB) pybytes is free so if you build something that you want to scale up or just want more functions you would have to pay for using the service.

To get started using Pybytes you first need to register an account. Plug in your microcontroller to your computer and in pybyte you go to **all devices** -> **add via USB** and then follow the instructions. when you have added your device you need to configure your network, this is done by pressing **configurations** -> **networks** and then you fill in your WiFi name and password. Now your device can get access to your WiFi and you can send data to Pybyte. 

# The code
The code for the project is very simple but does the job without issues.

**Boot file**
The boot file connects to my home WiFi, red part is my WiFi password.
![](https://i.imgur.com/vuODRkh.jpg)

**Main file**
Main file collects the senordata, lights the LED and sends it to pybyte.
![](https://i.imgur.com/i9SwxGo.png)

# Transmitting the data
Since the device will be used in my apartment i chose to use WiFi because it is stable and easy to connect to. If you want to use it in your garden or outside LoRa would be a great choice since WiFi range is bad. When data is sent to pybyte it uses the MQTT transfer protocol. As i don't need very frequent updates on the sensor data i will only send data every 12 hours and the device will sleep in between.  

# Presenting the data
Since i send all data to Pybyte i can use the existing functions they have of presenting data, you can choose different types of graphs and tables. I choose a simple table to present my data. 
![](https://i.imgur.com/AVYRuFX.png)

With the free version of Pybyte the data will be saved for one month which is good enough for this project.

# Result
I think the project went well and a lot better then i first thought given my very limited knowledge of electronics and programming. I am sure a great deal of things could have been done in smarter and more efficient ways. If you wanted to take the project another step a small pump could be added so the plant can water itself when it needs to or add more soil sensors and use in many different plants at the same time.
![](https://i.imgur.com/6c5BYPG.jpg)
![](https://i.imgur.com/bc2EH1I.jpg)



