Blinking LED

The Raspberry Pi comes with a set of GPIO pins which allow a programmer to control electronics directly. This first tutorial talks through arguably the simplest electronics project you can do on the Raspberry Pi but first we'll need to go through some of the things for the setup.

Python

First we need to make sure that we understand how to use Python on the command line. If you aren't sure you can run through the tutorial which can be found here.

Connecting up the LED

First we need to connect up the LED to the pins but to do this we need to know which pins are which on the Raspberry Pi. Here is a diagram of the Pins on a Raspberry Pi B+.

The pin out diagram for the Raspberry Pi B+ model

Connecting up the LED

First we need to connect the LED. We need an LED set of tails because LEDs can't produce light on their own, they need a resistor in series in order to operate. Connect the red wire to the pin labelled GPIO 02 and the black wire (which is ground) to any of the pins labelled Ground(GND).

Writing the code

The following code will turn the LED on and off several times. The code itself will be described in a different post.
from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
pin = 2
GPIO.setup(pin, GPIO.OUT)

GPIO.output(pin,GPIO.HIGH)
time.sleep(0.5)
GPIO.output(pin,GPIO.LOW)
time.sleep(0.5)
GPIO.output(pin,GPIO.HIGH)
time.sleep(0.5)
GPIO.output(pin,GPIO.LOW)
Once you have saved the code into a file then navigate to the foler containing the file (using a command prompt) then use the code
sudo python blink.py
where blink.py is the filename the code is in. Note we have to use 'sudo' because using the pins in this way requires super user privileges (sudo stands for Super User DO, which gives the command run special privileges when executing). You should then observe the LED blinking. If it doesn't work then check the code executes without an error and that the LED is wired up correctly.