• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to make a LED flash with the Raspberry Pi

by admin

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.

flash led with rasberry pi

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.

Filed Under: Linux

Some more articles you might also be interested in …

  1. foreman: command not found
  2. colrm Command Examples in Linux
  3. lz4 Command Examples
  4. git cherry-pick: Apply the changes introduced by existing commits to the current branch
  5. Linux OS Service ‘smartd’
  6. faketime: command not found
  7. cryptsetup Command Examples in Linux
  8. Linux OS Service ‘lm_sensors’
  9. sort Command Examples in Linux
  10. httpd Command Examples in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright