Controlling GPIO Outputs with Python — LED, Relay, and Buzzer
Controlling GPIO Outputs with Python — LED, Relay, and Buzzer The Raspberry Pi GPIO lets you control real-world hardware from Python. Three essential output patterns. Setup bash pip install gpiozero # usually pre-installed Blink an LED Wiring: Pin 17 > 330 ohm resistor > LED > GND python from gpiozero import LED from time import sleep led = LED(17) while True: led.on() sleep(1) led.off() sleep(1) Control a Relay python from gpiozero import OutputDevice from time import sleep relay = OutputDevice(18, activehigh=False) # most relay modules are active-low relay.on() # closes relay sleep(5) relay.off() # opens relay Caution: Relays switch mains voltage. Use proper housing and fusing. Piezo Buzzer Wiring: Pin 20 > Buzzer positive, GND > Buzzer negative python from gpiozero import Buzzer from time import sleep buzzer = Buzzer(20) for in range(3): buzzer.on() sleep(0.2) buzzer.off() sleep(0.1) Running at Boot Create a systemd service in /etc/systemd/system/myscript.service to auto-start GPIO scripts on boot.
Related Guides
- How to Control GPIO Pins on Raspberry Pi with Python
- Raspberry Pi GPIO: Complete Beginner Guide with Python Examples
- Automated Plant Watering System with Raspberry Pi
- Getting Started with ROS2 on Raspberry Pi for Robotics
- How to Set Up OpenCV Machine Vision on a Raspberry Pi
- How to Run a Timelapse Camera with Raspberry Pi
- Reading Sensors over I2C with Raspberry Pi
- Flipper Zero GPIO: Reading Sensors and Controlling LEDs