diff --git a/tutorial/3.led_control.py b/tutorial/3.led_control.py new file mode 100644 index 0000000..75b2d65 --- /dev/null +++ b/tutorial/3.led_control.py @@ -0,0 +1,48 @@ +import RPi.GPIO as GPIO +import time + +# Set the GPIO mode (BCM for GPIO numbers, BOARD for physical pin numbers) +# BCM is generally recommended for clarity. +GPIO.setmode(GPIO.BCM) + +# Define the GPIO pin connected to the LED +# LED_PIN = 17 # You can change this to any available GPIO pin (e.g., 2, 3, 4, etc.) +LED_RED_PIN = 17 +LED_YELLOW_PIN = 27 +LED_GREEN_PIN = 22 +LED_BLUE_PIN = 5 + + +# Set up the GPIO pin as an output +# GPIO.setup(LED_PIN, GPIO.OUT) +GPIO.setup(LED_RED_PIN, GPIO.OUT) +GPIO.setup(LED_YELLOW_PIN, GPIO.OUT) +GPIO.setup(LED_GREEN_PIN, GPIO.OUT) +GPIO.setup(LED_BLUE_PIN, GPIO.OUT) + +# print(f"Controlling LED on GPIO {LED_PIN}. Press Ctrl+C to exit.") + +try: + while True: + # Turn the LED on (set output to HIGH) + # GPIO.output(LED_PIN, GPIO.HIGH) + GPIO.output(LED_RED_PIN, GPIO.HIGH) + GPIO.output(LED_YELLOW_PIN, GPIO.HIGH) + GPIO.output(LED_GREEN_PIN, GPIO.HIGH) + GPIO.output(LED_BLUE_PIN, GPIO.HIGH) + print("LEDs ON") + time.sleep(1) # Wait for 1 second + + # Turn the LED off (set output to LOW) + # GPIO.output(LED_PIN, GPIO.LOW) + GPIO.output(LED_RED_PIN, GPIO.LOW) + GPIO.output(LED_YELLOW_PIN, GPIO.LOW) + GPIO.output(LED_GREEN_PIN, GPIO.LOW) + GPIO.output(LED_BLUE_PIN, GPIO.LOW) + print("LEDs OFF") + time.sleep(1) # Wait for 1 second + +except KeyboardInterrupt: + # Clean up GPIO settings when Ctrl+C is pressed + print("\nExiting program and cleaning up GPIO...") + GPIO.cleanup()