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
Raspberry Pi LED Blueprints

Raspberry Pi LED Blueprints: Design, build, and test LED-based projects using the Raspberry Pi

By Agus Kurniawan
zł106.99 zł59.99
Book Sep 2015 180 pages 1st Edition
eBook
zł106.99 zł59.99
Print
zł133.99
Subscription
Free Trial
eBook
zł106.99 zł59.99
Print
zł133.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Sep 24, 2015
Length 180 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781782175759
Category :
Table of content icon View table of contents Preview book icon Preview Book

Raspberry Pi LED Blueprints

Chapter 1. Getting Started with LED Programming through Raspberry Pi GPIO

In this chapter, you will learn the basics of Raspberry Pi GPIO and LED development so that you can be sure that you have the basic required knowledge to develop LED programming through Raspberry Pi GPIO.

The following topics will be the major takeaways from this chapter:

  • Setting up Raspberry Pi

  • Introducing Raspberry Pi GPIO

  • Blinking LEDs

  • Turning an LED on/off using a push button

  • Changing color through an RGB LED

Setting up Raspberry Pi


Raspberry Pi is a low-cost, credit card-sized computer that you can use to develop a general-purpose computer. There are several Raspberry Pi models that you can use to develop what you want. For illustration, this book will use a Raspberry Pi 2 board. Check https://www.raspberrypi.org/products/, which offers the Raspberry Pi 2 Model B board.

You can also see a video of the unboxing of Raspberry Pi 2 Model B from element14 on YouTube at https://www.youtube.com/watch?v=1iavT62K5q8.

To make Raspberry Pi work, we need an OS that acts as a bridge between the hardware and the user. There are many OS options that you can use for Raspberry Pi. This book uses Raspbian as an OS platform for Raspberry Pi. Raspbian OS is an operating system based on Debian with a targeting ARM processor. You can use another OS platform for Raspberry Pi from https://www.raspberrypi.org/downloads/. To deploy Raspbian with Raspberry Pi 2 Model B, we need a microSD card of at least 4 GB in size, but the recommended size is 8 GB. For testing purposes, we will use Raspbian as an operating system platform for Raspberry Pi.

You can set up your Raspberry Pi with the Raspbian image by following the instructions on this website, QUICK START GUIDE, https://www.raspberrypi.org/help/quick-start-guide/.

After having installed and deployed Raspbian, you can run the Raspbian desktop GUI by typing the following command on the terminal:

startx

This command makes Raspbian load the GUI module from the OS libraries. You can then see the Raspbian desktop GUI as follows:

Introducing Raspberry Pi GPIO


General-purpose input/output (GPIO) is a generic pin on Raspberry Pi, which can be used to interact with external devices, such as sensor and actuator devices. You can see the Raspberry Pi GPIO pinouts in the following figure (source: http://www.element14.com/community/docs/DOC-73950/l/raspberry-pi-2-model-b-gpio-40-pin-block-pinout):

To access Raspberry Pi GPIO, we can use several GPIO libraries. If you are working with Python, Raspbian will have already installed the RPi.GPIO library to access Raspberry Pi GPIO. You can read more about RPi.GPIO at https://pypi.python.org/pypi/RPi.GPIO. You can verify the RPi.GPIO library from a Python terminal by importing the RPi.GPIO module, as shown in the following screenshot:

If you don't find this library on Python runtime or get the error message ImportError: No module named RPi.GPIO, you can install it by compiling from the source code. For instance, we want to install RPi.GPIO 0.5.11, so type the following commands:

wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.11.tar.gz
tar -xvzf RPi.GPIO-0.5.11.tar.gz
cd RPi.GPIO-0.5.11/
sudo python setup.py install

Tip

To install and update through the apt command, your Raspberry Pi must be connected to the Internet.

Another way to access Raspberry Pi GPIO is to use WiringPi. It is a library written in C for Raspberry Pi to access GPIO pins. You can read information about WiringPi from the official website http://wiringpi.com/.

To install WiringPi, you can type the following commands:

sudo apt-get update
sudo apt-get install git-core
git clone git://git.drogon.net/wiringPi
cd wiringPi
sudo ./build

Please make sure that your Pi network does not block the git protocol for git://git.dragon.net/wiringPi. This code can be browsed on https://git.drogon.net/?p=wiringPi;a=summary.

The next step is to install the WiringPi interface for Python, so you can access Raspberry Pi GPIO from a Python program. Type the following commands:

sudo apt-get install python-dev python-setuptools
git clone https://github.com/Gadgetoid/WiringPi2-Python.git
cd WiringPi2-Python
sudo python setup.py install

When finished, you can verify it by showing a GPIO map from the Raspberry Pi board using the GPIO tool:

gpio readall

If this is successful, you should see the GPIO map from the Raspberry Pi board on the terminal:

You can also see values in the wPi column, which will be used in the WiringPi program as GPIO value parameters. I will show you how to use it in the WiringPi library in the next section.

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)

