owned this note
owned this note
Published
Linked with GitHub
## Soil moisture sensor with a pulse controlled valve for water control.
**Date:** 2023-07-02
**Author:** Sam Valashjardi - Sv222rk
**Course:** Introduction to Applied IoT- 1DT305 at Linnaeus University
**Estimated setup time:** I would say between 5 to 7 hours
## Overview
![](https://hackmd.io/_uploads/Bkt3JVgt2.jpg)
**This** project tutorial will aim to give you guidance on how to measure the moisture in soil, and with the data collected from the soil sensor then open the water valve from our pulse-controlled valve and water our plants. At first, I was a bit torn whether to base the opening of the valve on the collected data from the sensor, or to set a timer to open the valve and keep the soil sensor for the measuring of moisture in the soil. I choose to control the valve manually through Adafruit.
I will be using a Raspberry Pi Pico WH, a Bodenfeuchtesensor Hygrometer Boden Feuchtigkeit Module soil moisture sensor and pulse-controlled valve. More detailed list of all the material will be presented later on in the tutorial.
In addition to presenting the data in [Adafruit.io](https://io.adafruit.com/) I will also use a webhook to my discord server to get an updated notification from Adafruit.
## Objective
### So why this project?
I keep forgetting to water my plants, all the time. I have even managed to kill a cactus.
So it was a no-brainer that this project was the right one to create, in order to keep my plants happy and healthy. Automatization has always been a passion and hobby of mine and that reflects in my smart home. Robot-vacuum, Hue-lights, smart-TV & soundbar, security cameras and NFC-tags and so forth all over the place with automatization configurations.
I was also inspired by a colleague of mine who has a greenhouse and has been looking for a similar solution to his problem, which is forgetting to water some of the plants. He will use this tutorial to replicate this project later on, with some of his own tweaks.
### Purpose you ask?
Other than to create a system that water my plants, the purpose and goal is to study and learn as much as possible of the roadmap to IoT. With that I mean from step A to Z. How to collect and store data, then how to use it in an efficient way to make your life, or even the world a better place.
### The insight that this project might bring:
Hopefully this project and tutorial will bring some insight and understanding in one of the ways that you can use microcontrollers, sensors, a pulse valve and a bit of microPython as well. The goals are that you have hopefully learned something new at the end of this tutorial report. If you haven't, then I will most likely receive a lower grade.
So I urge you to press this [**link**](https://www.developgoodhabits.com/new-skills-to-learn/) so that I can receive my A+.
## Material
| Material | Function | Where to buy & Price | Picture |
|:--------------------------------------------------------------------------------:| --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | --------------------------------------------- |
| **Raspberry Pi Pico WH** | The Raspberry Pi Pico WH is a microcontroller board. It allows you to program and control various components, making it versatile for a wide range of projects. | [Electrokit.com](https://www.electrokit.com/produkt/raspberry-pi-pico-wh/) 109:- | ![](https://hackmd.io/_uploads/r1hC17w_h.jpg) |
| **Valve for water control (5V) - Pulse controlled** | Pulse-controlled valve that can turn water on and off with a microcomputer or Raspberry Pi | [Styrahem.se](https://www.styrahem.se/p/344/ventil-for-vattenstyrning-5v-pulsstyrd) 299:- | ![](https://hackmd.io/_uploads/rJH3emDdh.jpg) |
| **Relay board (2 relays/channels)** | For controlling equipment from a microcomputer (has optical isolator) | [Styrahem.se](https://www.styrahem.se/p/349/relakort-2-relaerkanaler) 59:- | ![](https://hackmd.io/_uploads/r1v5wXPO2.jpg) |
| **Bodenfeuchtesensor Hygrometer Boden Feuchtigkeit Module soil moisture sensor** | Used to accurately measure the moisture in the substrate | [Amazon.com](https://www.amazon.com.be/-/en/AZDelivery-Moisture-Hygrometer-Compatible-Raspberry/dp/B07V6SZYZW) 49:- | ![](https://hackmd.io/_uploads/ry7_YQvdn.jpg) |
| **Broadboard 840 connections** | High-quality connection decks for solder-free connections | [Electrokit.com](https://www.electrokit.com/produkt/kopplingsdack-840-anslutningar/) 69:- | ![](https://hackmd.io/_uploads/BkJEqmDd3.jpg) |
| **Lab cable 40-pin 30cm male/male** | 40-pin lab cord with connectors at both ends for connector decks and pin/sleeve strips | [Elektrokit.com](https://www.electrokit.com/produkt/labbsladd-40-pin-30cm-hane-hane/) 49:- | ![](https://hackmd.io/_uploads/ryysqXwuh.jpg) |
| **USB Cable (USB-A to Micro-USB)** | Data capable USB cable | Had one at home | ![](https://hackmd.io/_uploads/HJiM9wPun.jpg) |
| **Water horse** | Transfer the water from point A to B | Had the material at home | ![](https://hackmd.io/_uploads/H13voRc_h.png) |
## Computer setup
Windows 10, 2 screens, mouse, keyboard, headset, and shit for brains.
### Setting up the IDE, Extensions & other installations:
My choice of IDE is Visual studio code (that you can download from [**here**](https://code.visualstudio.com/download)) because I have the most experience using it. I already have all my favourite extensions installed as well. There are several other very good IDE's that you can use, such as Atom, Thonny, Arduino IDE 2.1.0 and so on
After you have chosen your IDE and installed it, head over [**here**](https://nodejs.org/en) and install Node.js, and what is that you ask?
Node.js is an open-source, cross-platform JavaScript runtime environment and library for running web applications outside the client's browser.
### Flashing our Raspberry Pi Pico Firmware:
Next step is to flash our Raspberry Pi Pico with the latest firmware. And [**here**](https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/3) is an easy and detailed guide on how to do that.
You will find the latest [**firmware here**](https://micropython.org/download/rp2-pico/)
Now we need to install the extension [**Pymakr**](https://docs.pycom.io/pybytes/pymakr-online/) (<-- link to some information and install guide) on our IDE. This makes it possible for us to communicate with our Raspberry Pi Pico using microPython.
Afterwards we will connect the Raspberry Pi Pico and create a project so that we can start implementing code.
![](https://hackmd.io/_uploads/SkQw5kj_2.png)
**Steg/Step 1:** Installing Pymakr
1. Navigate to the extensions tab
2. Type "pymakr" in the search bar
3. Install it
**Steg/Step 2:** Connecting our device
1. Navigate to the Pymakr tab
2. Find your Raspberry Pi Pico in the list and press 'Connect device'
3. Press the terminal so it shows
4. Check that we are connected to the device in the terminal
**Steg/Step 3 & 4:** Create a project
1. Navigate to the Pymakr tab
2. Press the '+' sign, if you don’t have a project active, you will have a blue box that says "Create project". Press it
3. Chose a location for your project and name it something appropriate, and after you press 'Enter' for your project name and chose 'Empty' for template.
**Steg/Step 5 & 6:** Add your device to the project and enter Development mode.
1. Navigate to Pymakr tab
2. In the list under "Empty project" click in 'ADD DEVICES' and then select your Raspberry pi Pico (it can say USB Serial Device (COM#) here, you'll just have to find your device.)
3. Press 'Enter' or the blue OK button.
4. Go back to the 'Empty project' list and next to the lightning bolt you have a icon that looks like </> this. Click it in order to enter Development mode.
#### Done.
## Putting everything together
So let’s connect all the materials and components together as meticulous as we possibly can. Because it's important to understand how the wiring should be connected, so that you don't risk damaging the board and components.
The colour of the wires does not matter.
The setup is shown in the figure below. Note that it is not the exact same component models, but the wiring and spots where you connect the wires are.
![](https://hackmd.io/_uploads/r1gqc111Kh.png)
#### Setting up the board:
1. Connect your Raspberry Pi Pico to the bread board
2. Start with giving your bread bord 5V by connecting one end of the wire to pin40 and the other end to '+' row, shown in the figure above.
3. Lets ground the '-' row as well by wiring pin38 to '-'.
#### Setting up the soil moisture sensor to the comparator board:
![](https://hackmd.io/_uploads/r1QzEyyKh.png)
*Grab 4 wires that are male to female and 2 wires that are female to female.*
1. Connect the VCC (female part of the wire) from the comparator board to '+' row on the bread board.
2. Connect the GND (female part of the wire)from the comparator board to '-' row on the bread board.
3. Grab 2 wires and connect 'D0' and 'A0' to pins 32 & 31.
4. Connect the 2 pins from the comparator board to the soil sensor with 2 female to female wires.
#### Setting up water valve --> relay --> bread board:
The figure below will show you how I connected the relay.
![](https://hackmd.io/_uploads/SynNh1JFn.png)
*Lets start off with the Relay --> Bread board side of the picture.*
1. Connect the VCC pin on the relay to a pin on the '+' row on the bread board.
2. Connect the GND pin on the relay to a pin on the '-' row on the bread board.
3. Take 2 wires and connect 'IN1' & 'IN2' to pins 1 & 2 on the bread board.
*Now we move on to Relay --> water valve side of the picture.*
1. Connect a wire from 'NO1' to 'NO2' for power.
2. Connect a wire from 'NC1' to 'NC2' for grounding.
3. You will then connect the water valves black wire to 'COM2' and the red wire to 'COM1'.
4. Then you take the wires from the power supply, connect the red wire to 'NO1' for power and the black wire to 'NC1' for grounding.
Please note that this setup is suitable for development and hobby usage only.
There are better sensors for production.
## Platform
![](https://hackmd.io/_uploads/HJjTtxJFh.png)
As I mentioned earlier in the report, a free account at Adafruit IO is the platform of choice for this project. I can't provide a comparison to other platforms, since I did not try any.
**So what is Adafruit IO?**
Adafruit.io is needed to display the IOT project’s data online in real-time. It is a cloud server with MQTT broker that can be used to connect to IoT devices through Wi-Fi and to control these devices through a dashboard. It can be used as a free service and it has got a simple easy-to-use interface to design dashboards.
*Source:* https://www.tutorialspoint.com/the-adafruit-io
Press the Adafruit link stated above and create a free account.
## Code
**Before** we start going over the code, I would like to give praise to the teaching-assistants in this course. I have never encountered such a group of helpful, kind, patient, understanding, driven and talented people in my academic life before. They really took the time to explain and made sure that you understand everything from material, setup and the code.
I have learned immensely thanks to them. Special shoutout to Jonas & Hamed for their patience and expertise.
First of all we have to import the necessary libraries that is needed in order for the project to work. We have the library 'Machine' which is used for connecting to our raspberry Pi Pico. 'Network' is used to be able to connect our device to Wi-Fi, 'Secret' is a file I created in order to protect sensitive information from being available in main part of the code (Wi-Fi credentials).
We also have 'mqtt' for sending data over the internet to our platform.
![](https://hackmd.io/_uploads/HktG4ZyY3.png)
#### Relay code:
I have defined the relay pins that we connected earlier (Pin1 & Pin2).
Then we have 3 functions for the control of the relay.
**relay_high():** Will activate relay Pin1 and deactivate relay Pin2, which will cause the water valve to open up.
**relay_low():** Will activate relay Pin2 and deactivate relay Pin1, which will cause the water valve to close.
**relay_off():** Will deactivate both relays and turn off the valve.
#### Soil moisture sensor code:
![](https://hackmd.io/_uploads/Hko5LbyY2.png)
We define soil = ADC (which is imported from machine) to Pin27 in order to get the data from our sensor. We also define our max and min value for our sensor readings with a reading delay at 15 seconds.
Then we have our function that calculates and the moisture readings and prints it out for us.
Last step is to publish the value to Adafruit through the feed.
#### Callback function and messages:
![](https://hackmd.io/_uploads/Hkyn1f1K2.png)
So the callback function **sub_cb** will handle the subscribed messages.
We have created a feed in Adafruit called **TOGGLE**, and when we send the message **"ON"** it will open the valve, since we implemented the function **relay_high** there, and the same goes for the message **"OFF"** with **relay_low**.
Then we have the code that tries to set up a connection between the device and adafuit by using MQTT.
Lastly, we have a while loop that calls **client.check_msg** and our **soil_sensor** function.
The entirely of the code will be published at my [**Github profile**](https://github.com/Samiieh/Applied-IoT-1DT305).
## Transmitting the data & connectivity
I have chosen to send the soil measure data once an hour to Adafruit and will then get notified on my discord channel with the help of a webhook that I installed. I could have chosen to get the notification when the moisture value in the soil hits a minimum and maximum value as well, but I feel that an hourly update will suffices.
I have also installed a webhook for every time the water valve opens and closes.
![](https://hackmd.io/_uploads/r1tzSglY3.png)
![](https://hackmd.io/_uploads/Bk1XrxgYh.png)
*Pictures of my webhook actions and notifications on my discord channel*
The Internet connection to my device is a good old wireless protocol on my home Wi-Fi network. My project is fairly close to my router as well, and with less bandwidth restrictions and low latency, it's the obvious choice. I tried connecting my project to the Hotspot on my phone as well and it worked just fine.
![](https://hackmd.io/_uploads/H1n5LWlt2.png)
*Here is a code snippet of the Wi-Fi setup*
As for the transport protocol, MQTT is used for sending the data measured by the soil sensor to Adafruit. It is a stable and good transport protocol to use, and we got it from the course roadmap.
## Presenting the data:
![](https://hackmd.io/_uploads/r1Qbvbet3.png)
*My adafruit dashboard with a gauge & chart for the soil moisture data and a toggle for the water valve*
Here is the data being presented in Adafruit IO dashboard. We see the value of moisture in % in the gauge block and chart values underneath (same values, different presentation). We also have a toggle block for turning the water valve ON and OFF.
Each feed preserves the data given for 30 days. I have chosen to display the data from the last 24 hours in my dashboard.
![](https://hackmd.io/_uploads/ry04OZgt2.png)
Here is a graph illustrating the fate of my plant.
## Finalizing the design
We are almost there! Here are some pictures of my project.
![](https://hackmd.io/_uploads/B1DjAMlFn.jpg)
![](https://hackmd.io/_uploads/HJCpRMeYn.jpg)
![](https://hackmd.io/_uploads/rJ6b1QeK3.jpg)
![](https://hackmd.io/_uploads/B1eYQVxF2.jpg)
![](https://hackmd.io/_uploads/Sy6sQNlF3.jpg)
### Thoughts and conclusion
All in all, I'm very happy with my performance in this course and project, considering the fact that I don't have that much experience with IoT. I'm proud of my project and what I accomplished with it.
I do have some future plans as well, 3D-print a case, dust & water secure all the wires, cabels and components, perhaps buy a watering system with drip function so that I can water multiple plants simultaneously.
The online lectures and roadmap given to us were really good, high quality and easy to understand and use. I never had any doubts about what to do and what to study, the quizzes were also fun and educational.
Thank you for your time.