didnt need the relay after all, the 2n7000 did the trick!
btw cant upload the mp4 video

Code: Select all
import machine
from machine import Pin
from machine import ADC
import time
from time import sleep
moisture = ADC(0) #declare soil sensor
pump = Pin(4, Pin.OUT) #declare pin 4 as output, naming it pump
dry_value = 600 #declare dry_value as 600
while True: #create infinit loop till somthing breaks the true statement
moisture_value = moisture.read() #when there are readings from the soil sensor put them in 'moisture_value'
print(moisture_value) #show readings from soil sensor
sleep(3) #wait for ... seconds
if (moisture_value < dry_value): #when readings from soil sensor is smaller than 600 then ...
pump.on() #pin 4 high
print(pump.value()) #shows pin state 1 (high)
sleep(10) #wait ... seconds
pump.off() #pin 4 (0)low
sleep(10) #wait 10 seconds
Code: Select all
c.publish(b'moisture', str(moisture).encode())
I am using a small pump, to carry water from a reservoir.torwag wrote: ↑Tue Nov 12, 2019 6:53 am
Also consider the hardware incidents. If you run out of water, the pump will be turned on after each reading. Thus, your sleep command at the end of the pump could be critical to make sure, the pump has time to cool down and does not overheat.
Same is true if the water never reaches the sensor, etc. You could add some emergency protocol, like checking if the pump was turned on 5 times in a row and if this is the case, stop the entire system as you might expect something went wrong.
Code: Select all
>>> import pyb
>>> from pyb import Pin
>>> import time
>>>
>>> relay_pump=Pin('B12',Pin.OUT)
>>> res_out=Pin('C6',Pin.OUT) # reservoir sensor OUTPUT
>>> res_in=Pin('C7',Pin.IN,Pin.PULL_DOWN) # reservoir sensor INPUT
>>>
>>> def res_empty():
... res_out.on()
... b= not res_in.value()
... res_out.off()
... return b
>>>
>>> def pump(t_in_sec):
... if res_empty():
... return False
... else:
... relay_pump.on()
... time.sleep(t_in_sec)
... relay_pump.off()
... return True
>>>