---
# System prepended metadata

title: Let the Light Indicate When To Water the Flower - Humidity Sensor

---

# Let the Light Indicate When To Water the Flower - Humidity Sensor
```
By Michaela Persson (mp223qc)
```
## Project overview 

The aim with this project is to measure the moisture in soil and get a LED diode to lighten depending on the result. If the plant is fine the LED diode is green, when it does not harm to give it water it is yellow and when it does need water the light is red. 

Estimated time to do this project by following this tutorial is 2,5 hours. 

## Objective 
I have a olive tree that is not very happy and I do not really know what is wrong, it might be that I water too often or too seldom. If I knew the humidity in the soil I would know when to water the tree. Therefore, I chose to make this project, and hopefully it will save my olive tree too.

The purpose of this project is to get a diode to change color when the soil is too dry. This will indicate that it is time to water the flower. The diode is green when the the soil is wet (the tree is living life), yellow when it soon needs water and red when it is thirsty. The tree should be watered when the diode is red. 

I think this project will give me insights in the way that sensors communicate and about data collection. 

## Material 

List of needed material:

| Component                                                        | Site                     | Price    |
| ---------------------------------------------------------------- |:-------|------|
| LoPy4 with headers-kit (inc. expansion board and antennea)| [:link:][LoPy4]|500 SEK|
| Soil Moisture Sensor| [:link:][Moisture] | 29 SEK|
| Battery Holder | [:link:][Battery]| 15 SEK|
| 2 AAA batteries | [:link:][Clas Olsson]| 8 SEK|
| 1 red, 1 yelow, 1 greed LED diode| [:link:][LED]| 6 SEK | 
| 2 resistors 330 ohm | [:link:][electrokit] | 4 SEK | 
| 10 soft wires | [:link:][softwires]|36 SEK|
| 3 wires with different head and tail |[:link:][wires]| 29 SEK
|Total cost | | 627 SEK | 


[LoPy4]:
https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-basic-bundle/
[Moisture]:
https://www.electrokit.com/produkt/jordfuktighetssensor/
[Battery]:
https://www.electrokit.com/produkt/batterihallare-2xaa-box-med-strombrytare-och-jst-xh-kontakt/

[Clas Olsson]:
https://www.clasohlson.com/se/Alkaliskt-batteri-AAA-LR03-Clas-Ohlson/p/36-5486-10?gclid=Cj0KCQjw3ZX4BRDmARIsAFYh7ZJclAjFmM2zsGOcfUzGUm5PjlKvQBLnqR9EFCu1SsR_7zJt5MOFQToaAhOmEALw_wcB

[softwires]:
https://www.electrokit.com/produkt/kopplingstrad-byglar-for-kopplingsdack-mjuka-65st/

[wires]:
https://www.electrokit.com/produkt/labbsladd-20-pin-15cm-hona-hane/

[LED]:
https://www.electrokit.com/produkt/led-rod-5mm-standard-diffus/

[electrokit]: 
https://www.electrokit.com/produkt/motstand-metallfilm-0-125w-1-330ohm-330r/

The components are mainly bought from electrokit.com, for more detailed information follow the links in the table above. 

I have chosen to work with the Pycom LoPy4 device in this project. It is a small device programmed by MicroPython and has several bands of connectivity. The device is good to use for an IOT project and it has many digital and analog input and outputs. See picture 1. 

