1

Introduction

The micro:bit (Figure 1-1) is a small uncased circuit board with a display made up of 25 LEDs, a couple of buttons, and some sensors. Crucially, it has a micro-USB socket that allows you to connect it to your computer both to power it and to send programs to it. The micro:bit can also be connected to a battery pack so that it can be used without your computer.

Images

Figure 1-1   The micro:bit: (A) front; (B) back.

The micro:bit was designed for use in education, but this useful little device has also endeared itself to electronics hobbyists and makers around the world.

Plug Me In!

To use your micro:bit, you will need a USB to micro-USB lead to connect it to your computer. You may have bought one at the same time as you bought your micro:bit. But if not, don’t worry because this lead is probably the most common USB lead in existence. The micro-USB connector on the end that plugs into the micro:bit is the same plug as is used on most non-Apple cellphones and countless other electronics devices. You will occasionally come across USB leads that are “charge only.” This means that they do not have the necessary wires inside to provide a data connection for the micro:bit. So, if you have problems when it comes to connecting to your micro:bit, try changing the lead for one that claims to be a “data” lead.

Figure 1-2 shows a micro:bit attached to a laptop. This laptop happens to be an Apple, but the micro:bit can be used with a Windows, Linux, Apple, or even Raspberry Pi computer.

Images

Figure 1-2   Connecting your micro:bit to your computer.

Plug your new micro:bit in, and a little animation will start that displays a welcome “Hello” message and then goes on to point out the two A and B buttons built into the board. Take some time to familiarize yourself with the board, and follow the prompts on the scrolling display.

If you want to start again, just press the Reset button (see Figure 1-1B) just to the right of the the USB connector.

History

The micro:bit is more correctly called the BBC micro:bit. The BBC (British Broadcasting Corporation) is the United Kingdom’s largest public-service broadcaster. The BBC micro:bit project was designed to provide an easy-to-use platform to teach children how to code while realizing that coding can also be used to control electronics and not just make things happen on screens.

As part of this initiative, in 2016, around a million micro:bits were given out free of charge to every school child in UK school year 7 (11- or 12-year-olds) in the UK public school system. Since then, the running of the micro:bit project has passed from the BBC to the Microbit Educational Foundation. This not-for-profit organization is now spreading the use of the micro:bit to educational communities around the world and also making this handy little device available to electronics hobbyists and makers.

What Can It Do?

When you plug in your micro:bit, it will give you a quick rundown of its own features. Let’s go through these features in a little more detail.

Images   LED display. This display is made up of 25 LEDs arranged in a 5 × 5 grid. You can control the brightness of any of these LEDs separately and display text messages on the screen that scroll from right to left.

Images   Push buttons A and B. You can write programs that will perform certain actions (perhaps display a message) when one of these buttons is pressed.

Images   Touch pins. The connectors marked 0, 1, and 2 (at the bottom of Figure 1-1A) can be used as touch switches, so by touching them you can trigger actions in your programs. You can also use alligator clips to attach these connectors to objects so that your programs can detect you touching those objects. Figure 1-3 shows a micro:bit attached to a banana!

Images

Figure 1-3   micro:bit and fruit.

Images   Accelerometer. An accelerometer measures acceleration—that is, the rate of change of speed. It does this by measuring the forces acting on a little weight built into a chip. There is such a chip on the micro:bit, and it allows you to do such things as detect the orientation of the micro:bit (like a Wii controller) or whether it is being shaken or just moved slightly, opening up all sorts of project opportunities.

Images   Compass. The micro:bit also has a built-in electronic compass that lets you detect the direction in which it is facing or detect whether you place a magnet near it.

Images   Radio. The micro:bit has a radio transmitter/receiver that can be used to send messages from one micro:bit to another.

micro:bit versus Raspberry Pi and Arduino

Keeping up with new boards such as the micro:bit can be a daunting task. It’s not always immediately obvious what the differences are and which you should use. Figure 1-4 shows perhaps the three most popular microcontroller and single-board computers: the micro:bit, Arduino Uno, and Raspberry Pi. The features of these boards are summarized in Table 1-1.

