BBC micro:bit Dogan Ibrahim
He is the author of over 60 technical books, published by international famous publishers, such as Wiley, Butterworth, and Newnes. In addition, he is the author of over 250 technical papers, published in journals, and presented in seminars and conferences.
ISBN 978-1-907920-54-7
The device is based on the Cortex-M0 processor and it measures 4 x 5 cm. It includes several important sensors and modules such as an accelerometer, magnetometer, 25 LEDs, 2 programmable push-button switches, Bluetooth connectivity, micro USB socket, 5 ring type connectors, and a 23-pin edge connector. The device can be powered from its micro USB port by connecting it to a PC, or two external AAA type batteries can be used.
35 Touch Develop & microPython Projects
BBC micro:bit
● DOGAN IBRAHIM
Prof Dr Dogan Ibrahim is a Fellow of the Institution of Electrical Engineers.
The BBC micro:bit is a credit sized computer based on a highly popular and high performance ARM processor. The device was designed by a group of 29 partners for use in computer education in the UK and will be given free of charge to every secondary school student in the UK.
BBC micro:bit - 35 PROJECTS
35 Touch Develop & microPython Projects
This book is an introduction to the BBC micro:bit computer and it is aimed for the people who want to learn to program the BBC micro:bit computer using the Touch Develop or microPython programming languages. 35 tried and tested interesting projects are explored in the book. All the software examples for the projects are available for download. LEARN
www.elektor.com
DESIGN
Elektor International Media BV
Dogan Ibrahim LEARN
DESIGN
SHARE
SHARE
LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DES ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● GN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DE ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ●
PREFACE
PREFACE The BBC micro:bit is a credit sized computer based on a highly popular and high performance ARM processor. The device is designed by a group of 29 partners for use in computer education in the UK and will be given free of charge to every secondary school student in the UK. The device, based on the Cortex-M0 processor, measures 4 x 5 cm and includes several important sensors and modules such as an accelerometer, magnetometer, 25 LEDs, 2 programmable push-button switches, Bluetooth connectivity, micro USB socket, 5 ring type connectors, and a 23-pin edge connector. The device can be powered from its micro USB port by connecting it to a PC, or two external AAA type batteries can be used. ARM offers 32-bit and 64-bit processors mainly for the embedded applications. Nowadays, the majority of mobile devices such as mobile phones, tablets, and GPS receivers are based on the ARM processors. The low cost, low power consumption, and high performance of the ARM processors make them ideal candidates to be used in complex communication and mixed signal applications. The BBC micro:bit was created using the ARM mbed development environment which is basically a web based interface used to compile user’s code which can then be uploaded to the target device using USB or Bluetooth connections. The BBC micro:bit appears as a USB drive when connected to a computer. The compiled code can be dragged and dropped to this drive so that it is uploaded to the BBC micro:bit automatically. This book is about the use of the BBC micro:bit computer in practical projects. The BBC micro:bit computer can be programmed using several different programming languages, such as Microsoft Block Editor, Microsoft Touch Develop, microPython, and JavaScript. The book makes a brief introduction to the Touch Develop programming language and the microPython programming language. It then gives 35 example working and tested projects using these language. Readers who learn to program in Touch Develop and microPython should find it very easy to program using the Block Editor or any other languages. The following are given for each project: • Title of the project • Description of the project • Aim of the project • Touch Develop and microPython program listings Complete program listings are given for each project. In addition, working principles of the projects are described briefly in each section. Readers are encouraged to go through the projects in the order given in the book.
●5
BBC Microbit UK 160929.indd 5
25-11-16 12:04
Contents PREFACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Chapter 1 • Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.1 What is a microcontroller? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.2 Where do we find microcontrollers? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.3 How does a microcontroller work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.4 What is a computer program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.5 What is an algorithm? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Chapter 2 • Touch Develop Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.1 Creating Touch Develop programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.2 Touch Develop commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Chapter 3 • The BBC micro:bit computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 3.2 LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 3.3 Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.4 Compass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.5 Accelerometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.6 Large pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.7 Micro USB socket and batteries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.8 Hardware development tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.9 What is in the box ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Chapter 4 • MicroPython Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 4.1 Variable names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 4.2 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 4.3 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 4.4 String variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4.5 List variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4.6 Arithmetic operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4.7 Comparison operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 4.8 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 4.9 Assignment operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 4.10 Control of flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 4.11 Trigonometric functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
●7
BBC Microbit UK 160929.indd 7
25-11-16 12:04
BBC micro:bit 4.12 Mathematical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 4.13 String functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 4.14 Creating a microPython program on the BBC micro:bit . . . . . . . . . . . . . . . . . . . 50 4.14.1 microPython commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Chapter 5 • BBC micro:bit Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Project 1 - Turn on BBC micro:bit LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Project 2 - Flash a BBC micro:bit LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Project 3 - Counting numbers on the LED matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Project 4 - LED animation using the BBC micro:bit LEDs . . . . . . . . . . . . . . . . . . . . . . 68 Project 5 - Button input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Project 6 - Button following LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Project 7 - Flash an external LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Project 8 - Lighthouse LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Project 9 - Flash an LED with happy face . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Project 10 - Chasing LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Project 11 - External button input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Project 12 - Reaction timer with button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Project 13 - Digital thermometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Project 14 - Digital thermostat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Project 15 - Generating musical tones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Project 16 - Steady hands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Project 17 - Noise generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Project 18 - Simple metronome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Project 19 - More advanced metronome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Project 20 - Measuring the light level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Project 21 - Darkness reminder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Project 22 - RGB LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Project 23 - Randomly changing LED colours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Project 24 - Quiz game controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Project 25 - DC motor control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Project 26 - Changing the speed of the motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
●8
BBC Microbit UK 160929.indd 8
25-11-16 12:04
Contents Project 27 - Spirit level with buzzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Project 28 - Serial Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Project 29 - Send the temperature to a PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Project 30 - Display temperature on the PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Project 31 - Times table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Project 32 - What direction are we heading to? . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Project 33 - Calculator using the PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Project 34 - Protect yourself from the ultraviolet rays . . . . . . . . . . . . . . . . . . . . . . . 141 Project 35 - Servo Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Chapter 6 • Saving your program and working with a mobile device . . . . . . . . . . 149 6.1 Saving and recalling a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 6.2 Accessing BBC micro:bit using a mobile device . . . . . . . . . . . . . . . . . . . . . . . . . 149 6.2.1 Accessing using an Android phone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Appendix • Converting from Block Editor to Touch Develop . . . . . . . . . . . . . . . . . 153
●9
BBC Microbit UK 160929.indd 9
25-11-16 12:04
Chapter 1 • Microcontrollers
Chapter 1 • Microcontrollers In this chapter we shall be looking at what a microcontroller is and where it is used. In addition, other topics such as how a computer works, what a computer program is, and the basic features of microcontrollers are also described briefly 1.1 What is a microcontroller?
A microcontroller is a single chip computer. The device is small (micro) and can be used in control applications (controller). Another term for a microcontroller is embedded controller, since most microcontrollers in domestic, commercial, and industrial applications are built into (or embedded in) the devices they control. 1.2 Where do we find microcontrollers?
Microcontrollers are used in many applications at home, in offices, in factories, at hospitals, in cars, and many other places. Some examples are: At home: in microwaves, televisions, cookers, fridges, washing machines, mobile phones, computers, printers, remote control units, watches and clocks. In offices: computers, printers, plotters, copy machines, fax machines, mobile phones, and telephone exchange systems. In factories: computers, printers, plotters, copy machines, manufacturing machinery, and burglary alarm systems. At hospitals: computers, printers, blood pressure devices, ECG devices, X-ray machines, ultrasound machines, and most of other medical devices. In cars: a network of microcontrollers are used to control the engine, windows, mirrors, climate control, brakes, and lighting. 1.3 How does a microcontroller work?
A microcontroller is the brain of a computer. As shown in Figure 1.1, a microcontroller receives data from its input ports such as from keyboards, keypads, buttons, the mouse, or sensors. This data is processed inside the microcontroller to perform the required operation. The result is then output through its output ports. For example, consider a very simple system consisting of a button, a light, and a microcontroller where the button and the light are connected to the input and output ports of the microcontroller respectively. In this system a program runs inside the microcontroller that checks the state of the button continuously and turns the light on when the button is pressed. As with many other microcontroller based systems, there are three parts here: INPUT, PROCESSING, OUTPUT. In some systems there is only processing and output, for example in a simple clock application the date and time are displayed continuously on a Liquid Crystal Display (LCD) with no inputs to the system.
â—? 11
BBC Microbit UK 160929.indd 11
25-11-16 12:04
BBC micro:bit
Figure 1.1 A microcontroller based system Some example microcontroller based systems are given below. Figure 1.2 shows an example microcontroller based liquid level control system. Here, the microcontroller controls the level of the liquid inside the tank. Liquid is pumped from the reservoir to the tank. A liquid level sensor detects the level of the liquid continuously and sends this information to the microcontroller. The microcontroller then stops or starts the pump so that the liquid is at the desired level.
Figure 1.2 Microcontroller based liquid level control system
â—? 12
BBC Microbit UK 160929.indd 12
25-11-16 12:04
BBC micro:bit
Chapter 2 • Touch Develop Programming In this chapter we will be looking at the Touch Develop programming environment and see how a BBC micro:bit computer program can be developed using this easy and popular programming tool. 2.1 Creating Touch Develop programs
The steps in creating a Touch Develop program are given below. Notice that the same initial steps should be used in all the projects in this book to create, load, and run a Touch Develop program. Before starting to write a program make sure that you have the following: • A PC or laptop running Windows 7 or later (or a MAC running OS X 10.6 or later) • A micro USB cable to connect your computer to the BBC micro:bit • Depending upon your computer operating system: Internet Explorer, Google Chrome, • Firefox, or Safari software • Internet access Connect your BBC micro:bit to your computer using the supplied micro USB cable. Your computer should recognise the BBC micro:bit as a new drive and on Windows computers you should see a new device called MICROBIT (you could see the new drive under the Computer folder). When everything is working you should see the orange status light at the bottom of the BBC micro:bit steady ON. Your BBC micro:bit is now ready to be programmed. The steps to create a new program are (it is assumed in this book that you are using Google Chrome): • Go to www.microbit.co.uk using Google Chrome on your PC. You should see the BBC micro:bit website startup screen as in Figure 2.1
Figure 2.1 BBC micro:bit website start-up screen
● 28
BBC Microbit UK 160929.indd 28
25-11-16 12:04
Chapter 2 • Touch Develop Programming
• Click on Create Code and then click on New project in Microsoft Touch Develop which is at the right hand side of the screen as shown in Figure 2.2.
Figure 2.2 Click on New project in Microsoft Touch Develop • You should see the Touch Develop start-up screen as in Figure 2.3.
Figure 2.3 Touch Develop start-up screen A Touch Develop program starts with the keywords Script, followed by a name, followed by the keyword function main() and function name, and terminated by the keyword end function.
● 29
BBC Microbit UK 160929.indd 29
25-11-16 12:04
BBC micro:bit
2.2 Touch Develop commands
A list of the commonly used Touch Develop commands is given in this section with a brief description of each command. BASIC basic -> show leds(leds:String, ms:Number) Turns ON the specified LEDs leds: led string ms: time to wait in ms after displaying basic -> show number(number, interval) Displays a number on LED matrix number: number to be displayed interval: time before scrolling left by one LED (in ms) basic -> show string(text, interval) Displays a text on LED matrix text: string to be displayed interval: time before scrolling left by one LED (in ms) basic -> clear screen Clears the screen basic -> pause(number) Delays program execution number: delay time in ms basic -> forever do Your code end Executes a forever loop basic -> show animation(LEDs, number) Shows a series of image frames made up from the LEDs Number: the interval between each frame in milliseconds LED led -> plot(X, Y) Turn ON the LED at given X,Y co-ordinate X: x-coordinate (horizontal) having values 0,1,2,3,4 Y: y-coordinate (vertical) having values 0,1,2,3,4 led -> plot all Turn ON all LEDs
â—? 30
BBC Microbit UK 160929.indd 30
25-11-16 12:04
Chapter 3 • The BBC micro:bit computer
Chapter 3 • The BBC micro:bit computer In this chapter we shall be looking briefly at the hardware details of the BBC micro:bit computer. The specifications and board layout of the BBC micro:bit are described in this chapter in some detail. 3.1 Specifications The BBC micro:bit is a small 5 x 4 cm microcontroller board developed with the partnership of 25 organisations. The specifications of the board are: • nRF51822 32-bit ARM Cortex M0 microcontroller (16 MHz) • 16 KB RAM • 256 KB flash memory • Bluetooth Low energy • MMA8652 3-axis accelerometer • MAG3110 3-axis magnetometer • 5x5 LED matrix • Two push-buttons • 5 banana/crocodile connectors • SPI, I2C and I/O edge connector • USB connector • 3V power connector ARM is a family of processors developed by Advanced RISC Machines (ARM). ARM processors are small and very fast processors used currently in many consumer devices such as in smart mobile phones, tablets, multimedia players, GPS navigation devices and in many more portable devices. The board is designed to be safe and is thus powered from 2 x AAA 1.5V batteries. The board’s shape has been designed to appeal to children and looks like a face with the push-buttons being the eyes, the LEDs the nose, and the edge connector the teeth. Before using the BBC micro:bit computer it is important to know what is on the board and the board layout. Figure 3.1 shows the board layout. At the front of the board we have: • 3 digital/analog input/output rings (marked 0, 1 and 2) • 25 LEDs in 5 x 5 matrix • Power pin (3V) • Ground pin • 2 programmable push-buttons (A and B) • Edge connector
● 37
BBC Microbit UK 160929.indd 37
25-11-16 12:04
BBC micro:bit
At the back of the board we have: • Accelerometer chip • Compass chip • ARM Cortex-0 microcontroller chip • Micro USB connector • Battery connector • Bluetooth antenna (embedded) Five pins on the board have been designed as large holes to accept banana plugs and crocodile clips for quick and easy connection to other external hardware. The 5 x 5 LED matrix is actually laid out as a 3 x 9 matrix. The pins shared with the LEDs can be used as general purpose input/output pins when the LED screen is turned off.
Figure 3.1 BBC micro:bit board layout Pin 5 (Button A) and pin 11 (Button B) pins have pull-up resistors to 3V power supply. This means that the states of these pins are normally at logic 1 and go to logic 0 when the button is pressed. The pin configuration of the edge connector is shown in Figure 3.2 Five pins on the board have been designed as large holes to accept banana plugs and crocodile clips for quick and easy connection to other hardware. The 5 x 5 LED matrix is actually laid out as a 3 x 9 matrix. The pins shared with the LEDs can be used as general purpose input/output pins when the LED screen is turned off. 3.2 LEDs
There are 25 LEDs on the front of the board that can be turned ON or OFF individually under program control.
● 38
BBC Microbit UK 160929.indd 38
25-11-16 12:04
Chapter 3 • The BBC micro:bit computer
The 25 LEDs have the following x and y co-ordinates, where x is the horizontal and y is the vertical co-ordinate. Since the row and column numbers start at 0, an easy way to figure out the x, y coordinates is to subtract 1 from the row and column number (when counting from 1).
Figure 3.2 Edge connector configuration In other words, to specify the LED in the 4th column 5th row, subtract 1 from each number to get coordinates 3,4. 0, 0, 0, 0, 0,
0 1 2 3 4
1, 1, 1, 1, 1,
0 1 2 3 4
2, 2, 2, 2, 2,
0 1 2 3 4
3, 3, 3, 3, 3,
0 1 2 3 4
4, 4, 4, 4, 4,
0 1 2 3 4
The yellow light at the back of the board is the status light and flashes when something happens on the board.
â—? 39
BBC Microbit UK 160929.indd 39
25-11-16 12:04
BBC micro:bit
Chapter 4 • MicroPython Programming In this chapter we shall be briefly looking at some of the commonly used microPython programming concepts. 4.1 Variable names
MicroPython variable names are case sensitive and can start with a letter A to Z or a to z or an underscore character (_), followed by optional numbers. Variable names Count, count, COUNT, counT etc represent all different variables. Some variable names are: Count - valid Count123 - valid 8loop - invalid %total - invalid My_name - valid _cnt - valid 4.2 Comments
Lines starting with a hash character (#) are comment lines and they are ignored by microPython. A comment line can either be at the beginning of a line or after a statement: # This is a comment line Cnt = Cnt + 1 # This is another comment 4.3 Indentation
Indentation is very important in microPython programs. Most programming languages are free format where blocks of code are identified using braces at the beginning and end of a block, or by using suitable statements, e.g. WEND, ENDIF, END. In microPython, blocks of code are identified by the indentation of the code. Thus, all statements within a block must be indented by the same amount. For example, in the following code, statements x = 3 and y = 5 belong to the if block, and statement a = 1 and b = 3 belong to the else block: if p == 0: x=3 y=5 else: a=1 b=3 The following code is not valid since the indentation is not correct:
if p == 0: x=3 y=5
â—? 46
BBC Microbit UK 160929.indd 46
25-11-16 12:04
Chapter 4 • MicroPython Programming
else: a=1 b=3 4.4 String variables
In microPython programming language, strings are declared by enclosing them between a pair of single or double quotes:
name = “David Evans”
The characters of a string can be extracted using square brackets and specifying the indexes. Notice that the first character has index 0:
P = name[0] P = name[1:3] P = name[2:]
# gives character D # gives character av # gives character vid Evans
Strings can be concatenated by using the + sign:
P = name + ‘ 25’ # gives David Evans 25
The * character can be used to repeat strings:
P = name*2
# gives David EvansDavid Evans
4.5 List variables
List variables are used to store related items in an array. The variables in a list must be enclosed in a pair of square brackets and separated by commas:
fruits = [‘apple’, ‘orange’, ‘banana’, ‘cherry’] p = fruits[0] # gives apple p = fruits[2] # gives banana p = fruits[1:3] # gives orange,banana p = fruits[1:] # gives orange,banana,cherry
4.6 Arithmetic operators
The following arithmetic operators are supported: • + addition • - subtraction • * multiplication • / division • % modulus (returns the remainder) • ** exponent
● 47
BBC Microbit UK 160929.indd 47
25-11-16 12:04
Chapter 5 • BBC micro:bit Projects
Chapter 5 • BBC micro:bit Projects In this chapter we will be developing simple programs to run on the BBC micro:bit computer. All the programs are developed using the Microsoft Touch Develop and the microPython programming languages. The steps required to create a program, load it into the memory of the BBC micro:bit computer and then run it are explained in this section. The following sub-headings will be given for each project: Description: Brief description of the project Aim: The aim of the project, explaining what is to be learned from the project Programming Steps: The programming steps are given with full Touch Develop and microPython program listings and description of the program. Try for Yourself: Ideas related to the project to try for yourself If the project uses an external component then the block diagram and the circuit diagram of the project will also be given. It is recommended that you run your program on the simulator if possible before uploading it to the actual BBC micro:bit computer. Project 1 - Turn on BBC micro:bit LEDs
Description: In this project the 5 LEDs on the first row of the LED matrix will turn ON Aim: The aim of this project is to show how to use the show leds statement and how to compile and run a Touch Develop program. Touch Develop Programming • Start a new project as described earlier. You should see a script name and a function block. Change the name of the script to LEDs by clicking on the existing name • Click add code here inside the function main() • You should see the list of valid Touch Develop commands at the bottom of the screen • Click basic and then show leds. You should see the LED matrix displayed. Click the LEDs to be turned ON as shown in Figure 5.1
● 57
BBC Microbit UK 160929.indd 57
25-11-16 12:04
BBC micro:bit
Figure 5.1 Select the top 5 LEDs • Click OK to exit from the screen • Click on the screen and your program should be as in Figure 5.2.
Figure 5.2 Touch Develop program of the project • Click the run main button at the top of the screen to simulate the program and you should see the top 5 LEDs to turn ON in the BBC micro:bit simulator model at the right hand side of the screen (Figure 5.3). Simulation is very useful as it can tell us how our program will behave on the actual device before uploading the program to the actual device. At this stage we are sure that our program is correct and it will be working on the actual BBC micro:bit.
● 58
BBC Microbit UK 160929.indd 58
25-11-16 12:04
BBC micro:bit
Figure 5.40 Project built on a breadboard Project 11 - External button input
Description: In this project an external button is connected to pin 0 of the BBC micro:bit. When the button is pressed the project displays a Happy face on the LED matrix. Aim: The aim of this project is to show how an external button can be used with your BBC micro:bit computer. In addition, it shows how a random number can be generated in a program. Block Diagram: The block diagram of this project is as in Figure 5.41.
â—? 84
BBC Microbit UK 160929.indd 84
25-11-16 12:04
Chapter 5 • BBC micro:bit Projects
Figure 5.41 Block diagram of the project Circuit Diagram: The circuit diagram of this project is as in Figure 5.42. A 10K resistor is connected to one side of the button and pin 0 is connected to this terminal. The other side of the button is connected to +3.3V. Therefore, when the button is not pressed pin 0 is at logic 0. When the button is pressed, pin 0 is connected to +3.3V, thus the pin becomes at logic 1.
Figure 5.42 Circuit diagram of the project Touch Develop Program The program of this project is named Button input shown in Figure 5.43. The program detects when the button connected to pin 0 is pressed and displays a Happy face.
â—? 85
BBC Microbit UK 160929.indd 85
25-11-16 12:04
Chapter 5 • BBC micro:bit Projects
• Write a program to read the ambient temperature from the TMP36 chip and display it in Degrees Fahrenheit. Figure 5.56 shows the circuit built on a breadboard.
Figure 5.56 Project built on a breadboard Project 14 - Digital thermostat
Description: This is a digital thermostat project. The project uses the TMP36 temperature sensor as in the previous project, but in this project the temperature sensor output is connected to pin 1. Additionally, a buzzer is connected to pin 0 of the BBC micro:bit computer. The operation of the project is such that if the temperature is above 30ºC or below 20ºC then the buzzer is activated. Aim: The aim of this project is to show how a thermostat program can be written.
● 93
BBC Microbit UK 160929.indd 93
25-11-16 12:04
BBC micro:bit
Block Diagram: The block diagram of the project is shown in Figure 5.57.
Figure 5.57 Block diagram of the project Circuit Diagram: The circuit diagram of the project is shown in Figure 5.58. The output of the temperature sensor chip is connected to pin 1 and the + pin of the buzzer is connected to pin 0 of the BBC micro:bit computer. Don’t forget to connect the power and ground lines of the temperature sensor chip and connect other pin of the buzzer to ground.
Figure 5.58 Circuit diagram of the project Buzzer is a two terminal device that generates sound in the audible frequency range. It has two pins: + and ground. The + pin must be connected to pin 0 of the BBC micro:bit computer, while the ground pin must be connected to 0V (the default buzzer port is pin 0). BBC micro:bit computer includes a library of functions for generating sound (or music) on a buzzer. When using the MicroPython programs it is necessary to import the music library at the beginning of our programs. Then function music.pitch(f, d) generates sound on the buzzer with frequency f and duration d milliseconds. Touch Develop Program The program is shown in Figure 5.59, named Thermostat. The program runs in an endless loop. Inside this loop the temperature is read from analog input 0 and is converted into degrees centigrade. If the temperature is greater than or equal to 30ºC or less than or equal to 20ºC then the buzzer is activated for 5 seconds.
● 94
BBC Microbit UK 160929.indd 94
25-11-16 12:04
BBC micro:bit
Project 22 - RGB LED
Description: This project uses an RGB (Red, Green and Blue) LED to generate different colours. RGB LEDs basically have 3 built-in LEDs that display the colours of Red, Green, and Blue when powered individually. An RGB LED has 4 pins: Red, Green, Blue, and common (which is usually common cathode, connected to ground. i.e. 0V). RGB LEDs are usually controlled from PWM type signals so tha the intensity of each colour can easily be changes and therefore different colours can be obtained from a single LED. Figure 5.89 shows a typical RGB LED.
Figure 5.89 Typical RGB LED Aim: The aim of this project is to show how an RGB LED can be used in a project. Block Diagram: The block diagram of the project is shown in Figure 5.90. Circuit Diagram: The circuit diagram of the project is shown in Figure 5.91. The RGB LED is connected as follows: G pin to 0, B pin to 1, common pin to 0V, and R pin to 2. 270 ohm current limiting resistors are used in the design.
Figure 5.90 Block diagram of the project
â—? 112
BBC Microbit UK 160929.indd 112
25-11-16 12:04
Chapter 5 • BBC micro:bit Projects
Figure 5.91 Circuit diagram of the project Touch Develop Program The program is called RGB and its listing is shown in Figure 5.92. In this program all three LEDs are given the same PWM signal (remember that the range of the PWM signal is 0 to 1023) and therefore the net displayed colour is R+G+B = WHITE.
Figure 5.92 Touch Develop program of the project microPython Program Figure 5.93 shows the microPython program listing of the project.
â—? 113
BBC Microbit UK 160929.indd 113
25-11-16 12:04
Chapter 6 • Saving your program and working with a mobile device
Chapter 6 • Saving your program and working with a mobile device In this chapter we shall be looking at how to save and recall a saved program, and also how to work with a mobile device such as a mobile phone. 6.1 Saving and recalling a program
After creating and testing a program successfully you may want to save it on your computer. This is done by clicking the my scripts button at the left hand side of the editor screen. You will be shown a screen similar to the one shown in Figure 6.1. You will be given a number of choices to: • Edit your file • Publish your program • Save your program on your computer • Clone your program • Remove your program
Figure 6.1 My script options Click the save button to save your program. To recall an already saved program, select the program from the left hand side of the editor screen and click the edit button. Click remove to delete the program. 6.2 Accessing BBC micro:bit using a mobile device
The BBC micro:bit can be accessed over a Bluetooth wireless link. In this section we shall see how we can access our BBC micro:bit using mobile phones such as Android (e.g. Samsung) and iOS (e.g. iPhone) and a tablet (e.g. iPad). 6.2.1 Accessing using an Android phone
The example access given here is for an Android phone, but the same steps can be used for an iPhone or a tablet.
● 149
BBC Microbit UK 160929.indd 149
25-11-16 12:04
BBC micro:bit
Before accessing the BBC micro:bit from your Android phone you should install the application micro:bit from the Play Store (or from the App Store if you are using an iPhone). After installing the apps, click to start the application. You will be presented with four options (see Figure 6.2): Connections: this is the option to choose to connect (or pair) your mobile phone (or tablet) with the BBC micro:bit Flash: you can use this option to send a .HEX (code file) file from your phone (or tablet) to your BBC micro:bit Create Code: Choosing this option you can go to a website to write code Discover: this option helps you to find out more about the BBC micro:bit and also to get ideas and examples from other people
Figure 6.2 Android micro:bit apps menu Connecting The steps to establish a link between your mobile device and the BBC micro:bit are as follows: • Plug-in your BBC micro:bit to your computer and make sure that it is powered-up
â—? 150
BBC Microbit UK 160929.indd 150
25-11-16 12:04
BBC micro:bit Dogan Ibrahim
He is the author of over 60 technical books, published by international famous publishers, such as Wiley, Butterworth, and Newnes. In addition, he is the author of over 250 technical papers, published in journals, and presented in seminars and conferences.
ISBN 978-1-907920-54-7
The device is based on the Cortex-M0 processor and it measures 4 x 5 cm. It includes several important sensors and modules such as an accelerometer, magnetometer, 25 LEDs, 2 programmable push-button switches, Bluetooth connectivity, micro USB socket, 5 ring type connectors, and a 23-pin edge connector. The device can be powered from its micro USB port by connecting it to a PC, or two external AAA type batteries can be used.
35 Touch Develop & microPython Projects
BBC micro:bit
● DOGAN IBRAHIM
Prof Dr Dogan Ibrahim is a Fellow of the Institution of Electrical Engineers.
The BBC micro:bit is a credit sized computer based on a highly popular and high performance ARM processor. The device was designed by a group of 29 partners for use in computer education in the UK and will be given free of charge to every secondary school student in the UK.
BBC micro:bit - 35 PROJECTS
35 Touch Develop & microPython Projects
This book is an introduction to the BBC micro:bit computer and it is aimed for the people who want to learn to program the BBC micro:bit computer using the Touch Develop or microPython programming languages. 35 tried and tested interesting projects are explored in the book. All the software examples for the projects are available for download. LEARN
www.elektor.com
DESIGN
Elektor International Media BV
Dogan Ibrahim LEARN
DESIGN
SHARE
SHARE
LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DES ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● GN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DE ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ●