Getting Started with the Micro:Bit Coding and Making with the BBC’S Open Development Board
Total Page:16
File Type:pdf, Size:1020Kb
Getting Started with the micro:bit Coding and Making with the BBC’s Open Development Board Wolfram Donat Copyright © 2017 Wolfram Donat All rights reserved. Printed in the United States of America. Published by Maker Media, Inc., 1700 Montgomery Street, Suite 240, San Francisco, CA 94111 Maker Media books may be purchased for educational, business, or sales promo- tional use. Online editions are also available for most titles (safaribooksonline .com). For more information, contact our corporate/institutional sales depart- ment: 800-998-9938 or [email protected]. Publisher: Roger Stewart Editor: Patrick DiJusto Copy Editor and Proofreader: Elizabeth Welch, Happenstance Type-O-Rama Interior Designer and Compositor: Maureen Forys, Happenstance Type-O-Rama Cover Designer: Maureen Forys, Happenstance Type-O-Rama Indexer: Valerie Perry, Happenstance Type-O-Rama August 2017: First Edition Revision History for the First Edition 2017-08-08 First Release See oreilly.com/catalog/errata.csp?isbn=9781680453027 for release details. Make:, Maker Shed, and Maker Faire are registered trademarks of Maker Media, Inc. The Maker Media logo is a trademark of Maker Media, Inc. BOOK TITLE and related trade dress are trademarks of Maker Media, Inc. Many of the desig- nations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Maker Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-680-45302-7 Safari® Books Online Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business. Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training. Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals. Members have access to thousands of books, train- ing videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bart- lett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online. How to Contact Us Please address comments and questions to the publisher: Maker Media 1700 Montgomery St. Suite 240 San Francisco, CA 94111 You can send comments and questions to us by email at [email protected]. Maker Media unites, inspires, informs, and entertains a growing community of resourceful people who undertake amazing projects in their backyards, base- ments, and garages. Maker Media celebrates your right to tweak, hack, and bend any Technology to your will. The Maker Media audience continues to be a growing culture and community that believes in bettering ourselves, our envi- ronment, our educational system—our entire world. This is much more than an audience, it’s a worldwide movement that Maker Media is leading. We call it the Maker Movement. To learn more about Make: visit us at makezine.com. You can learn more about the company at the following websites: Maker Media: makermedia.com Maker Faire: makerfaire.com Maker Shed: makershed.com This book is dedicated to Becky and Reed, who put up with a husband and father who disappears into the workshop or office for extended periods of time on a fairly regular basis. On a related note, they also deal with a number of screwy creations flowing out of the aforementioned workshop. Contents Acknowledgments ix About the Author x 1 Introduction to the micro:bit 1 2 A Tour of the micro:bit 15 3 Programming Using MicroPython 29 The MicroPython Web Editor 30 The mu Programming Environment 33 4 Some Basic Projects 45 The LEDs 45 The Buttons 52 The Accelerometer 54 The Compass 57 The Local Persistent Filesystem 60 Putting It All Together 63 5 The mbed Operating System 69 Getting an Account 70 yotta 77 6 Interfacing with the GPIO Pins 91 The GPIO Pins and the Edge Connector Breakout Board 92 The Motor Driver Board 103 7 Using Bluetooth 109 What Is Bluetooth? 109 Programming Bluetooth on the micro:bit 111 Third-Party Apps 123 8 Serial Bluetooth Communication and the micro:bot 125 UART and Bluetooth 125 The micro:bot 134 A The Story of the BBC micro:bit 145 B Other Programming Environments 153 The Blocks Editor 153 Code Kingdoms 158 Microsoft Touch Develop 164 Index 171 viii Getting Started with the micro:bit Acknowledgments Patrick is an awesome editor who not only makes sure that I mean what I say and I say what I mean, but is also fun to work with. Bob checks that stuff works the way I say it does. Liz catches all the last-minute errors and makes sure that the end product of our collaboration looks as great as it does. And last but not least, Oliver helps to make sure the office door remains in working order, Chloe ensures that all mobile creations are capable of evasive maneuvers, and Smudge both gives and receives emotional support. I couldn't do it without you guys. About the Author Wolfram Donat is an engineer, a writer, and a maker who has written books on subjects ranging from home-built ani- matronics to Windows XP to using the Raspberry Pi in your projects. His varied interests include robotics, embedded systems, autonomous underwater vehicles, computer vision, and the Internet of Things. He received his degree in computer engineering from the University of Alaska, Anchorage and has received fund- ing from NASA for work on autonomous submersibles. He currently lives in Southern California with his wife, son, and a small menagerie. 1 Introduction to the micro:bit If you’ve been paying attention to news in the world of technology, you may have noticed that there seems to be an astonishing number of single-board computers (SBCs) hitting the market lately. In a wave of devices that may have started with the Raspberry Pi, there are now dozens of small, powerful devices, ranging in price from a few dollars to a few hundred dollars. The Pi Zero, the Raspberry Pi Foundation’s lowest-cost board, uses a small, 1 GHz, single-core ARM chip and costs about five dollars. On the other end of the spectrum, the NVIDIA Jetson TK1 includes both an ARM A57 quad-core chip and a 256-core Maxwell GPU and will cost you about six hundred dollars. It is, however, still considered a single-board computer. Kickstarter is full of new SBCs, some successful, some not. At the same time as the release of all of these surprisingly powerful small computers, various technology companies have been quietly releasing a flood of even smaller, lower- power chips and devices in the background. These boards are powered by a variety of processors, from ARM CPUs to smaller microcontrollers like the Atmega 328, and they are usually designed mainly for one purpose: performing one or more simple tasks and then interfacing with the Internet of Things (IoT). What is the Internet of Things? For the full story behind the IoT, check out the accompanying sidebar. The short version is that the IoT is a worldwide web of small, low- power devices that are able to communicate with other devices—both IoT devices and more full-featured machines such as smartphones and computers—via the Internet and other smaller networks. These devices are meant to connect everything, from your home thermostat to your refrigerator to your toaster to your keychain, and allow them to communicate via a network. They must neces- sarily subsist on almost no power (there’s no room for big, bulky batteries in your keychain) and thus must also be sort of stupid, CPU-wise. The vast majority of them don’t need to be particularly powerful, though; many times their main function is simply to collect data and relay it to a more powerful computer, smartphone, or tablet, or per- form a simple task in response to a simple command from another device. What Is the IoT? The Internet of Things, or “IoT” for those in the know, is what many call a natural evolution of technological advances in shrinking microprocessors, low-power devices, and network- ing connectivity. It’s pretty much accepted that it’s the next step in wiring the planet together and making all of our stuff smarter. 2 Getting Started with the micro:bit Up until a decade ago or so, computer processors were still too big and required too much power to be used in anything other than servers, desktops, laptops, and maybe a few clunky tablets. Firms like Intel were still trying to shrink the chips, not really worrying about putting them into small devices. The huge growth of the cell phone changed that, however.