Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi LED Blueprints

You're reading from  Raspberry Pi LED Blueprints

Product type Book
Published in Sep 2015
Publisher
ISBN-13 9781782175759
Pages 180 pages
Edition 1st Edition
Languages
Author (1):
Agus Kurniawan Agus Kurniawan
Profile icon Agus Kurniawan

Table of Contents (14) Chapters

Raspberry Pi LED Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Getting Started with LED Programming through Raspberry Pi GPIO Make Your Own Countdown Timer Make Your Own Digital Clock Display LED Dot Matrix Building Your Own Traffic Light Controller Building Your Own Light Controller-based Bluetooth Making Your Own Controlled Lamps Through Internet Network Index

Blinking LEDs


In this section, we will build a simple app that interacts with Raspberry Pi GPIO. We will use three LEDs, which are attached to the Raspberry Pi 2 board. Furthermore, we will turn the LEDs on/off sequentially.

The following hardware components are needed:

  • Raspberry Pi 2.(you can change this model)

  • Three LEDs of any color

  • Three resistors (330 Ω or 220 Ω)

The hardware wiring can be implemented as follows:

  • LED 1 is connected to Pi GPIO18

  • LED 2 is connected to Pi GPIO23

  • LED 3 is connected to Pi GPIO24

The following image shows the hardware connection for LED blinking:

Now you can write a program using WiringPi with Python. The following is the complete Python code for blinking LEDs:

# ch01_01.py file

import wiringpi2 as wiringpi
import time

# initialize
wiringpi.wiringPiSetup()

# define GPIO mode
GPIO18 = 1
GPIO23 = 4
GPIO24 = 5
LOW = 0
HIGH = 1
OUTPUT = 1
wiringpi.pinMode(GPIO18, OUTPUT)
wiringpi.pinMode(GPIO23, OUTPUT)
wiringpi.pinMode(GPIO24, OUTPUT)


# make all LEDs off
def clear_all():
    wiringpi.digitalWrite(GPIO18, LOW)
    wiringpi.digitalWrite(GPIO23, LOW)
    wiringpi.digitalWrite(GPIO24, LOW)

# turn on LED sequentially
try:
    while 1:
        clear_all()
        print("turn on LED 1")
        wiringpi.digitalWrite(GPIO18, HIGH)
        time.sleep(2)
        clear_all()
        print("turn on LED 2")
        wiringpi.digitalWrite(GPIO23, HIGH)
        time.sleep(2)
        clear_all()
        print("turn on LED 3")
        wiringpi.digitalWrite(GPIO24, HIGH)
        time.sleep(2)

except KeyboardInterrupt:
    clear_all()

print("done")

Save this script in a file named Python ch01_01.py.

Moreover, you can run this file on the terminal. Type the following command:

sudo python ch01_01.py

You should see three LEDs blinking sequentially. To stop the program, you can press CTRL+C on the Pi terminal. The following is a sample of the program output:

Based on our wiring, we connect three LEDs to GPIO18, GPIO23, and GPIO24 from the Raspberry Pi board. You can see these WiringPi GPIO values from the gpio readall command and find GPIO18, GPIO23, and GPIO24 recognized as (the wPi column) 1, 4, and 5, respectively.

First, we initialize WiringPi using wiringpi.wiringPiSetup(). Then, we define our GPIO values and set their modes on Raspberry Pi as follows:

GPIO18 = 1
GPIO23 = 4
GPIO24 = 5
LOW = 0
HIGH = 1
OUTPUT = 1
wiringpi.pinMode(GPIO18, OUTPUT)
wiringpi.pinMode(GPIO23, OUTPUT)
wiringpi.pinMode(GPIO24, OUTPUT)

Each LED will be turned on using wiringpi.digitalWrite(). time.sleep(n) is used to hold the program for n seconds. Let's set a delay time of two seconds as follows:

clear_all()
print("turn on LED 1")
wiringpi.digitalWrite(GPIO18, HIGH)
time.sleep(2)

The clear_all() function is designed to turn off all LEDs:

def clear_all():
    wiringpi.digitalWrite(GPIO18, LOW)
    wiringpi.digitalWrite(GPIO23, LOW)
    wiringpi.digitalWrite(GPIO24, LOW)
You have been reading a chapter from
Raspberry Pi LED Blueprints
Published in: Sep 2015 Publisher: ISBN-13: 9781782175759
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}