blink_led.py

Download
python 60 lines 1.1 KB
 1#!/usr/bin/env python3
 2"""
 3LED Blink Example for Raspberry Pi
 4LED ๊นœ๋นก์ด๊ธฐ ์˜ˆ์ œ
 5
 6Hardware:
 7- LED connected to GPIO17 (pin 11)
 8- 330 ohm resistor in series
 9
10Usage:
11    python3 blink_led.py
12"""
13
14from gpiozero import LED
15from time import sleep
16import signal
17import sys
18
19# GPIO pin configuration
20LED_PIN = 17
21
22def signal_handler(sig, frame):
23    """Handle Ctrl+C gracefully"""
24    print("\nExiting...")
25    sys.exit(0)
26
27def main():
28    """Main function to blink LED"""
29    # Register signal handler
30    signal.signal(signal.SIGINT, signal_handler)
31
32    # Initialize LED
33    led = LED(LED_PIN)
34
35    print(f"LED Blink started on GPIO{LED_PIN}")
36    print("Press Ctrl+C to exit")
37
38    # Blink loop
39    blink_count = 0
40    try:
41        while True:
42            led.on()
43            print(f"LED ON  (count: {blink_count})")
44            sleep(0.5)
45
46            led.off()
47            print(f"LED OFF (count: {blink_count})")
48            sleep(0.5)
49
50            blink_count += 1
51
52    except KeyboardInterrupt:
53        print("\nStopped by user")
54    finally:
55        led.off()
56        print("LED turned off, cleanup complete")
57
58if __name__ == "__main__":
59    main()