We show you how to interface your Scratch 3.0 digital projects with the physical world using micro:bit.
If you haven’t used Scratch before, Scratch is a fantastic way to teach children as young as 8 how to code. The intuitive coding system can be used online or offline on a desktop computer or tablet, and uses colourful coding blocks that kids simply drag-and-drop to create a program.
With the recent release of Scratch 3.0, kids can now combine the magic of the digital world and the physical world. Scratch can connect to the computer’s camera, a Makey Makey, micro:bit, LEGO Mindstorms EV3, the LEGO WeDo 2.0, and more.
Let’s show you how easy it is to make a program in Scratch 3.0 and have it controlled using a micro:bit.
The micro:bit is a handheld programmable microcomputer designed by the BBC to help kids learn to code and create with technology.
There is certainly a lot packed into the circuit board, that measures only 52 x 43mm. Let’s talk you through all of the features:
2 buttons: Programmable momentary action push buttons.
25 LED lights: Can be individually programmed to show shapes, text or numbers.
USB connector: Connect to a computer for power or to load programs onto the micro:bit.
Accelerometer: Senses if the micro:bit is being moved, tilted, shaken, or in free-fall, and at what acceleration.
Compass: Detects which direction the micro:bit is facing.
Processor: Where your program is stored and executed.
Radio: Communicate with other micro:bits for multiplayer games, etc.
Bluetooth antenna: Wirelessly sends and receives signals to Bluetooth enabled PCs, Smartphones, or Tablets.
Reset button: Restarts the micro:bit
Battery socket: Power the micro:bit using batteries.
Temperature sensor: Detects the current temperature of the micro:bit in degrees Celcius.
Light sensor: The LEDs on the micro:bit can also act as a light sensor to detect ambient light.
Edge Connector: 25 external connectors, called Pins, on the edge of the micro:bit allow you to connect to other electronics hardware, including LEDs, motors, and other sensors. These can behave as inputs or outputs.
Note: At the time of writing this article, Scratch 3.0 doesn’t have code blocks to use the compass, thermometer, or light sensor. Only 3 of the 25 pins are used too.
You can purchase a micro:bit from our friends at Altronics and Core Electronics, or from other electronics retailers.
To learn more about the micro:bit visit: https://microbit.org
SCRATCH 3.0 OVERVIEW
Scratch 3.0 was released at the start of 2019 by the brilliant minds at MIT. It is free to use, and can be used directly on the Scratch website https://scratch.mit.edu, or downloaded to a desktop computer for use when the internet is not available. Scratch 3.0 is also Tablet friendly, so young kids can use their fingers on the touchscreen instead of having to guide a computer mouse.
Programming using Scratch is very easy. It uses colour-coded blocks that kids simply drag-and-drop on the screen. The blocks snap together like a jigsaw puzzle to create a set of instructions, also referred to as a script.
We’ve shown you a small script in the image above. Can you work out what it does?
Answer: When you press the ‘space’ key on the keyboard the Cat will go to the middle of the screen. For 5 times, the cat will make a “Meow” sound, move to the right 10 pixels, say “Meow!” on the screen, and wait for 1 second each time.
Many Scratch programs use a Sprite graphic, like our orange cat. There are hundreds of pre-designed Sprites to choose from, or you can design your own using the editor. Some Sprites have Costumes that you can toggle between to show movement or similar animations.
To get your Sprite to perform something, you simply drag the Code blocks to the Script area and use an Event block to start your script. There are blocks designed to move or change the appearance of the Sprite or background, and blocks to make sounds or to show text on the screen. There are even blocks for doing mathematics and to manage data variables.
There is a lot more to do than what you see on the default Scratch interface. By pressing the blue coloured Add Extension button at the bottom left, you can add much more functionality. This is where we find the micro:bit extension, which we will explain below.
There are plenty of tutorials within the Scratch interface to learn more, and the Scratch community is also a great place for inspiration.
If you are familiar with Scratch 2.0 but want to know about all of the new features in Scratch 3.0, read our ‘Scratch 3.0: We Have Lift Off’ article in Issue 18.
HOW TO CONNECT YOUR MICRO:BIT TO SCRATCH 3.0
PREPARE YOUR DEVICE
Connecting our physical world to our digital world will require a few things in order for Scratch and micro:bit to talk together. Scratch has made it quite straightforward, and have a dedicated web page with the necessary links and instructions: https://scratch.mit.edu/microbit
In summary, you need Bluetooth enabled and a small program called Scratch Link installed on your computer or Tablet, and also a small program loaded onto the micro:bit. Let’s go through this step-by-step:
Firstly, you will need to use a computer or Tablet running Windows 10 (version 1709+) or macOS (version 10.13+). It will need to have Bluetooth enabled.
Next, you will need to download and install a small program called Scratch Link. This allows you to control peripheral devices, such as the micro:bit.
Windows: If you are using a Windows tablet you can download from the Windows Store, or from the following link if you are using a PC: https://downloads.scratch.mit.edu/link/windows.zip
macOS: If you are running macOS you can download on the App Store or from the following link: https://downloads.scratch.mit.edu/link/mac.zip
Follow the installation wizard instructions. Once installed, you need to start the Scratch Link program. An icon in the toolbar to show that it is running.
Next, we need to install a program onto the micro:bit.
Step 1: Connect your micro:bit to your computer or Tablet with a USB cable.
Step 2: Download the Scratch micro:bit HEX file: https://downloads.scratch.mit.edu/microbit/scratch-microbit-1.1.0.hex.zip
Step 3: Within the ZIP file is the ‘scratch-microbit-1.1.0.hex’ file. Drag and drop (or copy and paste) that .hex file onto the micro:bit
Keep the micro:bit powered, by keeping it connected to your computer or using a battery pack if you have one.
ADD THE MICRO:BIT EXTENSION
The code blocks for micro:bit are not visible on the default Scratch 3.0 editor. You need to add this micro:bit extension by following these steps:
Step 1. Press the Add Extension icon in the lower left corner of the Scratch screen.
Step 2. Select the micro:bit extension.
An error message may appear if you do not have Bluetooth turned on or the Scratch Link program running.
Step 3. Providing your Scratch Link program is running and your Bluetooth enabled, you will see your micro:bit device listed in a pop-up window. Press the ‘Connect’ button, then press the ‘Go To Editor’ button once that appears.
The following code blocks will appear for you to choose from in order to interact with your connected micro:bit
Triggers a script when the A, B, or either button on the micro:bit is pressed.
Trigger an event when the micro:bit is moved, shaken or jumped.
Control the LEDs on the micro:bit to display a pattern, or to show letters or numbers. The characters will scroll if more than one character.
Control your program when you tilt the micro:bit front, back, left, right or any of those directions.
Trigger scripts when pin 0, 1 or 2 on the microbit is connected.
CODING THE MICRO:BIT USING SCRATCH
We can now create a program in Scratch to work with your micro:bit. We’ll start with some simple examples. and then finish off with a fun game that uses the Tilt feature on the micro:bit.
Let’s first show you how to program the buttons on the micro:bit. Add the code blocks to the script area as shown here. Make sure you match the same settings for the ‘A’ or ‘B’ button and the text that will display.
Press the buttons on the micro:bit and you should see the red LEDs on the micro:bit show an A or B that matches the button you are pressing. Well done!
Let’s show you a script example of the accelerometer that is built inside the micro:bit. This will demonstrate the Tilt function.
Add the code blocks to the script area, as shown in the above example. One will need to be set to ‘left’ and the other to ‘right’. You then need to create the arrow pattern, as shown in the example.
Now when you tilt your micro:bit left the LED display on the micro:bit should show an arrow to the left. Likewise, when you tilt to the right, the arrow should point to the right.
Let’s make our micro:bit control the cat Sprite on the Stage, which will show you how to control your program using the action on the micro:bit.
Add the code blocks to the script area, as shown in the above example. Pay close attention to ensure you use the correct blocks and settings.
If you have everything correct, the cat on the Stage should move left, right, up and down when you tilt the micro:bit. The cat should say the direction it is going. When you press the micro:bit’s A button the cat should skip 10 times. When you press the B button the cat should return to the middle of the stage and make a Meow sound (Make sure your volume is turned up).
CAT AND MOUSE
This fun game was inspired by Max Wainewright’s Scratch ‘Cat and Mouse’ Game, but adapted for use with the micro:bit.
In this game, the mice move down from the top of the screen, trying to reach the bottom. You use the micro:bit tilt function to move the cat left and right on the screen to try to catch the mice.
There is a 30 second timer and a scoreboard to score how many mice escape and how many you catch.
You can download this Cat and Mouse program from the Resources section below (You must be signed in), which will save you having to replicate what we've shown here.
Add the 'Cat 2' Sprite and add the following.
Add the 'Mouse 1' Sprite and add the following.
WHERE TO FROM HERE?
Scratch programs are virtually limited by your imagination. Keep experimenting with all of the micro:bit Scratch code blocks.
The Scratch community will continue to share their projects, which you can adapt to your needs.
Core Electronics and Altronics sell accessories that connect to the micro:bit, such as breakout boards, lights, relays, speakers and more.