Micro:bit IoT In CSecond EditionHarry FairheadI/O PressI Programmer Library Copyright 2021 IO Press All rights reserved. This book or any portion thereof may not be reproduced or used in any manner whatsoever without the express written permission of the publisher except for the use of brief quotations in a book review. Harry Fairhead, Micro:bit IoT In C ISBN Paperback: 9781871962673 Second Edition First Printing, February 2021 Revision 0 Published by IO Press www.iopress.info In association with I Programmer www.i-programmer.info and with I o T Programmer www.iot-programmer.com The publisher recognizes and respects all marks used by companies and manufacturers as a means to distinguish their products. All brand names and product names mentioned in this book are trade marks or service marks of their respective companies and our omission of trade marks is not an attempt to infringe on the property of others. In particular we acknowledge that "BBC and micro:bit are trade marks of the BBC. Preface The BBC micro:bit is a remarkable device capable of taking on a variety of roles.
When it was originally launched in 2015 it was the preserve of school children, with one million given away free via secondary schools in the UK and the emphasis was on introducing this audience to programming and digital technology. By the time the first edition of this book was written it had gone on public sale and the fact that it is an ideal vehicle for IoT could come to the fore. Writing this new edition was prompted by the advent of the micro:bit V2, an enhanced version based on a different, more powerful microprocessor. Whereas the original version is an mbed device, the V2 has broken free, giving it new capabilities but also presenting problems by not being fully backward compatible with its predecessor. Tempting as it might be to disregard the earlier version, the fact that there are so many of them, more than four million, out in the wild and that many people already have invested time and effort in working projects, rules out this option, so this book caters for both versions and points out the differences between that might trip up the unwary developer. This book isnt an introduction to the micro:bit.
For this you are recommended to visit its own website, https://www.microbit.co.uk/ where you will find activities with step-by-step guidance for beginners. Instead this book assumes you have taken you first steps with programming, have an interest in electronics and want to use the micro:bit for Internet of Things projects, that is any situation in which you want to interact with real world objects. While much of the introductory material for the micro:bit uses MicroPython, this book uses C in order to realize its full potential as an IoT device. The main reason for choosing C is speed which is crucial when you are writing programs to communicate with the outside world. Whereas the first edition used NetBeans and Yotta this book uses Visual Studio Code as its development environment. The only problem with this is setting it up and to make this easier there are two templates, one for the V1 and one for the V2 micro:bit available for download from the I Programmer GitHub repository.
You dont need to use these however if you are feeling up to tackling Yotta and Cmake from scratch. How to do this is described in the appendixes at the end of the book. After setting everything up and compiling and running a sample program we move on to a Blinky C program. Now we are ready to discover how to control the micro:bits I/O lines, exploring the basis of using the GPIO. For speed, however, we need to work directly with the raw hardware and also master memory mapping, pulse width modulation and other more sophisticated bus types. From here we can start connecting sensors using first the I2C bus, then by implementing a custom protocol for a 1-Wire bus, and eventually adding eight channels of 12-bit A-to-D with the SPI bus, which involves overcoming some subtle difficulties.
We then look at serial connections, one of the oldest ways of connecting devices but still very useful. The micro:bit lacks WiFi connectivity, but using a low-cost device we enable a connection to the Internet via its serial port which allows it to become a server. Next we look at the micro:bits LED display. This may only be 5x5, but it is very versatile, especially when you use pulse width modulation to vary the brightness level, something we demonstrate in a classic game, written of course in C. Finally we look at how to use the radio and the new sound capabilities of the micro:bit V2 and finish with an example of a two-way Morse code radio link. Thanks as ever to my painstaking editors Sue Gee and Kay Ewbank for their input to this edition.
For updates, errata, links to resources and the source code for the programs in this book visit its dedicated page on the IO Press website: www.iopress.info. Harry Fairhead February, 2021 Table of Contents
Preface
3
Chapter 1
The Micro:bit Family
Meet the Micro:bits...........................................................................11 Why Choose a Micro:bit?..................................................................14 The Software.....................................................................................14 Why C/C++?.....................................................................................16 Summary...........................................................................................18
Chapter 2
Getting Started With C
Prerequisites......................................................................................20 VS Code.............................................................................................21 Hello World For Micro:bit V1............................................................22 Hello World For Micro:bit V2............................................................27 Your First Program - Blinky...............................................................29 Universal Hex....................................................................................30 Summary...........................................................................................31
Chapter 3
First Steps With The GPIO
Pinouts...............................................................................................33 GPIO Capabilities..............................................................................37 Digital GPIO.......................................................................................38 Drive Type..........................................................................................39 GPIO Drive Characteristics...............................................................42 An FET Buffer....................................................................................43 GPIO Output......................................................................................44 Phased Pulses....................................................................................47 GPIO Input.........................................................................................50 A Basic Input Circuit - The Switch..................................................50 The Potential Divider........................................................................51 How Fast Can We Measure?..............................................................52 Interrupts...........................................................................................54 Summary...........................................................................................58
Chapter 4Fast Memory-Mapped GPIO The GPIO Registers...........................................................................59 A Fast Pulse........................................................................................62 Phased Pulses....................................................................................65 How Fast Can Input Be?....................................................................68 Summary...........................................................................................70
Next page