raspberry gpio in python

To control the Raspberry Pi's GPIO pins from Python, you will first need to install the RPi.GPIO library. You can do this by opening a terminal and running the following command:

main.py
sudo pip install RPi.GPIO
26 chars
2 lines

Once you have the library installed, you can start using it in your Python programs. Here's an example program that blinks an LED connected to GPIO pin 17:

main.py
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

while True:
    GPIO.output(17, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(17, GPIO.LOW)
    time.sleep(1)
195 chars
12 lines

This code sets up pin 17 as an output pin, then enters an infinite loop that turns the pin on for one second and off for one second, creating a blinking effect.

Note that you will need to run this program with superuser privileges (i.e. using sudo python yourprogram.py). Also, make sure to properly connect any external components to the GPIO pins to avoid damaging your Raspberry Pi.

gistlibby LogSnag