Lesson 1 -Microbits: A Refresher

Learning Outcomes

Objectives:

Must

Understand what microcontrollers are, and why they’re used.

Should

Remember how to set up and use a microbit

Could

Re-familiarise ourselves with the micro:python interface

Keywords

Words to learn for this lesson are: Micro-controller, Arduino, ARM, Maker’s revolution

Starter

Look at some of this video:

All the tech in this video uses a tiny open-source programmable computer called an Arduino.  These are all simple, home-made projects using affordable, easily sourced components.  Many of these projects (the Rubik’s cube solver, the robot arm, the 3D printer) have been made in after-school clubs here.    The point is, we are living in a time when we have the tools and means to make pretty much anything we can imagine…

3D printing and laser-cutting give us the opportunity to create any component we can think of, whilst programmable micro controllers mean we can add sophisticated sensors and input/output to our creations.  This has given rise to the Maker Movement – we no longer need factories to create products.  We make our own!!

Main

So, the Arduino you saw in the video contains an Atmega 328 ARM chip.  This is essentially an entire computer (RAM, Microprocessor and ROM) squeezed onto one chip.  The rest of the circuit board basically gives us access to input/output on this computer.

The BBC micro:bit you used in year 7 essentially uses similar technology, but has some simple sensors for input, and basic output built in.

Task: Research and explain the input/output features of the BBC micro:bit, labelling a diagram in your books.  This page should help.

Now a refresher of how to use your micro:bit:

OK, let’s get started.  You will be given the BBC Microbit device, a short USB cable and an (empty) battery pack.  The Microbit is reasonably tough, but please be careful when you are handling it – try not to drop it and be careful when plugging or unplugging cables.

Plug the small end of the USB cable into the USB socket on the top of the Microbit – it will only go in one way so don’t force it, check you’ve got it the right way round!

plugging_in1 plugging_in2

Plug the large end of the USB cable into a USB socket on your computer – your teacher will show you where this is.

connect_m8_computers carol_alison_usb adams_usb

When you plug it in the yellow light next to the USB power will light up to tell you the Microbit has power.  You may also see the red LEDs on the back light up as it runs a demo program.

Click here to open the MicroPython editor

Starter
Open the MicroPython editor by clicking the link above.  This will load a demo program which looks like this:

micropython1

Your teacher will show you how to flash this program to your micro:bit

You started to look at this in year 7,  but a quick refresher:

So what does all of the code written in Python mean mean?  Let’s go through it line by line:

from microbit import *

This line tells Python that it has to use the microbit library.  A library is a set of add-in instructions that allows Python to communicate properly with the microbit.  The programmer doesn’t need to do anything complicated.  You must always have this line at the top of any Python code you want to run on the Microbit.

while True:

This is the Python equivalent of the forever block in Blockly.  This means that whatever comes after it (and is indented) will repeat forever.

display.scroll('Hello, World!')

This tells the Microbit to scroll the message enclosed in apostrophes.  In this case it will scroll the message Hello, World! across your screen.

display.show(Image.HEART)

This tells the Microbit to display a built-in image.  Python has lots of built-in images – the list is below:

Image.HEART
Image.HEART_SMALL
Image.HAPPY
Image.SMILE
Image.SAD
Image.CONFUSED
Image.ANGRY
Image.ASLEEP
Image.SURPRISED
Image.SILLY
Image.FABULOUS
Image.MEH
Image.YES
Image.NO
Image.CLOCK12, Image.CLOCK11, Image.CLOCK10, Image.CLOCK9, Image.CLOCK8, Image.CLOCK7,Image.CLOCK6, Image.CLOCK5, Image.CLOCK4, Image.CLOCK3, Image.CLOCK2, Image.CLOCK1
Image.ARROW_N, Image.ARROW_NE, Image.ARROW_E, Image.ARROW_SE, Image.ARROW_S, Image.ARROW_SW,Image.ARROW_W, Image.ARROW_NW
Image.TRIANGLE
Image.TRIANGLE_LEFT
Image.CHESSBOARD
Image.DIAMOND
Image.DIAMOND_SMALL
Image.SQUARE
Image.SQUARE_SMALL
Image.RABBIT
Image.COW
Image.MUSIC_CROTCHET
Image.MUSIC_QUAVER
Image.MUSIC_QUAVERS
Image.PITCHFORK
Image.XMAS
Image.TARGET
Image.TSHIRT
Image.ROLLERSKATE
Image.DUCK
Image.HOUSE
Image.TORTOISE
Image.BUTTERFLY
Image.STICKFIGURE
Image.GHOST
Image.SWORD
Image.GIRAFFE
Image.SKULL
Image.UMBRELLA
Image.SNAKE

sleep(2000)

This tells the Microbit to wait for 2000 milliseconds.  2000 milliseconds are 2 seconds.  What would happen if this line wasn’t in the program?

In the MicroPython editor, click the Download button and transfer the .hex file to your Microbit to run the program.  See if it works.  Try changing the code – for example try changing the image it displays or the text that is displayed.

Plenary

Write some code that will display 4 different animals,  one after the other.

How about the name of the animal, followed by a picture?

Screenshot your code and add it to your book.

Homework