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))
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.

|improve this answer|||||
  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year. – Ingo Feb 22 at 19:17
  • Oh :) thanks. Is done. – Nareik Seivad Feb 23 at 20:31
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)
|improve this answer|||||
  • Both GPIO.output(RELAIS_1_GPIO, GPIO.LOW) and GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) trigger the relay. If I comment out that line it does not. The rest of my test runs perfectly with or without it. Am I importing the wrong library for the gpio? – Nareik Seivad Feb 15 at 6:55
  • What do you mean by LOW and HIGH trigger the relay? Normally with a relay LOW is relay ON, HIGH is relay OFF. Is your relay compatible with the 3.3V GPIO pins on the Pi - post a link to the relay your using. – CoderMike Feb 15 at 10:08
  • @NareikSelvad Your question can not be answered with out knowing which relay board you are using. I suggest that you follow the instructions provided by the relay board and test GPIO.output() commands as prescribed and post results to determine if they meet expectations. – gatorback Feb 15 at 14:36
  • An answer should not contain questions to be answered to get an answer. – Ingo Feb 16 at 12:31
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)
|improve this answer|||||
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)
|improve this answer|||||
  • No :/ the relay is still active even when printing off – Nareik Seivad Feb 16 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.