My DIY automatic watering system by Raspberry Pi Zero W

Electronic work

My wife is growing some plants.

yatch
yatch

Watering every day looks hard.
Is it possible to assist so she will be less tired?

I found some articles which realizes automatic watering system by Raspberry Pi.
So I tried.

Buying parts

This is the pachage which consists of necessary parts in Amazon.

Title is WayinTop Automatic Irrigation DIY Kit Self Watering System with Capacitive Soil Moisture Sensor 1 Channel 5V Relay Module and Water Pump + 1M Vinyl Tubing for Garden Plant Flower Herb Potted and it looks OK if I buy it!
It is 13.99 USD so it is in my budget!

I found the comments which he/she could use with Raspberry Pi.

I got it 2 days after ordering it.

Contents of package are below.

 - Soil moisture sensor
 - 3 pins cable of sensor
 - Relay module
 - Pump
 - Vinyl tube
Soil moisture sensor + 3 pins cabl
Relay module
2 pumps
Vinyl tube



I don’t why but there are 2 pumps even if I use only 1 pump.



Lead to connect each parts should be used, I ordered.
 ブレッドボード・ジャンパーワイヤー(メス-メス)(20cm)40本 – Japanese



This is general one.



I used Raspberry Pi Zero W which is enough to use for it.
I have pin headers so it’s good!



To use soil sensor I need A/D converter because Raspberry Pi doesn’t have A/D converter.
I bought this one.
KeeYees 2 Pack 16 Bit 4 Channel I2C IIC Analog-to-Digital ADC Converter High Precision ADS1115 PGA ADC Converter Development Board for Arduino for Raspberry Pi



I found that I need to solder pins and board.



I bought soldering kit at DAISO which is 100 JPY shop in Japan.
– Left: Soldering iron 500JPY
– Right: Solder 100 JPY




And, I need power supply for pump so I bought battery.

Preparation

First I performed soldering A/D converter and pins.
This is long time no experience from junior high school even if I am embedded engineer…



This is not smart but I could get sensor value so it’s OK.



I asked seller of watering package

Please give me wiring diagram how to connect to Raspberry Pi

Then they shared PDF soon, it’s great!

Test

I connected just to check connectivity.



Below is diagram which I connected following PDF by seller.
To connect soil sensor lead needs to be splitted to connect Raspberry Pi and A/D converter.
 Red line and light green line in below diagram
I created by peeling skin outside of leads and tied multiple leads together.



I got sample application of python.
Let’s try.

#!/usr/bin/python3

import RPi.GPIO as GPIO
import time
import Adafruit_ADS1x15
import math

adc = Adafruit_ADS1x15.ADS1115()
GAIN = 1
SENSOR_THRESHOLD = 15000

PIN = 4

values = [0] * 100

def setup():
	GPIO.setmode(GPIO.BCM)
	GPIO.setup(PIN, GPIO.OUT)
	GPIO.output(PIN, GPIO.LOW)

def loop():
	while True:
		maxValue = 0
		# read some times to avoid pulse value
		for i in range(100):
			values[i] = adc.read_adc(0, gain = GAIN)
			if values[i] > maxValue:
				maxValue = values[i]
		print("maxValue == " + str(maxValue))
		# if dry
		if (maxValue) > SENSOR_THRESHOLD:
			GPIO.output(PIN, GPIO.HIGH)
			print("Pump is ON")
		# if not dry
		else:
			GPIO.output(PIN, GPIO.LOW)
			print("Pump is OFF")
		time.sleep(0.5)

def destroy():
	GPIO.setup(PIN, GPIO.IN)
	GPIO.cleanup()

if __name__ == '__main__':
	setup()
	try:
		loop()
	except KeyboardInterrupt:
		destroy()



I uploaded to github.
Dockerfile is there because I like docker container but you can use only water.py if you don’t need container.

kurofuku/sense-and-water
Contribute to kurofuku/sense-and-water development by creating an account on GitHub.



Installing packages which are used.

sudo apt install python3-pip
sudo pip3 install Adafruit-ADS1x15



When I run water.py, /dev/i2c-1 is not found.

python3 water.py 
Traceback (most recent call last):
  File "water.py", line 6, in <module>
    adc = Adafruit_ADS1x15.ADS1115()
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_ADS1x15/ADS1x15.py", line 319, in __init__
    super(ADS1115, self).__init__(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_ADS1x15/ADS1x15.py", line 82, in __init__
    self._device = i2c.get_i2c_device(address, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_GPIO/I2C.py", line 64, in get_i2c_device
    return Device(address, busnum, i2c_interface, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_GPIO/I2C.py", line 97, in __init__
    self._bus = Adafruit_PureIO.smbus.SMBus(busnum)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/smbus.py", line 125, in __init__
    self.open(bus)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/smbus.py", line 151, in open
    self._device = open("/dev/i2c-{0}".format(bus), "r+b", buffering=0)
FileNotFoundError: [Errno 2] No such file or directory: '/dev/i2c-1'



i2c communication is inactive.
I activated it.
Rebooting Raspberry Pi is mandatory to reflect system.

vim /boot/config.txt

# Add them to bottom
dtparam=i2c1=on
dtparam=spi=on

vim /etc/modules

# Add them to bottom
i2c_bcm2835
i2c-dev



i2c communication works good.

sudo apt install i2c-tools
i2cdetect -y 1
0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --



I tried again but pump kept working and I couldn’t stop even if GPIO4 was low.



As a conclusion this is because this relay module is low level trigger.



I mistakenly assumed this relay module is high level trigger which becomes active if high voltage is supplied.

Threshold value of transitting ON <-> OFF is 3.6V-3.8V if VCC is 5V.
– I referred this site (Sorry Japanese site)


GPIO of Raspberry Pi supplies 3.3V which doesn’t reach threshold of 3.6V-3.8V.
So changing HIGH/LOW doesn’t make sense…



I found solution.
– I refered here (Sorry Japanese site)
Threshold can be 2.0V-2.4V if I supply 3.3V to relay module.
This threshold value looks working fine with GPIO LOW/HIGH.
– GPIO LOW: 0V
– GPIO HIGH: 3.3V



I tried again and worked fine!!
Pump switches ON/OFF every 1 second.



To realize it I changed connection.
– Before: VCC of relay module is connected to 5V.
– After: VCC of relay module is connected to 3.3V.

Deploy

My wife requested to deploy pot of rose and I did.

HARIBO boxes are used to put into as organized.



Let’s see short movie.
You may be able to see water is supplied via vinyl tube right side.

Issues

There are some issues which should be solve.

Water amount should be adjusted
Protection from rain/wind should be done if deploying outside
How to assure power supply of Raspberry Pi



Amount of water and condition to run pump can be controlled by below.
– Threshold value to judge soil is dry
– Duration time to keep pump on

And I also need to consider inserting wait time to assure soil becomes wet.
– 10 seconds for example
This is because if sensor measures immediately after watering perhaps soil doesn’t wet enough.
– My assumption



And as you can imagine from above photo, parts will be broken if strong wind/rain comes.

The easiest solution is to deploy inside.
This should be discussed my wife.

For this time I wrapped by plastic bag.
Of course this is tentative solution so I need to fix soon.



Power supply of Raspberry Pi is another headache.
If inside no problem because we have many sockets for power supply.
But if outside this is not easy.

Mobile battery like this is one solution.
Recently large capacity battery like 40000mAh is available which can work 1 week.

There is solar panel to charge on battery.
So if solar panel can charge more than consuming this will be perfectly resolved.

But,,, fact is not ideal…

To charge from empty to full takes about 300 hours which means about 24 days.
Effeciency will be lower in cloudy/rainy day than sunny day.



Or I may be able to use socket of power supply which is outside home.

Conclusion

How was it?

I want add features like this!
– If water becomes empty notification will be sent by email or push notification by LINE or etc.
– Taking photoes every day and upload to nextcloud.

Comments

タイトルとURLをコピーしました