RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login
    Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

    Help with relay

    Scheduled Pinned Locked Moved Help and Support
    relaygpiopython
    3 Posts 1 Posters 362 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      corminos
      last edited by

      I have a 12v relay hooked up to my Raspberry Pi 4. I am able to turn an LED light off and on by running the command "python3" then setting a GPIO pin to LOW and HIGH. This all works great.

      Here is what I run from the command line:

      pi@retropie:~ $ python3
      Python 3.7.3 (default, Dec 20 2019, 18:57:59)
      [GCC 8.3.0] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import RPi.GPIO as GPIO
      >>> GPIO.setmode(GPIO.BCM)
      >>> PIN_1 = 23
      >>> GPIO.setup(PIN_1, GPIO.OUT)
      >>> GPIO.output(PIN_1, GPIO.LOW)
      >>> GPIO.output(PIN_1, GPIO.HIGH)
      >>> GPIO.cleanup()
      >>> exit()
      

      But, when I put the code into a script and run the script with a sudo command, nothing happens.

      Below is the script. I run it using "sudo ./light.py on"

      My question is why isn't the script running? What is the difference between running a script and entering the command by just running "python3"?

      #!/usr/bin/env python3
      
      """
      A script to turn the relay off and on
      """
      
      usage = """
      Usage: light.py [on|off]
      
      Takes a single argument which should be either "on" (turn lights on) or "off" (turn them off)
      """
      
      import sys
      
      import RPi.GPIO as GPIO
      
      GPIO.setmode(GPIO.BCM)
      
      PIN_1 = 22
      PIN_2 = 23
      
      GPIO.setup(PIN_1, GPIO.OUT)
      GPIO.setup(PIN_2, GPIO.OUT)
      
      def turn_off():
        print("turning off")
        GPIO.output(PIN_1, GPIO.HIGH)
        GPIO.output(PIN_2, GPIO.HIGH)
      
      def turn_on():
        print("turning on")
        GPIO.output(PIN_1, GPIO.LOW)
        GPIO.output(PIN_2, GPIO.LOW)
      
      mode = sys.argv[1]
      
      print(mode)
      
      if mode not in ("on", "off"):
        print("Invalid argument!", file=sys.stderr)
        print(usage)
        GPIO.cleanup()
        sys.exit(1)
      
      if mode == "on":
        turn_on()
      elif mode == "off":
        turn_off()
      
      GPIO.cleanup()
      sys.exit()
      

      I do get the output from the "print" statements, so I know the code is getting into the defined methods.

      Thanks in advance.

      1 Reply Last reply Reply Quote 0
      • C
        corminos
        last edited by

        Sorry, I shouldn't have said "why isn't the script running?" What I should have said was "Why isn't the script turning on and off the relay?"

        Also, I had another script that I would test the state of the pin and this would toggle the output from reading the GPIO pin as I ran the light.py script.

        #!/usr/bin/env python3
        
        import RPi.GPIO as GPIO
        import sys
        
        channel = int(sys.argv[1])
        
        GPIO.setmode(GPIO.BCM)
        # Setup your channel
        GPIO.setup(channel, GPIO.OUT)
        
        # To test the value of a pin use the .input method
        channel_is_on = GPIO.input(channel)  # Returns 0 if OFF or 1 if ON
        
        if channel_is_on:
         print('it is on')
        else:
         print('it is off')
        
        GPIO.cleanup()
        sys.exit()
        
        1 Reply Last reply Reply Quote 0
        • C
          corminos
          last edited by

          One more piece of the puzzle...at the end of my script, if I remove the GPIO.cleanup(), then the light stays on.

          But, in subsequent calls to my script, I receive a warning that the channel is already in use. It doesn't stop the code, but the programmer in me doesn't like warnings.

          My question now is why does the cleanup function change the output of the GPIO pins?

          1 Reply Last reply Reply Quote 0
          • First post
            Last post

          Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

          Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.