1
import RPi.GPIO as GPIO
import time
def mist():
        GPIO.setmode(GPIO.BCM)

        # setup and switing on relay
        GPIO.setup(4, GPIO.OUT, initial=GPIO.LOW)
        # sleep for 30 second
        time.sleep(30)
        # off relay
        GPIO.output(4,  GPIO.HIGH)

        # clean up all
        GPIO.cleanup()

if __name__ == "__main__":
        mist()

what I am trying to do that to on 4 channel relay module for 30 seconds.

Problems that I am facing

  1. Even if I set out put pin to GPIO.HIGHit won't switch on. (I think my relay is ACTIVE LOW, so I set initial=GPIO.LOW for switching on)
  2. I set time.sleep for 30 seconds but relay switch off after 2 or 10 seconds

Why this happening, I am new to python and Rasberry pi

EDIT

The problem was 1-Wire Interface option was switched on. GPIO4 is the default for 1-wire. If that is activated in /boot/config.txt it may interfere with other hardware connected to that pin

dirk pointed out the issue.

11
  • @Hi Ebin Manuval, have you tried testing your code with an LED instead of a relay?
    – David
    Mar 22, 2019 at 15:26
  • @David No i hav't tried it Mar 22, 2019 at 15:54
  • I suggest you try that first, and see if you observe the same odd timing behavior. If you don't, and your LED switches state after 30 seconds, then I'd suspect it's a matter of how your relay is connected to the RPi.
    – David
    Mar 22, 2019 at 15:56
  • I tried it with the LED but same result Mar 22, 2019 at 16:07
  • 3
    GPIO4 is the default for 1-wire. If that is activated in /boot/config.txt it may interfere with other hardware connected to that pin
    – Dirk
    Mar 22, 2019 at 17:08

1 Answer 1

4

GPIO4 is the default for the 1-wire interface. If that is activated in /boot/config.txt it may interfere with other hardware connected to that pin.

Check config.txt for a line like this:

dtoverlay=w1-gpio

To deactivate it you can remove this line manually or use one of the Pi configuration utilities (raspi-config or the one in the GUI under 'Preferences'). You will need to reboot after changing the configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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