Labels

Arduino (1) Brainf**k (1) Bugs (3) C (1) HTML (1) Install (3) Linux (27) Maths (1) Python (12) Raspberry Pi (4)

Sunday, 19 July 2015

How to make a LED flash with the Raspberry Pi.

To flash a LED we will use the GPIO pins(general purpose input/output) to control the LED. We will control these GPIO pins using python.
The GPIO layout on the Raspberry Pi model B.












We will be using GPIO 17 which is Pin 11.

Installing the library for python.
sudo apt-get install python-dev python-rpi.gpio

Now create a file called blink.py
vi blink.py
Add the following lines

import time
import RPi.GPIO as GPIO

This imports the two libraries, the time library provides the delay for the LED to flash and the RPi.GPIO allows us to control the GPIO pins.

pin = 17

This sets pin to 17


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



This sets the pin mode to BCM so we can reference the pins and sets up pin 17 for output

while True:
    GPIO.output(pin, True)
    time.sleep(0.5)
    GPIO.output(pin, False)
    time.sleep(0.5)
This is the Loop where the LED is turned on then turned off.

Now connect the positive leg of your LED to pin 17 and the negative to ground.
When you run the code you should see the LED flash.