Warren Gay

Custom Raspberry Pi Interfaces

Design and build hardware interfaces for the Raspberry Pi

Warren Gay

St Catharines, Ontario, Canada

Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842-2405-2 . For more detailed information, please visit http://www.apress.com/source-code .

ISBN 978-1-4842-2405-2

e-ISBN 978-1-4842-2406-9

DOI 10.1007/978-1-4842-2406-9

Library of Congress Control Number: 2017931068

© Warren Gay 2017

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Printed on acid-free paper

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

Contents

  1. Chapter 1:​ Introduction
    1. Raspberry Pi 3 and Zero
    2. Why GPIO Is Important
    3. What to Purchase
    4. Software to Download
    5. Let’s Begin
  2. Chapter 2:​ 3V/​5V Signal Interfacing
    1. 7400 Series (TTL)
    2. 3.​3V Logic
    3. Voltage Dividers
    4. 7400 Series Derivative Families
    5. Unused CMOS Inputs
      1. Converting 5V to 3V Input:​ 74LVC245
      2. Converting 5V to 3V Input:​ 74LVC244
      3. CD4049/​CD4050
      4. Input Protection Diodes
    6. Converting 3 Volts to 5 Volts with the HCT Family
      1. 74HCT245
      2. 74HCT244
    7. Switching Speed
    8. Summary
    9. Bibliography
  3. Chapter 3:​ VGA LCD Monitors
    1. VGA Converters
    2. Resolution and Refresh Rates
    3. /​boot/​config.​txt
    4. Confirming Resolution
    5. Summary
    6. Bibliography
  4. Chapter 4:​ I2C LCD Displays
    1. LCD Module 1602A
    2. I2C Serial Interface
    3. I2C Module Configuration
    4. I2C Module Output
    5. I2C Module Input
    6. PCF8574P Chip
    7. 3 Volts to 5 Volts
    8. Attaching the I2C Serial Module
    9. Displaying Data
    10. Reading from the LCD1602
    11. Class LCD1602
    12. I2C Baud Rate
    13. Profit and Loss
    14. Summary
  5. Chapter 5:​ MC14490 and Software Debouncing
    1. Hardware:​ MC14490
    2. Chip Operation
    3. Capacitor C 1
    4. Experiment
    5. More Inputs
    6. Software Debouncing
    7. Experiment
    8. Summary
  6. Chapter 6:​ PCF8591 ADC
    1. The YL-40 PCB
    2. Voltage Range
    3. I2C Bus
    4. I2C Addresses
    5. DAC (AOUT)
    6. Removing YL-40 LED D1
    7. Hacking YL-40 I2C Address
    8. I2C Bus Setup
    9. Reading from PCF8591
      1. Experiment
    10. Writing to the DAC
      1. Experiment
      2. Experiment
    11. Limitations
    12. Extending Voltage Range
    13. Repairing the Temp Sensor
    14. Conversion to Celsius
    15. Reading Temperature
      1. Experiment
    16. The YL-40 LDR
      1. Experiment
    17. 1N914 Experiment
    18. Software
    19. Potential Experiments
    20. Summary
    21. Bibliography
  7. Chapter 7:​ Potentiometer Input Controls
    1. Potentiometers
    2. Voltage Dividers
    3. ADC Circuit
    4. Pot Resistance
    5. Taper
    6. Effect of ADC Bits
    7. Experiment
    8. Applying Potentiometer Controls
    9. Selection Resolution
    10. Summary
    11. Bibliography
  8. Chapter 8:​ Rotary Encoders
    1. Keyes KY-040 Rotary Encoder
    2. The Switch
    3. Operation
    4. Voltage
    5. Evaluation Circuit
    6. Interfacing to the Pi
    7. Experiment
    8. Experiment
    9. Sequence Errors
    10. Experiment
    11. FM Dial 1
    12. FM Dial 2
    13. Class Switch
    14. Main Routine
    15. Summary
    16. Bibliography
  9. Chapter 9:​ More Pi Inputs with 74HC165
    1. 74HC165 Pinout
    2. Function Table
    3. Breadboard Experiment
    4. Program
    5. Logic Analyzer View
    6. Profit and Loss
    7. Even More Inputs
    8. Other Bit Counts
    9. Combining GPIOs
    10. Chip Enable
    11. CD4012B
    12. Summary
  10. Chapter 10:​ More Pi Outputs with 74HC595
    1. 74HC165 Pinout
    2. Function Table
    3. Breadboard Experiment
    4. Experiment Run
    5. Input and Output
    6. Additional Outputs
    7. Profit and Loss
    8. Summary
  11. Chapter 11:​ MCP23017 I/​O Port Extender
    1. MCP23017
    2. Wiring
    3. Output GPIO Experiment
    4. Input Experiment
    5. Software Operations
      1. I2C Header Files
      2. Opening the I2C Driver
      3. I2C Write
      4. I2C Read
      5. Configuration
    6. Interrupt Capability
    7. Interrupt Profit and Loss
    8. Summary
  12. Chapter 12:​ MPD/​MPC Hardware Controls
    1. Audio Preparation
      1. MPD/​MPD
    2. Hardware Setup
      1. Test Volume Control
      2. Test Rotary Control
      3. Test LCD
    3. The mpcctl Program
      1. Main Program
      2. Rotary Encoder Thread
      3. LCD Thread
      4. MPC Status Thread
      5. Volume Control Thread
      6. Program Summary
    4. Summary
    5. Bibliography
  13. Chapter 13:​ Custom Keypads
    1. Breadboard Setup
    2. Output Tests
    3. Input Tests
    4. High-Side Driver
    5. Low-Side Driver
    6. Driving an LED
    7. The Keypad
    8. Keypad Program
      1. The main Program
      2. The key_​lookup Function
      3. The i2c_​write Function
      4. The i2c_​read Function
    9. Combination Lock
    10. Combination Lock
      1. The main Program
      2. The get_​key_​code Function
    11. Interrupts
    12. Summary
    13. Bibliography
  14. Index

About the Author and About the Technical Reviewer

About the Author

Warren Gay started out in electronics at an early age, dragging discarded TVs and radios home from public school. In high school he developed a fascination for programming the IBM 1130 computer, which resulted in a career plan change to software development. After attending Ryerson Polytechnical Institute, he has enjoyed a software developer career for more than 30 years, programming mainly in C/C++. Warren has been programming Linux since 1994 as an open source contributor and professionally on various Unix platforms since 1987.

A432417_1_En_BookFrontmatter_Figb_HTML.jpg

Before attending Ryerson, Warren built an Intel 8008 system from scratch before there were CP/M systems and before computers got personal. In later years, Warren earned an advanced amateur radio license (call sign VE3WWG) and worked the amateur radio satellites. A high point of his ham radio hobby was making digital contact with the Mir space station (U2MIR) in 1991.

Warren works at Datablocks.net, an enterprise-class ad-serving software services company. There he programs C++ server solutions on Linux back-end systems.

About the Technical Reviewer

Massimo Nardone has more than 22 years of experience in security, web/mobile development, the cloud, and IT architecture. His true IT passions are security and Android.

A432417_1_En_BookFrontmatter_Figc_HTML.jpg

He has been programming and teaching how to program with Android, Perl, PHP, Java, VB, Python, C/C++, and MySQL for more than 20 years.

He has a master’s of science degree in computing science from the University of Salerno, Italy.

He has worked as a project manager, software engineer, research engineer, chief security architect, information security manager, PCI/SCADA auditor, and senior lead IT security/cloud/SCADA architect for many years.

His technical skills include the following: security, Android, cloud, Java, MySQL, Drupal, Cobol, Perl, web and mobile development, MongoDB, D3, Joomla, Couchbase, C/C++, WebGL, Python, Pro Rails, Django CMS, Jekyll, Scratch, and more.

He currently works as chief information security officer (CISO) for Cargotec Oyj.

He has worked as a visiting lecturer and supervisor for exercises at the Networking Laboratory of the Helsinki University of Technology (Aalto University). He holds four international patents (PKI, SIP, SAML, and proxy areas).

Massimo has reviewed more than 40 IT books for different publishing company and is the coauthor of Pro Android Games (Apress, 2015).