Getting started To get started, plug your Micro:bit into an available USB port and choose your preferred programming environment depending on how confident you are with your coding ability (loosely linked to your age) ... With both these editors, you have to download the micro:bit 'hex' file and transfer it manually to your micro:bit by dragging it onto the micro:bit drive. I would spend a little time reading through the help documentation and familiarising yourself with the available commands - better to know what you don't know than not to know, no? Code with Micropython using the 'Mu' editor - best option for confident programmers. The Mu editor has all the features of the standard Python editor but it allows you to flash your script directly onto the micro:bit and also has a 'REPL' (Read, Evaluate, Print, Loop) which allows you to run code directly on the device without flashing a script onto it - great for testing!
Physical computing The BBC Micro:bit was designed primarily for physical computing - that's why it has an edge connector. For lots of the projects, you will need some physical devices to connect to your Micro:bit. If you are lucky enough to be able to buy them, these are good ones to start with ... Monk Makes Electronic Starter Kit for micro:bit (other accessories here) Kitronik Inventor Kit for BBC micro:bit (full range of accessories here) Grok Learning If you are lucky enough to have a Grok Learning subscription, there are lots of courses available for the micro:bit.
The BBC micro:bit ships with various hardware. First, let's investigate that. Display Show an icon Design an image Set / unset a pixel Animation Button presses Where we learn how to use the hardware buttons and the LED display. The micro:bit as two hardware buttons on the front - button A and button B. You can program the micro:bit to do different things when the buttons are pressed. BL / JS / PY : On the press of Button A, show a picture of a smiley face on the LED display. Use one of the built in icons or create your own if you want. BL / JS / PY : Now extend your script to show a sad face when you press Button B. BL / JS / PY : Finally, display a confused face when you press button A and button B together. (microbit-button-a-happy) (microbit-button-a-happy-b-sad) (microbit-button-a-happy-b-sad-both-confused) Pin touches Where we learn how to recognise pin touches and how to use variables. The micro:bit has 25 pins on the 'edge connector' which you can use to connect to external devices. The large pins (or pads) are useful for connecting crocodile clips to or will respond when you touch them. You will have to touch the 'GND' pin at the same time for the micro:bit to detect the touch. BL / JS / PY : Write a script which displays a number 0, 1 or 2 depending on which of the large pins / pads you touch on the edge connector of the microbit. BL / JS / PY : The number stays on the display. Extend your script so that the micro:bit clears the display after 1s. BL / JS / PY : Using three separate variables, pin_0, pin_1 and pin_2, keep track of how many touches each pin has had and display the number of touches on the display rather than the pin number you touched. (microbit-pin-touch) (microbit-pin-touch-clear) (microbit-pin-touch-variables) Thermometer Where we learn how to access the built in thermometer and how to plot graphs on the LED display. The thermometer is built into the micro:bit processor - you can touch the processor to affect the temperature reading but make sure that you have 'grounded' yourself first in case you are charged up with static. BL / PY : Design a thermometer that displays the temperature in digits on the display when you shake you micro:bit. BL / PY : Now change so that the micro:bit plots a graph of the temperature up to 50°C. The micro:bit should pause with the graph on the screen for 1s (1000ms) before clearing the screen. (You could try this with micropython but you will need to create a series of images for the graph and generate the graph manually - I don't *think* that micropython supports plotbargraph method in its led library). (microbit-temperature-on-shake) (microbit-temperature-on-shake-graph) Extension : Try outputting the temperature to the serial port so you can monitor the reading live using the simulator, REPL in Mu or a terminal window using Putty. You can either use the 'serial' blocks in the Block and Javascript editor or simply 'print' the temperature reading using micropython. Compass Where we learn how to access the compass, join strings together, use selection to make choices and stand in a field. The built in compass uses the Earths magnetic field to work out which direction the micro:bit is pointing. You will probably have to calibrate the compass before your scripts will run - follow the instructions on the screen. BL / JS / PY : Create a simple script which displays the compass heading, H, as 'Heading : H' scrolling on the display. You will have to use the 'join' block from the 'text' section. BL / JS / PY : The display doesn't really respond quick enough to changes in heading because the scrolling message locks up the micro:bit until it finishes. Adapt your script so that it displays 'N', 'S', 'E' or 'W' based on the direction (in degrees) the micro:bit is facing. (HINT : There are two situations where the micro:bit should display 'N'). You might want to introduce a slight pause after the micro:bit displays the heading to stop the display flashing. You will probably also find that the simulator works perfectly but your micro:bit goes a bit crazy. This is because of local magnetic fields - connect your battery pack and take your micro:bit out into a field to see it working properly. BL / JS / PY : Get some variables in there! You will notice that the 'compass.heading' block appears 5 times in the last script meaning that the sensor will be 'polled' 5 times during each cycle of the forever loop. Adapt your scripts so that they use a single variable, 'heading', which is set at the start of the loop and then used in the comparison statements. (microbit-compass-heading) (microbit-compass-heading-letter) (microbit-compass-heading-letter-variable) Extension : Try outputting the compass heading to the serial port so you can monitor the reading live using the simulator, REPL in Mu or a terminal window using Putty. You can either use the 'serial' blocks in the Block and Javascript editor or simply 'print' the compass heading using micropython. Accelerometer As well as registering 'gestures', the micro:bits accelerometer will also measure accurately the position of the device in three dimensions (in milligravities, mg), along the x-axis (tilt left and right), along the y-axis (tilt forward and backward) and along the z-axis (face up or face down). The limits of the values for the accelerometer in either axis are -2048mg to +2048mg, although this range can be changed. Accelerometers are used in mobile phones to detect screen orientation (portrait / landscape) and to control games by tilting the phone. BL / JS / PY : The easiest way to see this in action is to draw a graph (up to 1023) of the accelerometer reading. Create a simple script which plots a graph of the 'x' accelerometer reading. NOTE : The graphing function cannot show negative numbers so -1023 looks the same as +1023. The best way to see the actual readings is to use Putty to monitor the serial port of the micro:bit which will show the actual accelerometer reading. BL / JS / PY : Now make similar scripts which detect the movement in the 'y' and the 'z' direction. BL / JS / PY : Finally, make a script which can be used to change the axis which is being monitored using the hardware buttons - press button A to monitor 'x', button B to monitor 'y' and both button to monitor 'z'. BL / JS / PY : Create a script which monitors the accelerometer strength using a graph (up to 2048). Again, you can monitor the actual accelerometer reading using Putty. (microbit-accelerometer-x-graph) (microbit-accelerometer-y-graph) (microbit-accelerometer-z-graph) (microbit-accelerometer-x-y-z-graph-buttons) (microbit-accelerometer-strength-graph) Free-fall sensor The accelerometer can also be used to detect if the micro:bit has been dropped - the gesture is called 'freefall'. Accelerometers are built into mechanical hard drives to detect if the drive (or device containing the drive) has been dropped and park the drive head to prevent damage. The accelerometer monitors the strength of the acceleration due to gravity on the device and if it fall quickly below a threshold (about 500mg), freefall is triggered. TAKE CARE : To trigger 'freefall', you will have to drop your micro:bit. Make sure that you have a long USB cable and that there is something to catch the device like a cushion or a coat. Don't just drop your micro:bit! BL / JS / PY : Develop a simple script which allows you to detect whether the micro:bit has entered freefall. Because it's difficult to see the display, you are better outputting a message to the serial port and monitor this with Mu or Putty. BL / JS / PY : Use the join text block to add the accelerometer strength reading to the message which displays to the terminal when freefall is detected. You should see that the accelerometer strength is below 500mg when freefall is detected. (microbit-accelerometer-freefall) (microbit-accelerometer-freefall-strength) Rotation (degrees) sensor Pitch ... 0 : flat -90 : pins up facing away from me 0 : upside down +90 : pins down facing towards me 0 : flat Roll ... 0 : flat -90 : vertical facing left -179/+180/+179 : upside down +90 : vertical facing right 0 : flat [diagram of an aeroplane] Radio Send a number to all other microbits in the same radio group Send a string to all other microbits in the same radio group Send a string and a number to all other microbits in the same radio group Change radio group Set transmit power (link) Send serial (anononymous transmission) (link) Send packet to serial port (link) Magnetic force https://makecode.microbit.org/reference/input/magnetic-force
To Do : Convert all scripts to use functions where possible. To Do : Score the challenges - 1 for each block (least efficient version), 2 for forever and onstart, 2 for each gesture or button press, 10 for any sound, 5 for LED plotting, 5 for chart, 30 for any radio communication, 5 for repeat, 10 for while / for, 2 for each part of if statement, 10 for any use of variables, 2 for maths or random numbers, 20 for any appropriate use of functions (i.e. not just to structure code), 20 for arrays, 5 for any text handling, 20 for use of the game library functions, 2 for images, 30 for any use of pins, 50 for use of serial or control. 01. Message BL / PY : Display a simple, 'Hello World!', once on the Micro:bit display. BL / PY : Change your script so that the message displays forever. BL / PY : To be honest, it might be nice if you controlled what the micro:bit said - change your script so that it displays 'Hello World!' when you press button A, 'Goodbye Cruel World!' when you press button B. PY : I'd rather not wait for each message to display before I view the next one - change the micropython script to stop the micro:bit waiting for the message to finish displaying. PY : If you really fancy a challenge, try changing the script so that it speaks the messages (with the built in speech synthesiser) instead of displaying them! PY : The speech is really quiet - add a smiley face on button A press and a sad face on button B - make sure that it displays before the speech starts! (microbit-message-one-once) (microbit-message-one-forever) (microbit-message-two-on-button-press) (microbit-message-two-on-button-press-no-wait) (microbit-message-two-on-button-press-speech) (microbit-message-two-on-button-press-speech-faces) 02. I ♥ micro:bit! PY : Display a scrolling message on the screen "I ♥ micro:bit!" You will have to split up the message into three separate parts - make sure you put a pause in after displaying the ♥ or else you won't see it! Mess about with the timing of the display to make your message just right :) PY : Extend your script by making the heart flash on and off three times before displaying the rest of the message. Consider using Image.HEART_SMALL to make the animation look a little nicer. (microbit-i-heart-microbit) (microbit-i-heart-microbit-flashing-heart) 03. Rock, paper, scissors BL / PY : Make a rock paper scissors game that you can play with your friends. The micro:bit should respond to being shaken by selecting one of three images, a rock, a piece of paper or a pair of scissors. The image should clear after 2s. BL / PY : Add a countdown (3, 2, 1) after you shake the micro:bit before it chooses the random image. BL / PY : Adapt your game by including a scoring system. If you win, press button A to add 1 to your score, display a smiley face, wait 1s and clear the screen. Pressing button B will scroll your score on the screen like 'Score : x'. You can also visit this page to make a cool (?) bracelet to mount your micro:bit on your wrist. (microbit-rock-paper-scissors) (microbit-rock-paper-scissors-countdown) (microbit-rock-paper-scissors-countdown-score) 04. Cyber pet BL / PY : Write a cyber pet game. When the game starts, your pet is happy but gets more sad the longer you leave him alone. If you tickle him (button_a) or feed him (button_b), you restore him to his highest level of happiness. If you leave him untickled or unfed for more than 30 seconds, he will die. Make sure you animate the pet on the screen - you can use different faces to make your pet more lifelike. WARNING : This is hard! (microbit-cyber-pet) 05. Tilt BL / PY : Using the game library, design a script which allows you to move a 'dot' around the screen by tilting the micro:bit forwards, backwards, left or right using gestures. Using the game library makes it easier to move the dot, though you could try and do it without the game library for a challenge! BL / PY : The only issue is that you have to return the micro:bit to flat and reapply to gesture to keep the dot moving. Change your script so that dot with move as long as you hold the micro:bit in that direction and stop it moving if you return the micro:bit to 'face up'. BL / PY : Now adapt your script so that you use the accelerometer x and y position to move your dot around the screen. I've found that limits of -200 and 200 seem to work quite well. Since the accelerometer will continually register the tilt of the micro:bit, it will keep moving whilst you are tilting the device so you might have to use pauses to slow things down a bit or else your dot will constantly end up on the edges of the screen! (microbit-tilt-gestures) (microbit-tilt-gestures-automatic) (microbit-tilt-accelerometer) Special Prize! Ask your teacher to give you a copy of some instructions to make a game called 'Catch me if you can' where you tilt your micro:bit to get your dot to try to catch a flashing ghost. You've got 30 seconds to get as many points as you can! (microbit-catch-me-if-you-can-accelerometer) BL / PY : Code a dice which shows a random number between 1 and 6 on the display when you shake your micro:bit. BL / PY : Now try to code a dice which shows an image of a dice face when you shake your micro:bit. (microbit-dice-with-numbers) (microbit-dice-with-images-basic) (microbit-dice-with-images-array) 07. Smiley and sad buttons BL / PY : Create a script which displays a smiley face whilst you keep button A pressed down and a sad face whilst you keep button B pressed down. When you let go of either button, the screen clears! BL/PY : Animate your faces so that each face cycles with the 'confused' face. HINT : You will have to add a sleep(500) to the micropython script after each image or the faces will change too quickly! (microbit-smiley-sad-buttons) (microbit-smiley-sad-animated-buttons) 08. Smiley up (and down) BL / PY : Create a script which displays a smiley face on the LED display when the logo on the front of your micro:bit faces upwards (and towards you). Use gestures to do this rather than the accelerometer. BL / PY : Now add to your script so that when you turn your micro:bit upside down with the logo facing downwards (and towards you), it still shows a smiley face! To do this, you will have to create your own LED patterns for a smiley face and an upside down smiley face (i.e. not a sad face!) (microbit-smiley-up) (microbit-smiley-up-and-down) 09. Beating heart BL / PY : Draw an image of a heart on the LED display and then make the heart 'beat' by pulsing through brighter to dimmer and back again for ever. It might be better if you don't take the brightness right down to 0 so the LEDs stay lit - it'll look better. HINT : If you are trying this in micropython, firstly, it's hard and secondly, the range of brightness goes from 0 (off) to 9 (brightest). (microbit-beating-heart) 10. Spinning arrow of doom BL / PY : Produce an animation that shows all the arrows in the arrows collection one after the other for ever. You can do this by displaying each arrow in the collection one at a time manually or by looping through an array. If you are using micropython, there is already a collection built for you called Image.ALL_ARROWS! (microbit-spinning-arrow-basic) (microbit-spinning-arrow-array) 11. Keep right (straight on or left?) BL / PY : Embedded computer systems are used to control traffic signals. Produce a continuous animation which shows a south east arrow for 1s followed by a clear display for 0.5s - "Keep right!" BL / PY : Now change your script so that the default behaviour is to keep straight on (north arrow) but if you press button A, it shows keep left (south west arrow) or if you press button B, it shows keep right (south east arrow). Both buttons would set the display back to 'keep straight on'. NOTE : The micropython version of this must use the button.is_pressed() method which doesn't behave as nicely in this situation as the block version :( (microbit-keep-right) (microbit-keep-straight-left-right) 12. Smile for the camera BL / PY : Write a program which displays a sad face until you press button_a when your micro:bit smiles at you - a bit like when your friend smiles for a photo! BL / PY : If you have a speaker to connect to your micro:bit, you could add a 'BA_DING' sound when you press the button (unless you fancy making your own shutter sound!) but, the sound should only play once ... (microbit-smile-for-the-camera) (microbit-smile-for-the-camera-shutter-sound) 13. Zoo BL / PY : Get your micro:bit to display a cow when button A is pressed, a giraffe when button B is pressed, a rabbit when both button A and B are pressed and a blank screen when no buttons are pressed. If you are coding this in micropython, you have to be very careful with the order in which you check your button presses ... BL / PY : Change your script so that the micro:bit displays a blank screen when no buttons are pressed instead of a picture of a ghost. (microbit-zoo) (microbit-zoo-no-ghost) 14. Dot This challenge uses the 'game' blocks / commands. There is no game library in micropython, so, whilst it's possible to complete this challenge in micropython, you would have to code the sprite yourself. BL : Use the 'game' and variables blocks to create a sprite called 'dot' and display him on the screen at position (2,2). When button A is pressed, dot should move left (x changes by -1) and when button B is pressed, dot should move right (x changes by 1). Dot doesn't move past the edge of the display (the game blocks stop this from happening). BL : Add another event into your script so that you can move dot forward by pressing both buttons together. The only problem is that there is no way of moving him backwards, so, when he reaches the top of the display, move him back to the bottom on the next button press. This allows you to reach every position on the display. (microbit-dot-left-right-buttons) (microbit-dot-left-right-forward-buttons) Special Prize! Ask your teacher to give you a copy of some instructions to make a game called 'Catch me if you can' where you use the hardware buttons to move your dot to try to catch a flashing ghost. You've got 30 seconds to get as many points as you can! (microbit-catch-me-if-you-can-buttons) STILL TO SORT OUT ... Basic Programming
Team programming
Physical Computing (only available during code club) Hack your headphones (link) Banana Keyboard (link) Guitar (link) Ducktape wallet (link) Watch including countdown timer and digital watch (link) Soil moisture meter (link) Reaction timer (link) States of matter (link) Inchworm (link) Milk carton robot (link) Milk monster (link) Remote control car (link) Timing gates (link) Railway crossing (link) Happy birthday (link) Music (link) Random neopixel (link) Physical computing Input / output (link) Music (link) Storing files on the microbit using micropython (link)
There is an error with my code but I can't see it! All the messages that Micropython generates pipe down the serial port so you can view them by ... - if you are working on a version of Windows before Windows 10, install the Mbed serial driver; - check out 'Device Manager > Ports (COM & LPT)' and make a note of the COM port for the mbed device; - use a tool like Putty to connect a virtual terminal to that port to see your messages. - more information is available on the Microsoft MakeCode documentation. If you are using Mu to flash your programs, you can't use Putty to view the serial port messages at the same time because Mu is already using the COM port and you'll get an error message from Mu if you do. However, if you start up Putty after you've flashed using Mu and then reset the micro:bit (to re run the last flashed program), you can see the error messages in Putty. Alternatively, if you start up the REPL in Mu, the flashed program will stall (you'll see the message on the display freeze) and if you reset your micro:bit then, the error will display in the REPL as normal. Either way, you're good! Download the original program that came with the micro:bit If you want to put the original program that came with your micro:bit back, download this file and flash it onto the micro:bit. HINT : did you know there is an easter egg on this script? When you see the 'heart' animation, press A and B together to unlock a game of 'Snake'! (source) |
Learn to program >