20

gpio.py:5: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(8, GPIO.OUT) That is what I get after I run:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(8, GPIO.OUT)
GPIO.output(7, 0)
GPIO.output(8, 0)

Is there anyway I can fix the warning?

1
  • Also, what does GPIO.cleanup() do? Should I use it?
    – LilVinny
    Commented Sep 18, 2016 at 16:40

3 Answers 3

25

This code helped me get rid of the warning (the "finally" part at the end):

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme

GPIO.setup(6, GPIO.OUT) # output rf

# Initial state for LEDs:
print("Testing RF out, Press CTRL+C to exit")

try:
     print("set GIOP high")
     GPIO.output(6, GPIO.HIGH)
     time.sleep(5)               
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
   print("Keyboard interrupt")

except:
   print("some error") 

finally:
   print("clean up") 
   GPIO.cleanup() # cleanup all GPIO 
2
6

You can safely ignore the warning.

As the message says add GPIO.setwarnings(False) to the start of your script to disable warnings.

GPIO.cleanup() sets any GPIO you have used within RPi.GPIO to INPUT mode. If that behaviour is desirable call the method before exiting your script.

1
  • Is there a way that I can repeat the script after every 5 minutes and after every loop turn the lights off.
    – LilVinny
    Commented Sep 18, 2016 at 17:01
0

List item

import RPi.GPIO as GPIO 
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(7, GPIO.OUT) 
GPIO.setup(8, GPIO.IN)     

state = "ON"

if GPIO.input(8) == 0: GPIO.output(7, GPIO.HIGH)
state = “OFF” 
print(state)
else:
GPIO.output(7, GPIO.LOW)
print(state)
1
  • 2
    Would you care to edit and explain your answer? Commented Mar 9, 2021 at 5:41

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