Reader small image

You're reading from  Raspberry Pi LED Blueprints

Product typeBook
Published inSep 2015
Publisher
ISBN-139781782175759
Edition1st Edition
Right arrow
Author (1)
Agus Kurniawan
Agus Kurniawan
author image
Agus Kurniawan

Agus Kurniawan is an independent technology consultant, author, and lecturer. He has over 18 years' experience working on various software development projects, including delivering training courses and workshops, and delivering technical writing. He has done a few research activities related to wireless networking, software, and security in multiple universities. Currently, he is pursuing a Ph.D. program in Computer Science in Germany. He has previously written five books for Packt.
Read more about Agus Kurniawan

Right arrow

Chapter 2. Make Your Own Countdown Timer

In this chapter, we will learn how to work with a 7-segment display. Then we will build a countdown timer. The basics of 7-segment display programming will be introduced. Furthermore, we will learn what a shift register is and how to use it to enhance the handling of several 7-segment display modules.

From this chapter, you will learn the following topics:

  • Introducing a 7-segment display

  • Introducing a shift register

  • Driving a 7-segment display using a shift register

  • Working with the 4-digit 7-segment display

  • Building a countdown timer

Introducing a 7-segment display


In general, a 7-segment display consists of seven LEDs, and an additional LED is used for a dot (DP pin). This then allows us to display each of the 10 decimal digits 0 to 9 on the same 7-segment display.

There are two types of LED 7-segment displays, named common cathode (CC) and common anode (CA). Each LED has two connecting pins: the anode and the cathode. A sample LED datasheet can be found at http://www.kitronik.co.uk/pdf/7_segment_display_datasheet.pdf.

We can show a number on the 7-segment display by combining LED lighting through its pins. For instance, if we want to display the number 7, we should turn on LEDs a, b, and c. To turn an LED on/off, we can use Raspberry Pi GPIO:

Furthermore, we are going to build a program to display the numbers 0 to 9 using Python. The following is the hardware required:

Introducing a shift register


If our project needs to control 32 LEDs, we would normally require 32 pins of a microcontroller (MCU). The problem is that every MCU has a limited number of pins for GPIO. To address this issue, we can extend our MCU GPIO pins.

One of the solutions to extend GPIO pins is to use a shift register. We can use 74HC595 to extend the GPIO output pins. If you want to extend the GPIO input pins, you can use 74HC165. The schema of 74HC595 can be seen in the following figure:

The Q0 to Q7 pins are the parallel output from the chip. The DS pin is the serial data. STCP is the latch pin, and SHCP is the clock pin.

In this section, you will see how to implement a shift register to extend Raspberry Pi GPIO output pins using IC 74HC595 (Sparkfun, https://www.sparkfun.com/products/733). We need eight LEDs for the demonstration. The program will turn on only one LED at a time. It starts from LED 1 to 8. The hardware wiring is shown in the following figure:

When the output enable...

Driving a 7-segment display using a shift register


We have already learned how to use a shift register using 74HC595. In this section, we will try to use a shift register to drive a 7-segment display.

To drive a 7-segment display using a shift register, you can connect 74HC595 to the 7-segment module. The following is the hardware wiring:

  • 74HC595 Q0 to Q6 pins to 7-segment a to b pins

  • The 74HC595 Q7 pin to the 7-segment DP pin

  • The 74HC595 VCC pin is connected to Raspberry Pi VCC +3.3 V

  • The 74HC595 GND pin is connected to Raspberry Pi GND

  • The 74HC595 DS/Data pin is connected to Raspberry Pi GPIO25 (wPi 6)

  • The 74HC595 OE pin is connected to Raspberry Pi GPIO GND

  • The 74HC595 STCP/LATCH pin is connected to Raspberry Pi GPIO24 (wPi 5)

  • The 74HC595 SHCP/Clock pin is connected to Raspberry Pi GPIO23 (wPi 4)

  • The 74HC595 MR pin is connected to Raspberry Pi GPIO VCC +3.3 V

You can see that there are resistors on the wiring. These components are used to prevent the higher current flow on you wiring so a resistor...

Working with a 4-digit 7-segment display


After learning how to use a shift register with a 7-segment display, we are going to explore how to apply a shift register on a 4-digit 7-segment display. In general, a 4-digit 7-segment display consists of four 7-segment display modules. You can see this module scheme in the following figure (source: http://www.g-nor.com/html/GNQ-5643Ax-Bx.pdf):

You can see that four 7-segment display modules have been connected and shared on a, b, c, d, f, g, and DP pins. To display the first digit on 7-segment, you can set a value high on digit-1 (DIG 1). Otherwise, you can display the second digit on 7-segment by setting a value high on digit-2 (DIG 2). It means only one 7-segment display is running. You can run all 7-segment displays by manipulating the delay shown on this module.

For a sample illustration, you can see how to display a 4-digit number on this module. To achieve this, we need two 74HC595 shift registers. These chips are formed as cascading shift...

Building a countdown timer


In the previous section, we already learned how to display four digits on a 7-segment module and wrote the program for displaying a 4-digit number (ch02_04.py). In this section, we continue to build a simple program for a countdown timer using a 4-digit 7-segment module and two 74HC595 shift registers.

Our scenario is to get a number input from the user, for instance, 30. After this, the number is displayed on the module. Then, we decrease the number down to 0.

Let's copy the ch02_04.py file and then modify it as follows:

# ch02_05.py
…
…
print("Running...")
number_s = raw_input("Enter a number (1-999): ")
number = int(number_s)
print("Countdown " + number_s)
try:
    timer = 0

    while 1:
        digit = number

        LED_display(0, digit % 10, 0)
        digit /= 10
        time.sleep(0.01)

        LED_display(1, digit % 10, 0)
        time.sleep(0.01)
        digit /= 10

        LED_display(2, digit % 10, 0)
        time.sleep(0.01)
        digit /= 10

...

Summary


Let's summarize what we have learned in this chapter. We connected a 7-segment module to a Raspberry Pi board through eight GPIO pins. Then, we show a number on the module. To minimize the GPIO pin usage, we implemented a shift register using a 74HC595 IC, so we only need three GPIO pins. A shift register is also used to drive a 4-digit 7-segment module by cascading two 74HC595 shift registers. At the end of the section, we tried to build a simple program for a countdown timer.

In the next chapter, we will build a digital clock using several LED modules. The chapter will introduce LED modules related to the digital clock stack.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi LED Blueprints
Published in: Sep 2015Publisher: 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.
undefined
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

Author (1)

author image
Agus Kurniawan

Agus Kurniawan is an independent technology consultant, author, and lecturer. He has over 18 years' experience working on various software development projects, including delivering training courses and workshops, and delivering technical writing. He has done a few research activities related to wireless networking, software, and security in multiple universities. Currently, he is pursuing a Ph.D. program in Computer Science in Germany. He has previously written five books for Packt.
Read more about Agus Kurniawan