Images

Figure 1-4   (From left to right) micro:bit, Arduino Uno, and Raspberry Pi 3.

Table 1-1   Comparing the micro:bit with an Arduino Uno and Raspberry Pi

Images

As you can see from this table, the micro:bit actually represents a pretty good value compared with the Arduino Uno. Where the Arduino scores is in its connectors, which allow wires to be plugged in directly, as well as a vast range of plug-in “shield” circuit boards that sit on top of the Arduino, providing extra features.

The Raspberry Pi 3 is really a very different beast, actually being a fully fledged computer to which you can attach a keyboard, mouse, and monitor. However, unlike most computers, the Raspberry Pi has general-purpose “pins” to which you can attach external electronics just like micro:bit and the Arduino.

If you look around your home, you are quite likely to find a range of devices that contain the equivalents of a micro:bit, Arduino, or Raspberry Pi. For example, your TV remote control contains a microcontroller (like the “brain” of a micro:bit or Arduino) that has pins to which the remote control’s keys are connected and an infrared LED that will send out a series of pulses that transmit a code to the receiver on the TV.

A smartphone or media center will have something very similar to the processing part of a Raspberry Pi at its core. This will be a much more powerful device than the micro:bit or Arduino because it will need to be capable of generating video signals and also running an operating system that can do more than one thing at a time.

So What Is Programming?

As we have already established, many of the electronics goodies that you buy contain a microcontroller like the micro:bit’s microcontroller. This is a tiny computer designed to monitor buttons and switches as inputs and control outputs. You will find microcontrollers in car radios, kitchen timers, TV remotes, and even disposable greetings cards that play a tune when you open them. Pretty much anything that has push buttons and some kind of display will also have a microcontroller in it.

Without programming, a microcontroller doesn’t do anything. It will just sit there idly until someone uploads a program to it telling it what to do. Telling a microcontroller what to do involves writing a computer program (called programming or coding) and then installing the program on the microcontroller.

If you look at your TV remote, it’s unlikely to have a USB port through which to program it. In cases such as this, where a large number of devices are made, special programming hardware is used to program the device while it is being manufactured. Once programmed, the device probably will never have another program installed on it. This is quite different from the micro:bit, which has a built-in programming interface that allows you to install a new program hundreds of thousands of times if you want. Figure 1-5 shows how the whole programming process works on a micro:bit when using MicroPython.

Images

Figure 1-5   Programming a micro:bit.

The process starts with your program written in the Python programming language using an editor program running on your computer. When you are ready to transfer the program to the micro:bit, the editor program first converts the text version of the program along with MicroPython itself into a file in a format called hex (short for hexadecimal). This file is then transferred to the micro:bit (uploaded or flashed) simply by copying the hex file from your computer’s disk drive to the micro:bit’s virtual USB device. This is made possible by a neat trick of the micro:bit’s to look like a data storage device to your computer. The uploaded file is then processed by the micro:bit into a binary form that it can run (called compiling). Once the process is complete, the program will run automatically.

Why MicroPython?

There are many computer languages with which you can program your micro:bit. The two most popular and the two being promoted by the Microbit Educational Foundation are MicroPython and JavaScript Blocks. JavaScript Blocks uses a graphical-type interface to plug together code blocks to put your program together, whereas MicroPython is a more conventional free text language that is completely text based. Chapter 12 provides an introduction to JavaScript Blocks.

Both languages have their pros and cons, but this book concentrates on MicroPython, which is an implementation of Python created by Damien George specifically for use on low-power microcontrollers such as the micro:bit. Python is a popular language in educational settings because it is considered to be one of the easiest programming languages to learn that is also well used in the software industry.

Summary

In this chapter, you have plugged your micro:bit into your computer and learned a little about this small device and what it can be used for. In Chapter 2, you will start to program your micro:bit.