Turning an LED on/off using a push button


In the previous section, we accessed Raspberry Pi GPIO to turn LEDs on/off by program. Now we will learn how to turn an LED on/off using a push button, which is used as a GPIO input from Raspberry Pi GPIO.

The following hardware components are needed:

You can see the push button connection in the following figure:

Our hardware wiring is simple. You simply connect the LED to GPIO23 from Raspberry Pi. The push button is connected to Raspberry Pi GPIO on GPIO24. The complete hardware wiring can be seen in the following figure:

Furthermore, you can write a Python program to read the push button's state. If you press the push button, the program will turn on the LED. Otherwise, it will turn off the LED. This is our program scenario.

The following is the complete code for the Python program:

# ch01_02.py file

import wiringpi2 as wiringpi

# initialize
wiringpi.wiringPiSetup()

# define GPIO mode
GPIO23 = 4
GPIO24 = 5
LOW = 0
HIGH = 1
OUTPUT = 1
INPUT = 0
PULL_DOWN = 1
wiringpi.pinMode(GPIO23, OUTPUT)  # LED
wiringpi.pinMode(GPIO24, INPUT)  # push button
wiringpi.pullUpDnControl(GPIO24, PULL_DOWN)  # pull down


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

try:
    clear_all()
    while 1:
        button_state = wiringpi.digitalRead(GPIO24)
        print button_state
        if button_state == 1:
            wiringpi.digitalWrite(GPIO23, HIGH)
        else:
            wiringpi.digitalWrite(GPIO23, LOW)

        wiringpi.delay(20)

except KeyboardInterrupt:
    clear_all()

print("done")

Save this code in a file named ch01_02.py.

Now you can run this program via the terminal:

$ sudo python ch01_02.py

After this, you can check by pressing the push button; you should see the LED lighting up.

First, we define our Raspberry Pi GPIO's usage. We also declare our GPIO input to be set as pull down. This means that if the push button is pressed, it will return value 1.

GPIO23 = 4
GPIO24 = 5
LOW = 0
HIGH = 1
OUTPUT = 1
INPUT = 0
PULL_DOWN = 1
wiringpi.pinMode(GPIO23, OUTPUT)  # LED
wiringpi.pinMode(GPIO24, INPUT)  # push button
wiringpi.pullUpDnControl(GPIO24, PULL_DOWN)  # pull down

We can read the push button's state using the digitalRead() function from WiringPi as follows:

button_state = wiringpi.digitalRead(GPIO24)

If the push button is pressed, we turn on the LED; otherwise, we turn it off:

print button_state
if button_state == 1:
    wiringpi.digitalWrite(GPIO23, HIGH)
else:
    wiringpi.digitalWrite(GPIO23, LOW)

Changing color through an RGB LED


The last demo of basic LED programming is to work with an RGB LED. This LED can emit monochromatic light, which could be one of the three primary colors—red, green, and blue, known as RGB.

The RGB LED connection is shown in the following figure:

In this section, we will build a simple program to display red, green, and blue colors through the RGB LED.

The following hardware components are needed:

Our hardware wiring can be implemented as follows:

  • RGB LED pin 1 is connected to Raspberry Pi GPIO18

  • RGB LED pin 2 is connected to Raspberry Pi VCC +3 V

  • RGB LED pin 3 is connected to Raspberry Pi GPIO23

  • RGB LED pin 4 is connected to Raspberry Pi GPIO24

The complete hardware wiring can be seen in the following figure:

Returning to the Raspberry Pi terminal, you could write a Python program to display color through RGB LED. Let's create a file named ch01_03.py and write this script as follows:

# ch01_03.py file

import wiringpi2 as wiringpi
import time

# initialize
wiringpi.wiringPiSetup()

# define GPIO mode
GPIO18 = 1  # red
GPIO23 = 4  # green
GPIO24 = 5  # blue
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, HIGH)
    wiringpi.digitalWrite(GPIO23, HIGH)
    wiringpi.digitalWrite(GPIO24, HIGH)


def display(red, green, blue):
    wiringpi.digitalWrite(GPIO18, red)
    wiringpi.digitalWrite(GPIO23, green)
    wiringpi.digitalWrite(GPIO24, blue)


try:
    while 1:
        clear_all()
        print("red")
        display(0, 1, 1)
        time.sleep(2)
        clear_all()
        print("green")
        display(1, 0, 1)
        time.sleep(2)
        clear_all()
        print("blue")
        display(1, 1, 0)
        time.sleep(2)
        clear_all()
        print("white")
        display(0, 0, 0)
        time.sleep(2)
        clear_all()
        print("110")
        display(1, 1, 0)
        time.sleep(2)
        clear_all()
        print("101")
        display(1, 0, 1)
        time.sleep(2)
        clear_all()
        print("011")
        display(0, 1, 1)
        time.sleep(2)

except KeyboardInterrupt:
    clear_all()

print("done")

Save this script. You can run this file by typing the following command:

$ sudo python ch01_03.py

Then, you should see that the RGB LED displays a certain color every second. The program output can also write a message indicating which color is currently on the RGB LED:

The RGB LED can display a color by combining three basic colors: red, green, and blue. First, we initialize Raspberry Pi GPIO and define our GPIO usage:

# initialize
wiringpi.wiringPiSetup()

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

For instance, to set a red color, we should set LOW on the red pin and HIGH on both green and blue pins. We define the display() function to display a certain color on the RGB LED with the red, green, and blue values as parameters as follows:

def display(red, green, blue):
    wiringpi.digitalWrite(GPIO18, red)
    wiringpi.digitalWrite(GPIO23, green)
    wiringpi.digitalWrite(GPIO24, blue)

In the main program, we display a color via the display() function by passing red, green, and blue values, as shown in the following code:

clear_all()
print("red")
display(0, 1, 1)
time.sleep(2)
clear_all()
print("green")
display(1, 0, 1)
time.sleep(2)
clear_all()
print("blue")
display(1, 1, 0)
time.sleep(2)
clear_all()
print("white")
display(0, 0, 0)
time.sleep(2)
clear_all()
print("110")
display(1, 1, 0)
time.sleep(2)
clear_all()
print("101")
display(1, 0, 1)
time.sleep(2)
clear_all()
print("011")
display(0, 1, 1)
time.sleep(2)

Summary


Let's summarize what we have learned in this chapter. We connected three LEDs to a Raspberry Pi board. After that, we made these LEDs blink. Then, we read the Raspberry Pi GPIO input. Finally, we learned to display several colors through an RGB LED.

In the next chapter, we will work with 7-segment display and a shift register to manipulate several 7-segment display modules. We will also build a countdown timer app by utilizing a 7-segment module.

Left arrow icon Right arrow icon

Key benefits

What you will learn

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Sep 24, 2015
Length 180 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781782175759
Category :

Table of Contents

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

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.