![Picture 1](https://i.imgur.com/YcCK5Cf.png)
*Picture 1*

The soil moisture sensor is set up with two components. An electric board and a probe with two pads that measures the water content. The voltage that the sensor outputs changes on the water content in the soil. If the soil is wet the voltage decreases and if the soil is dry the voltage increases. See picture 2. 

![Picture 2](https://i.imgur.com/RV6FlKy.jpg)
*Picture 2*


The soft wires are mainly uses to connect the LoPy4 with the breadboard and the breadboard to the sensor. It is possible to directly connect the sensor to the LoPy4 device but it is more clean to use the breadboard. Especially when to connect the LED diodes. 


These are all the needed components:

![](https://i.imgur.com/RinCwXL.jpg)


## Computer setup

The device is programmed with MicroPython and I have used Visual Studio Code because that is what I normally use when coding. I then installed the Pymakr plugin which adds a REPL console to the terminal that connects to the Pycom board. Now you are ready to start!

Start by connecting the Pycom device with the USB-cord to the computer. When the light on the device starts to flash blue it has connection. When you have your code you can either select to run it or upload it to the device. By selecting run you do not save anything on the device, it is good to use to try out new code. When you want to upload the files to the device it is important that your main file is called "main.py", otherwise you will not be able to upload the code to the Pycom device. The first time I tried to upload the file it said "uploading failed", this seems to happens sometimes. I tried to upload again and after a few times it worked. 


## Putting everything together

![](https://i.imgur.com/SmF5aC4.jpg)

The sensor is connected to the breadboard to make it more neat and get a longer distance for the cords. The ground (gpn) from the pycomeboard is connected to the negative part of the breadboard to make it possible to connect the diodes. Look at the scheme to see how the diodes are connected. The resistors are connected to the red and yellow light, the green light did not get strong enough if a resistor was used because the resistors are a bit to strong for this circuit and take too much current from the diode.  

## Platform 

I have chosen to use Pybytes as my platform. It is a free cloud-based device management platform that is very user friendly. The platform is a bit basic but it works fine for my project. 


## The Code

```python=
from machine import ADC, Pin #import machine 
import machine
import time

def main():

    adc = machine.ADC()             # create an ADC object
    apin = adc.channel(pin='P16', attn = ADC.ATTN_11DB)   # create an analog pin on P16
    val = apin() #read an analog value
    print(val)

    p_green = Pin('P10', mode=Pin.OUT) #create a output at P10
    p_yellow = Pin('P9', mode=Pin.OUT) #create a output at P9
    p_red = Pin('P8', mode=Pin.OUT) #create a output at P8

    while True:
        val = apin()
        wet_procent = procentCalc(val)
        chooseDiod(val,p_green,p_yellow,p_red) #run the function that turn on the diodes 
        print(val)
        pybytes.send_signal(1,val) #send signal to bybytes
        pybytes.send_signal(2,wet_procent) #send signal to pybytes

        time.sleep(10) #wait 10 sec before taking the next measurement

    
def chooseDiod(val, p_green, p_yellow, p_red):

    if val < 3300:
        p_red.value(0) 
        p_green.value(1) #1 = turn on diode, 0 = turn off diode 
        p_yellow.value(0)
    
    elif val <= 3350 and val >= 3300:
        p_yellow.value(1)
        p_green.value(0)
        p_red.value(0)

    elif val > 3350:
        p_green.value(0)
        p_red.value(1)
        p_yellow.value(0)
    
    else:
        None
    return

def procentCalc(val): #calculate the waterprocentage in the soil

    maximum = 4095 # 100% dry
    #minimum = 1005 # 0% dry 

    dry_procent = (val/maximum)*100
    wet_procent = 100-dry_procent

    print(wet_procent)

    return str(round(wet_procent,2)) #return the value with 2 decimals


if __name__ == "__main__":
    main()
```


## Transmitting the Data and Connectivity

I have chosen to use WiFi as my wireless protocol and are using MQTT as transport protocol. My data is sent every 10th second, which was good when developing the project but more reasonable would be to sent every 30 minute because it is a measurement that does not change that fast. The optimal would have been to send data every 10th second when the diode is red, because you want to get an immediate feedback when you have watered the plant, and when it is green every 30 minute. 


## Presenting the Data

![](https://i.imgur.com/fE7pRjY.png)
![](https://i.imgur.com/gCftL3k.png)

This is how my dashboard is presented at PyBytes. It shows the water percentage of the soil and the value of the sensor measurement. It also shows the amount data received.

The data is saved in the database every 10th second. That is actually a bit unnecessary because of the same reason as discussed before. The water content of the soil doesn't change that rapid except for when the flower is being watered. It would therefore be more efficient to save data less often. A good metric could be 2 times per hour. 


## Finalizing the Design

![](https://i.imgur.com/vMCGfa4.jpg)
*Diode is red, tree needs water.*
![](https://i.imgur.com/Jtbf5r0.jpg)
*Closer look on the sensor before watering.*
![](https://i.imgur.com/24cUVBE.jpg)
*The tree is happy after watering, diode is green.*
![](https://i.imgur.com/slAzy6b.jpg)
*Closer look on the sensor after watering.*

### Conclusion 
The final result was positive. I managed to get the sensor to work correctly and the result was satisfying. To make a prettier solution smaller connecting wires could have been used for the breadboard. When I started to read more about the sensor I realized that this sensor is not made to be in the soil all the time, which sort of defeats the purpose why I wanted to use the sensor. I have got to the conclusion that I will see how long it lasts before it starts to be not accurate anymore and when that happens buy a sensor that is made for being constantly in the soil. I also realized that the batteries runs out sort of quickly therefore, it would have been smarter to go with rechargeable batteries. 