ebook img

Beginning BBC micro:bit: A Practical Introduction to micro:bit Development PDF

262 Pages·2018·13.64 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Beginning BBC micro:bit: A Practical Introduction to micro:bit Development

TECHNOLOGY IN ACTION™ Beginning BBC micro:bit A Practical Introduction to micro:bit Development — Pradeeka Seneviratne Beginning BBC micro:bit A Practical Introduction to micro:bit Development Pradeeka Seneviratne Beginning BBC micro:bit: A Practical Introduction to micro:bit Development Pradeeka Seneviratne Mulleriyawa, Sri Lanka ISBN-13 (pbk): 978-1-4842-3359-7 ISBN-13 (electronic): 978-1-4842-3360-3 https://doi.org/10.1007/978-1-4842-3360-3 Library of Congress Control Number: 2018930256 Copyright © 2018 by Pradeeka Seneviratne 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. Cover image designed by Freepik Managing Director: Welmoed Spahr Editorial Director: Todd Green Acquisitions Editor: Natalie Pao Development Editor: James Markham Technical Reviewer: Michael Rimicans Coordinating Editor: Jessica Vakili Copy Editor: Kezia Endsley Compositor: SPi Global Indexer: SPi Global Artist: SPi Global 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 [email protected], 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. For information on translations, please e-mail [email protected], or visit http://www.apress. com/rights-permissions. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. 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-3359-7. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper Table of Contents About the Author ���������������������������������������������������������������������������������ix About the Technical Reviewer �������������������������������������������������������������xi Foreword �������������������������������������������������������������������������������������������xiii Chapter 1: Getting Ready ����������������������������������������������������������������������1 What Is the BBC micro:bit? �����������������������������������������������������������������������������������1 What’s on the micro:bit?����������������������������������������������������������������������������������3 Buying a micro:bit �����������������������������������������������������������������������������������������10 Buying a Starter Kit ���������������������������������������������������������������������������������������11 micro:bit Accessories ������������������������������������������������������������������������������������15 Powering Your micro:bit ��������������������������������������������������������������������������������������18 Powering the micro:bit with Batteries �����������������������������������������������������������19 Powering micro:bit with a USB ����������������������������������������������������������������������21 Alternative Ways to Power the micro:bit �������������������������������������������������������24 Powered through the 3v Pin ��������������������������������������������������������������������������27 Creating Your First Program with Online Python Editor ���������������������������������������28 Coding with the Online Python Editor ������������������������������������������������������������28 Coding with Mu ���������������������������������������������������������������������������������������������33 Using REPL with Mu ��������������������������������������������������������������������������������������������36 Summary�������������������������������������������������������������������������������������������������������������38 iii TTaabbllee ooff CCoonnTTeennTTss Chapter 2: Working with Display and Images ������������������������������������39 The micro:bit Built-In LED Display ����������������������������������������������������������������������39 Turning LEDs On and Off ��������������������������������������������������������������������������������41 Setting and Getting the Brightness of an LED �����������������������������������������������43 Clearing the Display���������������������������������������������������������������������������������������44 Turning the Display On and Off ����������������������������������������������������������������������45 Using Built-in Images ������������������������������������������������������������������������������������47 Creating Your Own Images ����������������������������������������������������������������������������50 Lists and Animations �������������������������������������������������������������������������������������54 Custom Animation �����������������������������������������������������������������������������������������58 Summary�������������������������������������������������������������������������������������������������������������60 Chapter 3: Working with Buttons �������������������������������������������������������61 Built-in Buttons ���������������������������������������������������������������������������������������������������61 Handling User Input with Buttons ������������������������������������������������������������������62 Connecting External Buttons �������������������������������������������������������������������������������66 Momentary Pushbuttons �������������������������������������������������������������������������������66 Using External Buttons ����������������������������������������������������������������������������������68 Connecting Buttons to GPIO ���������������������������������������������������������������������������70 Summary�������������������������������������������������������������������������������������������������������������73 Chapter 4: Using Inputs and Outputs ��������������������������������������������������75 Edge Connector ���������������������������������������������������������������������������������������������������75 Using an Edge Connector Breakout Board �����������������������������������������������������76 Experimenting with I/O Pins ��������������������������������������������������������������������������79 Touch �������������������������������������������������������������������������������������������������������������82 Analog Input and Output ��������������������������������������������������������������������������������85 Digital Input and Output ���������������������������������������������������������������������������������87 iv TTaabbllee ooff CCoonnTTeennTTss I2C (Inter-Integrated Circuit) ��������������������������������������������������������������������������90 SPI (Serial Peripheral Interface) ��������������������������������������������������������������������96 UART (Universal Asynchronous Receiver- Transmitter) ��������������������������������103 Summary�����������������������������������������������������������������������������������������������������������107 Chapter 5: Using the Accelerometer and Compass ��������������������������109 Accelerometer ���������������������������������������������������������������������������������������������������109 Reading Acceleration �����������������������������������������������������������������������������������110 Building a Spirit Level ����������������������������������������������������������������������������������114 Calculating Overall Acceleration ������������������������������������������������������������������115 Gesture Detection ���������������������������������������������������������������������������������������������116 Detecting the Current Gesture ���������������������������������������������������������������������118 Getting Gesture History �������������������������������������������������������������������������������121 Compass �����������������������������������������������������������������������������������������������������������122 Calibrating the Compass �����������������������������������������������������������������������������123 Reading Compass Values ����������������������������������������������������������������������������124 Getting Compass Heading ���������������������������������������������������������������������������126 Summary�����������������������������������������������������������������������������������������������������������131 Chapter 6: Working with Music ��������������������������������������������������������133 Connecting a Speaker ���������������������������������������������������������������������������������������133 Using Earphones ������������������������������������������������������������������������������������������141 Built-in Melodies �����������������������������������������������������������������������������������������143 Making Your Own Melodies �������������������������������������������������������������������������146 Using Octave ������������������������������������������������������������������������������������������������148 Beats �����������������������������������������������������������������������������������������������������������150 Setting the Tempo ����������������������������������������������������������������������������������������151 Getting the Tempo����������������������������������������������������������������������������������������152 v TTaabbllee ooff CCoonnTTeennTTss Resetting Attributes �������������������������������������������������������������������������������������153 Playing a Pitch ���������������������������������������������������������������������������������������������153 Summary�����������������������������������������������������������������������������������������������������������154 Chapter 7: Working with Speech ������������������������������������������������������155 Connecting a Speaker ���������������������������������������������������������������������������������������155 Timbre ���������������������������������������������������������������������������������������������������������157 Example: Creating a Robotic Voice ��������������������������������������������������������������161 Punctuation �������������������������������������������������������������������������������������������������162 Phonemes ����������������������������������������������������������������������������������������������������162 Stress Markers ��������������������������������������������������������������������������������������������167 Singing with Phonemes �������������������������������������������������������������������������������168 Summary�����������������������������������������������������������������������������������������������������������169 Chapter 8: Storing and Manipulating Files ���������������������������������������171 Creating a File ���������������������������������������������������������������������������������������������������171 Reading a File ���������������������������������������������������������������������������������������������������172 Writing Multiple Lines in a File ��������������������������������������������������������������������������174 Appending Text to a File ������������������������������������������������������������������������������������175 Creating Files with a �py Extension �������������������������������������������������������������������176 Creating Your Own Libraries ������������������������������������������������������������������������������178 File Manipulation ����������������������������������������������������������������������������������������������179 Listing Files �������������������������������������������������������������������������������������������������179 Deleting Files �����������������������������������������������������������������������������������������������180 Getting the Size of a File �����������������������������������������������������������������������������181 File Transfer with MicroFS ��������������������������������������������������������������������������������181 Installing MicroFS ����������������������������������������������������������������������������������������181 Upgrading MicroFS ��������������������������������������������������������������������������������������182 Summary�����������������������������������������������������������������������������������������������������������187 vi TTaabbllee ooff CCoonnTTeennTTss Chapter 9: Networks and Radios ������������������������������������������������������189 Building a Wired Network ���������������������������������������������������������������������������������189 Buffering Incoming Data ������������������������������������������������������������������������������192 Using Radios �����������������������������������������������������������������������������������������������������193 Turning the Radio On and Off�����������������������������������������������������������������������193 Sending and Receiving Messages ���������������������������������������������������������������194 Configuring Radio ����������������������������������������������������������������������������������������195 Remotely Controlling an LED �����������������������������������������������������������������������197 Building the Wireless Buggy ������������������������������������������������������������������������200 Summary�����������������������������������������������������������������������������������������������������������206 Appendix A: Updating DAPLink Firmware and Using REPL with  Tera Term ������������������������������������������������������������������������������������������207 DAPLink Firmware ��������������������������������������������������������������������������������������������207 Updating DAPLink Firmware ������������������������������������������������������������������������208 Maintenance Mode ��������������������������������������������������������������������������������������209 Using REPL with Tera Term �������������������������������������������������������������������������������211 Downloading mbed Serial Port Windows Driver ������������������������������������������212 Downloading Tera Term �������������������������������������������������������������������������������212 Configuring Tera Term ����������������������������������������������������������������������������������213 Writing MicroPython Code on Tera Term ������������������������������������������������������215 Appendix B: Using micro:bit and micro:bit Blue Apps on  Mobile Devices ���������������������������������������������������������������������������������219 Using the micro:bit App �������������������������������������������������������������������������������������219 Pairing with micro:bit ����������������������������������������������������������������������������������220 Writing Code with micro:bit App ������������������������������������������������������������������228 vii TTaabbllee ooff CCoonnTTeennTTss Using the micro:bit Blue App �����������������������������������������������������������������������������240 Installing micro:bit Blue �������������������������������������������������������������������������������240 Entering Pairing Mode ���������������������������������������������������������������������������������241 Pairing Your micro:bit with Your Android Phone or Tablet ����������������������������241 Using the App ����������������������������������������������������������������������������������������������244 Index �������������������������������������������������������������������������������������������������251 viii About the Author Pradeeka Seneviratne is a software engineer with over 10 years of experience in computer programming and systems design. He is an expert in the development of Arduino and Raspberry Pi-based embedded systems and is currently a full-time embedded software engineer working with embedded systems and highly scalable technologies. Previously, Pradeeka worked as a software engineer for several IT infrastructure and technology servicing companies. He collaborated with the Outernet (free data, forever) project as a volunteer hardware and software tester for Lighthouse and Raspberry Pi-based DIY Outernet receivers based on Ku band satellite frequencies. Pradeeka is the author of Building Arduino PLCs, Internet of Things with Arduino Blueprints, IoT: Building Arduino- Based Projects, and Raspberry Pi 3 Projects for Java Programmers. ix

Description:
Learn essential concepts and techniques to build simple-to-advanced projects and overcome common programming challenges in micro:bit development. Beginning BBC micro:bit will take you through the complete features and capabilities of the micro:bit controller, enabling you to program and build your o
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.