2

I'm building a little automated hydroponics system using a raspberry pi zero, a 5 volt relay, a pump and a soil moisture sensor from adafruit. So far everything is working, I can pull the pin to high and engaged the pump via the relay, I can get data from the soil-sensor.

I wrote a test to see if I could make it only print the readout from the sensor if the value of the read was over a certain amount and then activate it with my finger. This works great, it starts and stops if the value is greater than 600 or less than 600... However if I add the line to pull the pin to high in place of the Print function, it simply pulls the pin to high until I kill the program. What am I missing here?

My Test program:

import time

import board

from board import SCL, SDA

import busio

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

RELAIS_1_GPIO = 18

GPIO.setup(RELAIS_1_GPIO, GPIO.OUT) # GPIO Assign mode


from adafruit_seesaw.seesaw import Seesaw

i2c_bus = busio.I2C(SCL, SDA)

ss = Seesaw(i2c_bus, addr=0x36)

while True:
    # read moisture level through capacitive touch pad
    touch = ss.moisture_read()

    if touch > 600:
        GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
        print("moisture: " + str(touch))

4 Answers4

2

Thank you all for your help in diagnosing this. Relay was faulty, swapped for one of the same make and model and now its working with the test provided by Coder Mike, as well as my original script.

1

OBSERVATION: I do not see a line of code / instruction to pull the GPIO back to low. If the observation is correct and this instruction is executed:

GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
  • How would the line GPIO return to a low state?
  • Under what condition do you expect the GPIO to return to the low state?

What happens if the instructions is changed to:

GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
gatorback
  • 637
  • 1
  • 6
  • 21
1

Setting a GPIO pin has a latching behavior. That is when you set it, it stays that same value until you tell it otherwise. So in your if statement, just add an else clause to reset it low if the value read is <=600:

if touch > 600:
    GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
    print("moisture: " + str(touch))
else:
    GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
John S
  • 359
  • 1
  • 7
1

Does the following turn on and off your relay accordingly?

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
RELAIS_1_GPIO = 18
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT)

while True:
    print('on')
    GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
    time.sleep(2)

    print('off')
    GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
    time.sleep(2)
CoderMike
  • 7,102
  • 1
  • 11
  • 16