A simple way to extend a micro:bit is by using alligator clips or banana plugs on the large pins to read (input) from or write (output) to an external component, which could be a sensor or an output.
A simple output example is powering an LED - using 2 alligator clips we can connect the 3V pin (positive) to the long leg (anode) of the LED, and connect the GND pin (negative) to the short leg of the LED (cathode):
This is simple, but the power to the LED is continuous. What if we want to make the LED blink?
Large pins to the rescue!!! We can simply move our 3V alligator clip to pins 0-3 and tell the micro:bit to send short bursts of electricity to the LED using the digital write block.
Digital, like binary, is either off or on (like a lightbulb), so when we write, we set the value to either 1 (on) or 0 (off).
Move your alligator clip that is attached to the 3V pin to Pin 0:
Start a new Project on MakeCode
Search the block drawers for "digital write" and drag that block into the Forever loop:
4. Set the value to 1:
5. In the Basic drawer, find the pause block, drag underneath the digital write block, and set the value to 500 ms (millisecond = thousandth of a second) - this tells the micro:bit how long to keep writing 1/on for:
6. Now we need to tell the micro:bit to write "off"/0 for the same amount of time. Duplicate the digital write and pause blocks by right-clicking on them individually and selecting "Duplicate"; then drag underneath the existing code and set the digital write block value to "0":
7. Look at the Simulator - you should see Pin 0 flashing off/0 and on/1. Try downloading the code to your micro:bit and testing it!
This is cool, but what if we want the LED to fade in and out, instead of this harsh off/on?
An analog signal can provide a range of values, not just 1 & 0; on the micro:bit, the value can be between 0 and 1023, so this allows us to gradually increase or decrease the amount of electricity we send to the LED.
The analog signal sent by the micro:bit is actually faux-analog 😲 - called Pulse-Width Modulation (PWM) it actually sends short bursts of 0s and 1s over a certain amount of time to fake the analogue signal! This is a common technique on digital devices.
In order to gradually increase the electricity, we will need to use a loop (iteration) and a variable (which stores data).
Delete the blocks inside your Forever loop block
From the Loops drawer, drag the "repeat x times" block into the forever loop and set it to repeat 1023 times
From the Variables drawer, drag the set index to 0 block into the on start block
From the Variables drawer, drag the change index by 1 block into the repeat block - this will change the variable from 0 to 1023 gradually
Search the block drawers for "analog write" and drag underneath the change index block (inside the repeat block)
From the Variables drawer, drag the oblong index block into the analog write block's value field:
7. Add the pause block back in and set to 1 millisecond (you'll need to type it in)
8. Duplicate the entire repeat block and it's contents and place below the first repeat block
9. In the 2nd repeat block, change the change index by block to -1 (negative 1).
10. Download and test your code!
The Love Meter project is a good example of reading external components - in this case though, the external component is a human. In this project the on pin press block is used, which is the equivalent of using a digital read block in a forever loop block.
Imagine we want to make our external LED blink when something happens, e.g. in a security alarm, where an opened window sets off a light and sound, and stop when the window is closed. In this case, the input is either off or on, ie, the window is either open or closed, so using digital read (or on pin pressed) is appropriate.
Now imagine that we want the alarm to go off when an external temperature sensor senses 40 degrees Celsius and greater or minus 10 degrees and lower - in this case we would use an analog read block to read in the temperature, as the value would be in a range.
So if the value you're wanting to read is binary (off/on, true/false, 0/1), use digital read; if the value you're reading could be in a range of values, use analog read.
You can physically extend the micro:bit with different components and accessories. These sometimes require adding a code extension to your editor.
Here are some common electronic components which can be useful:
Jumper wires - PVC coated wires with stiff metal ends for easy to connection to breadboards. They come in male-to-male (pictured), male-to-female, and female-to-female varieties.
LEDs - standard colours and RGB (Red Green Blue) LEDs (multiple colours)
Piezo-electric buzzers or speakers - use with Music blocks
Potentiometers - read-only, for varying the amount of current
Ultrasonic sensors - use sound to measure distance
DC motors - for use with a fan, or wheels, gears, lego...
Servo motors - good for robotics
If you want access to all of the micro:bit's pins, you'll need a breadboard adapter - this will allow you to prototype with a breadboard and use multiple inputs and outputs.
There are a ton of accessories for the micro:bit, from robots to breakout boards to wind and solar kits - see the Microbit.org Accessory Guide for a comprehensive list.
For some of these external accessories or components (like the servo and the ultrasonic sensor ), you'll need to add a code extension to your project. There are 2 main ways to add an extension:
Via the Cog icon
2. Via the Advanced > Extensions drawer
Many accessories also have their own extensions, which can be searched for, or added via a URL. Once added, these extensions appear as their own coding block drawer.
You can connect to Scratch and use your micro:bit to trigger and control things on Scratch, or send info to your micro:bit from Scratch - this is a great way to create interactive experiences!
Requirements: your laptop must be bluetooth-capable, and run Windows 10+ or macOS 10+
If that's you, go to scratch.mit.edu/microbit, follow the installation instructions, and try one of the tutorials there!
Oh, and they also have an awesome set of Scratch cards for micro:bit!