The Book of I2C
A Guide for Adventurers
by Randall Hyde
THE BOOK OF I2C. Copyright 2023 by Randall Hyde.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
First printing
26 25 24 23 22 1 2 3 4 5
ISBN-13: 978-1-7185-0246-8 (print)
ISBN-13: 978-1-7185-0247-5 (ebook)
Publisher: William Pollock
Managing Editor: Jill Franklin
Production Manager: Rachel Monaghan
Production Editor: Miles Bond
Developmental Editors: Abigail Schott-Rosenfield, Athabasca Witschi, and Jill Franklin
Cover Illustrator: Gina Redman
Interior Design: Octopod Studios
Technical Reviewer: Anthony Tribelli
Copyeditor: Kim Wimpsett
Compositor: Happenstance Type-O-Rama
Proofreader: Sadie Barry
For information on distribution, bulk sales, corporate sales, or translations, please contact No Starch Press, Inc. directly at info@nostarch.com or:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900
www.nostarch.com
Library of Congress Cataloging-in-Publication Data
Names: Hyde, Randall, author. Title: The book of IC : a guide for adventurers / Randall Hyde.
Description: San Francisco : No Starch Press, [2022] | Includes index. |
Identifiers: LCCN 2022018708 (print) | LCCN 2022018709 (ebook) | ISBN 9781718502468 (print) |
ISBN 9781718502475 (ebook) Subjects: LCSH: I2C (Computer bus) | Microcomputers--Programming. | Microcontrollers--Programming.
Classification: LCC TK7895.B87 H94 2022 (print) | LCC TK7895.B87 (ebook)
| DDC 621.39/16--dc23/eng/20220627
LC record available at https://lccn.loc.gov/2022018708
LC ebook record available at https://lccn.loc.gov/2022018709
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
This book is dedicated to Limor Lady Ada Fried of Adafruit and Nathan Seidle of SparkFun. Without these two great companies, I never would have thought about creating a book such as this one.
About the Author
Randall Hyde is the author of The Art of 64-Bit Assembly; The Art of Assembly Language; and Write Great Code, Volumes 1, 2, and 3 (all from No Starch Press); as well as Using 6502 Assembly Language and P-Source (Datamost). He is also the coauthor of Microsoft Macro Assembler 6.0 Bible (The Waite Group). Over the past 40 years, Hyde has worked as an embedded software and hardware engineer developing instrumentation for nuclear reactors, traffic control systems, and other consumer electronics devices. He has also taught computer science at California State Polytechnic University, Pomona, and at the University of California, Riverside. His website is https://www.randallhyde.com.
About the Technical Reviewer
Anthony Tribelli has more than 35 years of experience in software development. This experience ranges, among other things, from embedded device kernels to molecular modeling and visualization to video games. The latter includes 10 years at Blizzard Entertainment. He is currently a software development consultant and is privately developing applications utilizing computer vision.
Acknowledgments
I would like to briefly mention the people at No Starch Press who made this book possible: Abigail Schott-Rosenfield, Athabasca Witschi, Jill Franklin, Bill Pollock, the production team, and anyone else Ive missed.
I would also like to thank Anthony Tribelli for the great job he did as the technical reviewer for this book. The amount of effort he put into searching through datasheets, reviewing code, writing sample programs to test the books concepts, and otherwise ensuring this book was of the highest technical quality was amazing.
I would like to thank Adafruit and Limor Lady Ada Fried for allowing me to use the Adafruit I2C Address Compilation. This is an incredible resource for I2C programmers, and its presence in this book is no small asset.
Finally, I would like to mention No Starch Presss original The Book of SCSI: The Adventure Begins by David Deming (2nd edition by Field, Ridge, et al.), the inspiration for this books title.
Introduction
Welcome to The Book of I2C. This book provides the resources you need to design and program systems using the Inter-Integrated Circuit Bus (IIC, I2C, or I2C), a serial protocol for connecting various integrated circuits (ICs) together in computer systems. This book will teach you how to expand your embedded system design by adding I2C peripherals with minimal wiring and software.
To paraphrase from https://i2c.info, I2C uses only two wires to easily connect devices such as microcontrollers, A/D and D/A converters, digital I/O, memory, and many others, together in an embedded system. Although it was originally developed by Philips (now NXP Semiconductors), most major IC manufacturers now support I2C. I2C is popular because it is ubiquitousmost CPUs destined for embedded systems include support for I2Cand its peripheral ICs are inexpensive. It is present in hobbyist systems like Arduino and Raspberry Pi, as well as in most professional single-board computers (SBCs) intended for use in embedded systems.
The I2C bus is especially important on hobbyist-level embedded systems employed by makers working on personal projects, which typically use a commercially available off-the-shelf (COTS) SBC like an Arduino Uno, Teensy 4.x, or Raspberry Pi as the brains for the system. Such SBCs generally have limited I/O capability or other limitations, so the addition of peripheral ICs may be necessary to realize a given design. The I2C bus is one of the most popular and common ways to expand such systems, since its easy, convenient, and inexpensive to use. Furthermore, there are hundreds of different devices available as individual ICs with a wide range of capabilities that connect directly to the I2C bus. Combined with a huge library of open source code to control these devices (especially for Arduino devices), its almost trivial to expand small systems using the I2C bus.
NOTE
The SPI bus is another popular option for hobbyist systems, but discussing it will have to wait for a different book.
Although higher-end custom SBCs intended for professional embedded systems often include many of the peripherals missing in hobbyist-grade SBCs, the I2C bus is still a cost-effective way to design such systems. Often, peripherals that dont have high-performance requirements connect to the CPU on the SBC using the I2C bus.