---
# System prepended metadata

title: Tutorial on how to set up a moisture sensor in a plant

---

# Tutorial on how to set up a moisture sensor in a plant
For the project a sensor measuring the moisture of plants in soil will be used to fullfill the purpose of inidcating when water is needed for the plant.

It is quite difficult to calculate the exact time of the porject because I was very new to it all and some things took a lot longer time than they should have. The first step was to order everything, then read documentations on setup, then I needed some help aswell and then comes the whole trial and error-phase wich can go on forever. I would approximate that I spent a good 5 weekdays, excluding the time for project-writing.

The reason I chose this project was because it was the first idea I had when I was accepted to the course. My mind got stuck on the challenge and I had to build it to prove I could. I also found it interesting seeing as it has such an everyday appliance.

Having plants in the home has been trendy for a very long time. There is something very welcoming and soothing about the extra oxygen in ones home. A common topic of discussion amongst my circle is the fact that students never manage to even keep a cactus alive. I felt that I needed to do something about this, build a device to monitor what is actually happening.

I believe that this type of data will generate great insights to how plants actually consume water. The plan was to use the device to measure different plants att different times and then use that data to create perfect watering schedules. That is not exactly the way it all played out but more about that later.

An important point on this project is the environment. This device will help keep plants alive and stop people from buying new ones all the time. The plants often come in plastic containers and that is just unneccessary. Buying new fresh herbs each time they are desired is a waste that can be avoided by this monitoring device, by keeping your own one alive instead!.

## Material

To have att home:
- Several different plants grown in soil
- MicroUSB to USBC/USB(depening on your computer) - cable
- Space for the setup

To order:
- Moisture sensor - Electrokit - Art.nr: 41015738 - 29kr
- LoPy4 Multipack with Expansion Board - Pycom - €56.40

I also bought 25 other sensors, a battery pack and some other things but they have still not arrived so I have not been able to use these. The connecting cables were included in the moisture sensor but they did not show up either so I bought these from Kjell&Company: https://www.kjell.com/se/produkter/el-verktyg/elektronik/elektroniklabb/delbar-kopplingskabel-40-pol-15-cm-hane-hona-p87076

![](https://i.imgur.com/mwVBzBv.png)
Figure 1 - Mositure Sensor - image Electrokit
![](https://i.imgur.com/vI7J9KS.png)
Figure 2 - Pycom LoPy4 - image Pycom.io
![](https://i.imgur.com/JX08ety.png)
Figure 3 - Pycom Expansion Board - image Pycom.io

In this project I have chosen to work with the Pycom LoPy4 device as seen in Figure 2, it’s a neat little device programmed by MicroPython and has several bands of connectivity. The device has many digital and analog input and outputs and is well suited for an IoT project.

## Computer setup
I installed the firmware and ofcourse updated the Lopy4. I chose Atom to write code in and it was a very straight forward-process of downloading and connecting to the device. I wrote some different code episodes and uploaded to my device, just to get to know Python and Pycom, since I have only used Java before. This was very fun and educational. 

## Putting everything together
The sensor that I bought was not obviously compatible with Pycom so I needed a bit of help with the setup beacuse I could not use the one given in the documentation. This is because the sensor could not be energized all the time. The best connection ended up being:
VCC - VIN
GND - GND
AO - P10
This is visualised below. In figure 5 you are able to see the plus and minus-signs. These where connected to each others opposite when I tried it for the first time, wich is the evidence that the sensor probably suffered a short circuit. More on this later.

![](https://i.imgur.com/sA86TiF.jpg)
Figure 4 - The connections - own image
![](https://i.imgur.com/42y0kVP.jpg)
Figure 5 - More connections - own image
![](https://i.imgur.com/ED5ja3Q.jpg)
Figure 6 - When running - own image

## The code

The code gives a measured value when it is run through once. As it is a while true-loop it will keep doing this forever. How often a value is printed is regulated by the amount of "sleep" that is used in the code plus the time that the operations take, wich here are tiny.

The code tells the LoPy4 wich information to collect from where, from wich pin for example and then it prints it out in the control box in the interface of atom. 

```python=
import machine
import pycom
import time
from machine import Pin

pycom.heartbeat(False)
p_out = Pin('P9', mode = Pin.OUT, pull = Pin.PULL_DOWN)

adc = machine.ADC()             # create an ADC object
apin = adc.channel(pin='P10')   # create an analog pin on P10 val = apin()
while True:
    p_out.value(1)
    time.sleep(0.2)
    volts = apin()
    p_out.value(0)
    print('moisture ',volts)
    time.sleep(2)
```
## Presenting the data
Because of the short circuit there was no data to present. The plan however was to export the data to excel and to create pivot charts visualising data from differant plants undergoing different circumstances. This would then be used as a reference table when wanting to find out wich state other plants are in. The further development of this project would be connecting to a more advanced plattform and with some watering deviced being able to automize the watering schedules. This I plan on doing in the close future seeing as I now have all the knowledge for it.

## Finalizing the design
The problem with this project occured whilst first testing the circuit. The values that started being produced were att first differing a bit from one another and I thought it was calibrating, I got about 3078 and 3456. Then it all changed and the only value it gave me was 4095, independent of the sensors placement in water or dry soil or wet soil. This was not what I wanted and the teacher assistent told me that the only logic reason was a short circuit in the beginning, so the sensor was now ruined. I have not been able to get a new one in this time frame but I have other sensors on the way and this project has only inpsired me to do more with IOT in the future!

### Fanny Holmqvist
fh222ts
