Introduction: Raspberry Pi GPIO Programming

Hey guys, we're going to learn how to program the Raspberry Pi's GPIO ( General Purpose Input Output ) Header pins using Python.

The pins can be programmed using Scratch, but, we won't get into that today.

To actually have access to the Raspberry Pi's pins, you need to open Python as a root user, or an admin.

The way that we do this in Linux (which is what the Raspbian is based off of), is by clicking Menu > Accessories > Terminal then, typing sudo idle3 &

That will open the Python IDE as a root user, allowing you control over the Raspberry Pi's GPIO pins.

Step 1: Python Code

Here is the Python code, in case you need it:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False)

led = 4

button = 14

GPIO.setup(led, GPIO.OUT) //Setting pin 4 as output

GPIO.setup(button, GPIO.IN, GPIO.PUD_UP) //Setting pin 14 as input

while True:

if GPIO.input(button) == True:

GPIO.output(led, 1) //Turns led on (1 = on, 0 = off)

time.sleep(1) //Delay

GPIO.output(led, 0)

time.sleep(1)

GPIO.output(led, 1)

time.sleep(1)

GPIO.output(led, 0)

time.sleep(1)

GPIO.cleanup() //Cleans up used ports and makes them ready to be used again.

Step 2: GPIO Reference

Picture of GPIO Reference

You can use that picture, to reference your GPIO pins.

Comments

About This Instructable

2,872views

46favorites

License:

Add instructable to: