#! /usr/bin/python3 # Importa les llibreries requerides import RPi.GPIO as GPIO import time # Inicialitza una variable global amb el pin on connectarem el LED, segons els noms de la Raspberry Pi GPIO Extension Board. En este cas, GPIO17 ledPin = 17 # Configura els pins def setup(): # Utilitza els noms de la extension board GPIO.setmode(GPIO.BCM) # Posa el ledPin en mode OUTPUT GPIO.setup(ledPin, GPIO.OUT) # Escriu un nivell LOW per el ledPin per apagar el LED GPIO.output(ledPin, GPIO.LOW) print('Utilitzant el pin %d'%ledPin) # Funció amb el codi principal def run(): while True: # Escriu un nivell HIGH per el ledPin per engegar el LED GPIO.output(ledPin, GPIO.HIGH) print('LED engegat') # Espera 1 segon time.sleep(1) # Escriu un nivell LOW per el ledPin per apagar el LED GPIO.output(ledPin, GPIO.LOW) print('LED apagat') # Espera 1 segon time.sleep(1) # Allibera els recursos utilitzats def destroy(): # Allibera els pins GPIO GPIO.cleanup() # Si s'executa este script... if __name__ == '__main__': print('Executant el script... \n') # Ho prepara tot per funcionar setup() try: # Executa la funció principal run() # Captura el CTRL+C del teclat per a no abortar directament except KeyboardInterrupt: # Acaba ordenadament destroy()