I'm configuring some leds and buttons on my raspberry pi, and when running the script the system warns me a channel is already in use. I tried looking for a solution, and a tip was to do a
GPIO.Cleanup() in a finally clause. I tried, but it keeps failing. Any idea why? The error points to
GPIO.setup(button_pin, GPIO.IN) but not sure to add these lines of code in my try clause?
#!/usr/bin/env python import RPi.GPIO as GPIO import os import time # Hide Warnings # GPIO.setwarnings(False) # Assign GPIO pin numbers button_pin = 3 # Input Pin button_led = 14 # Output Pin green_led = 22 # Output Pin red_led = 27 # Output Pin # ir_receiver_pin = 17 # Use BCM pin numbering GPIO.setmode(GPIO.BCM) # 1. GPIO Setup GPIO.setup(button_pin, GPIO.IN) GPIO.setup(button_led, GPIO.OUT) GPIO.setup(green_led, GPIO.OUT) GPIO.setup(red_led, GPIO.OUT) # 2. Button Led Behaviour on Startup def button_led_on(): GPIO.output(button_led, GPIO.HIGH) def button_led_off(): GPIO.output(button_led, GPIO.LOW) def button_flicker_startup(): a = 1 while a < 4: button_led_on() time.sleep(0.3) button_led_off() time.sleep(0.3) a = a + 1 button_led_on() # LED is high when Pi is on # 3. Define front led behaviour on startup def green_led_on(): GPIO.output(green_led, GPIO.HIGH) def green_led_off(): GPIO.output(green_led, GPIO.LOW) def red_led_on(): GPIO.output(red_led, GPIO.HIGH) def red_led_off(): GPIO.output(red_led, GPIO.LOW) def boot_flicker(): time.sleep(1.0) green_led_on() time.sleep(0.5) green_led_off() time.sleep(0.2) green_led_on() time.sleep(0.3) green_led_off() red_led_on() time.sleep(0.3) red_led_off() time.sleep(0.2) green_led_on() # LED is high when Pi is on # 4. Main program try: button_flicker_startup() boot_flicker() GPIO.wait_for_edge(button_pin, GPIO.FALLING) os.system("sudo shutdown -h now") except: pass finally: GPIO.cleanup()
I could use
GPIO.setwarnings(False) but this is just hiding the error and would like to avoid that.
EDIT: My Raspberry Pi is using a Hifiberry dac who is using GPIO 2 and 3 for configuration. Could it be related to that?