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.

1. Installing the library for python.

$ sudo apt-get install python-dev python-rpi.gpio

2. 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.

3. Below sets pin to 17.

pin = 17

4. Below sets the pin mode to BCM so we can reference the pins and sets up pin 17 for output:

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

5. This is the Loop where the LED is turned on then turned off.

while True:
GPIO.output(pin, True)
    time.sleep(0.5)
    GPIO.output(pin, False)
    time.sleep(0.5)

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.

Related Post