These are exciting times for hobby computing! In the 1980s you had to round up chips for the central processing unit (CPU), erasable programmable read-only memory (EPROM), and random access memory (RAM); find some peripherals; and then wire up the numerous address and data bus lines. After all that, you still had a pretty limited 8-bit system with no operating system. Today, you can acquire a 64-bit ARM quad-core system on a chip (SoC) with 1GB of memory and several built-in peripherals already assembled! Not only that, but the system will run a Unix operating system, whether it be Linux or some flavor of BSD, complete with a compiler and a plethora of other software tools. All of this fits in the space of a credit card footprint.
You can purchase a number of Raspberry Pi “hats” to add hardware interfaces, but these come at a steeper cost than doing it yourself. Adding your own hardware interfaces is not only fairly easy to do but is incredibly fun! There are some small challenges, however, especially when interfacing the 3-volt (3V) Raspberry Pi to a 5V world. But this book will prepare you for that.
Chapter 2 focuses on interfacing between the 3V and 5V logic systems. While there are an increasing number of 3V parts today, there is still a wealth of transistor-transistor logic (TTL) parts, which are 5V, available that you might want to use. Sometimes you are simply faced with the need to communicate with a 5V interface, like that of a liquid-crystal display (LCD) display. Chapter 2 will step you through what you need to know.
Since the monitor is often a critical display component, Chapter 3 will examine how you can use discarded Video Graphics Array (VGA) monitors, available almost for free. These are perfect for embedded applications involving the Raspberry Pi Zero, for example. Chapter 4 will examine how to use the small 16×2-character LCD screens over the Pi’s inter-integrated circuit (I2C) bus.
Button and switch interfaces with metal contacts often require “debouncing.” Chapter 5 explores a hardware and software solution for debouncing. Chapter 6 covers the world of analog-to-digital (ADC) conversion and digital-to-analog (DAC) conversion. Chapter 7 covers how to read potentiometers, further exploiting the ADC concepts.
Chapter 8 covers rotary encoders so that the interface can be used in Chapter 12 as part of an embedded music-playing daemon (MPD) selection control. Chapters 9 and 10 demonstrate how the 74HC165 and 74HC595 can be used to add extra general-purpose input/output (GPIO) ports. Chapter 11 explores the more powerful MCP23017 input/output (I/O) extender chip for GPIO expansion.
Chapter 12 brings many of the presented topics together in an embedded application. In this project, a hardware rotary control is used in conjunction with an MPD to select music or Internet radio stations. A potentiometer feeds volume control information for instant digital volume adjustment. An embedded application normally runs headless, so an economical 16×2 LCD is used to round out this demonstration.
Chapter 13 wraps up the book with one more interface—the keypad. In this chapter, a PCF8574 in dual inline package (DIP) form is used to create a remote keypad using the I2C bus. The software application demonstrates a combination lock. The I2C bus permits the keypad to exist some distance away from the Raspberry Pi but using only a four-wire ribbon cable. The ribbon cable carries two lines for power and two more for I2C communication. This is also an ideal arrangement for robotics.
Raspberry Pi 3 and Zero
The two recent developments are the Raspberry Pi Zero and the Raspberry Pi 3. You could say these models are at opposite ends of the spectrum. The Zero is in high demand, mainly because of its killer price of $5. The Pi 3, on the other hand, has killer performance for the price.
This book is generally neutral about what Pi you choose to apply. The book was developed using a Raspberry Pi 3, but except for performance differences, all examples should work for all models.
Why GPIO Is Important
One of the keys to the success of the Raspberry Pi is a design that offers GPIO interfaces. In the PC era, people had to attach their home-brewed hardware through the PC’s printer parallel interface or work with the more cumbersome RS-232 interface. With the Pi, you have access to serial data, a clock, I2C, serial peripheral interface (SPI), pulse width modulation (PWM), and general I/O signaling. This arrangement is far more capable and opens a world of options to the Pi user.
What to Purchase
In addition to the basics necessary to run your Raspberry Pi, you’ll need to acquire the following items for the chapter experiments. Also listed are the specific chapters they apply to.
Breadboard
Plenty of Dupont wires (for breadboarding)
Pi Cobbler (GPIO to breadboard)
74LS04 (Chapter 2)
74LVC244 (Chapter 2)
74LVC245 (Chapter 2)
74LVC244 (Chapter 2)
CD4049 or CD4050 (Chapter 2)
74HCT244 (Chapter 2)
74HCT245 (Chapter 2)
CD4001 (Chapter 2)
High-Definition Multimedia Interface (HDMI) to VGA adapter (Chapter 3 only)
I2C Serial Interface Module with PCF8574 chip, for LCD (Chapters 4 and 12)
MC14490 chip (Chapter 5)
0.01 microfarad capacitor (Chapter 5)
Push buttons (Chapter 5)
YL-40 printed circuit board (PCB) using PCF8591 chip (Chapter 6)
3.1 kiloohm and 15 kiloohm 1/8 watt, resistors (Chapter 6)
1N914 diode (Chapter 6)
3.3 kiloohm 1/8 watt resistor (Chapter 6)
2 × 170 ohm resistor (Chapter 8)
2 × LEDs
74HC165 (Chapter 9)
74HC595 (Chapter 10)
MCP23017 (Chapter 11)
2 × PCF8574P (Chapter 13)
Hex keypad (Chapter 13)
Software to Download
For this book, you’ll need to perform two software downloads.
Download the software for this book, Exploring the Raspberry Pi 2 with C++, from here:
Download the source code for this book from either of the following locations:
Once the Exploring the Raspberry Pi 2 with C++ software is downloaded, go into its directory and install it, as shown here:
$ cd raspberry_pi2$ make all install
This will give you access to the C++ library code that this book builds upon, as well as the gp utility for controlling GPIO from the command line.
To build this volume’s source code, go into its subdirectory and type the following:
$ cd custom_interfaces_pi$ make
This will compile all the programs discussed in this book, saving you from having to do that later. The compiled executables are left in their subdirectories and are not installed anywhere else on your Raspberry Pi.
Let’s Begin
With those formalities out of the way, you can begin your exploration of hardware interfaces on the Pi!
Most of the chapters can be read out of sequence. This is helpful when you are waiting for ordered components to arrive. Before attempting any interface involving 5 volts, however, I strongly recommend that you digest Chapter 2 first.