Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 2 / 86 Contents ......................... 1 Contents ..............................2 Chapter 1: MakeCode and micro:bit ....................... 5 An Introduction to MakeCode ........................5 A Brief Introduction to micro: bit ........................ 5 How to Use micro:bit ........................... 6 STEP 1: Open MakeCode ........................
Page 3
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 3 / 86 Project 3: Notification Light ........................25 Components list ............................25 Connection ..............................26 Program ............................... 26 Exercise: ............................... 30 Project 4: Electric Fan ..........................31 Components list ............................31 Connection ..............................32 Program ...............................
Page 4
Components list ............................77 Connection ..............................78 Program ............................... 78 Exercise ................................ 85 Remember to check out the DFRobot blog for more tutorials, projects and latest trends. The link can be found on the top left corner of the page.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 5 / 86 Chapter 1: MakeCode and micro:bit An Introduction to MakeCode MakeCode for micro: bit is one of the most widely used graphical programming environment from the micro: bit website. It is an open source project developed by Microsoft.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 6 / 86 The micro: bit is equipped with light and temperature sensors and other common sensing devices, which means that it itself can also produce a lot of common smart products used in our daily lives.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 7 / 86 The following steps are based on Windows 10 OS. It can be used as a reference for other operating systems. STEP 1: Open MakeCode Visit the MakeCode page from the following link: https://makecode.microbit.org/ STEP 2: Connect micro: bit to computer Micro: bit connects to computer via the USB cable.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 8 / 86 Before programming, we should make sure that the mainboard is recognized by the computer. When micro: bit is connected, a "MICROBIT" directory will show up in “My Computer”. STEP 3: Start a New Project...
Page 9
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 9 / 86 Simulation window: simulates the operating status of micro: bit. During the process of programming, you can always check how your program looks like the through the window. Function area: Where you can find all the function blocks, including input, output, loop, logic, etc..
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 10 / 86 STEP 4: Download the program and upload to micro: bit When we have finished our program, we can now download it from the website and uploaded to micro: bit. We can also rename the project and save it in the browser. The project will stay in “my stuff ”.
Page 11
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 11 / 86 Click "Download" in the lower part of the simulation window and select "Save As" in the pop-up dialog box.
Page 12
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 12 / 86 Choose to save the “.hex” file to "MICROBIT Disk" and click "Save". During the process of downloading, the power indicator on the back of micro: bit will blink. When completed, it will stop flashing and keep on going.
Page 13
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 13 / 86 Now you are all good for basic setups. Remember to visit our blog to check out more micro: bit projects. Please also leave a comment if there you have question or new idea to share.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 14 / 86 Chapter 2: What Makes a Machine “Come to Life” Interactive devices In the next few chapters, we'll build interactive projects such as a button controlled single LED, a simple colorful LED animation, or even make our own music. These devices can be categorized as "interactive devices."...
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 15 / 86 Relationship between Program and Hardware The input unit, control unit and output unit mentioned above are physical components. As for human-being, the hardware includes the body, brain and limb. However, what actually controls the behavior of our physical body is our “mind”.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 16 / 86 Chapter 3:Get hands on! You may now have a rough idea about what micro: bit and expansion board are and how MakeCode operates. You must be eager to get a hand on it by yourself. So, let's start the magical...
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 17 / 86 Connection Connect micro: bit to computer via USB cable. Program https://makecode.microbit.org/ Open MakeCode: STEP 1: Start a new project. You may go back to the last chapter if you forget how to do it.
Page 18
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 18 / 86 STEP 4: Drag the "show leds" function to the programming area and place it in "on start" When "show leds" function is inside the “on start” module, you can then click the light blue dot inside the block to edit the pattern.
Page 19
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 19 / 86 STEP 5: Here, we draw a heart in the block. To help us remember, we may rename it as "heart". STEP 6: Click "Download" to save the file to MICROBIT to the computer, or you may directly save it to micro: bit.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 20 / 86 Exercises Try to design an emoji that can switch between two different patterns.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 21 / 86 Project 2:Flashing LED Light In the previous part, we have learnt how to program the LED penal to make our own emojis. In this chapter, we will connect an external LED module and learn how to light it up and make it blink.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 22 / 86 Connection Insert micro:bit into the expansion board. Note:micro: bit will stay inside the expansion board in all following chapters. We will skip this step in all following chapters. Connect the LED module to P1 ...
Page 23
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 23 / 86 STEP 3: Put the function “digital write” into "forever" loop (the “forever” loop will show up in the area once create a new program, it can also be found in “Basic”) and the LED connected to P1 lights up.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 24 / 86 STEP 3: You will also need to keep the external LED on for 1 second and off for another second in a sequence. To do this, go to “Basic” and select “Pause (ms) (100)”. The "Pause" function keeps the same status lasting for a certain period of time.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 25 / 86 Project 3: Notification Light As you have learnt how to light an external LED through micro: bit, we will now apply an external button module to control the LED. Also, we will learn how to set the brightness of the light by using a knob.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 26 / 86 1 × USB cable Connection Connect the button module (knob module) to P0 Connect the LED module to P1 Program Task 1: button controlled lamp Goal: when the button is pressed, the LED module turns on. When the button is released, the LED turns off.
Page 27
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 27 / 86 STEP 2: The condition operator "=" under “Logic” is used to determine whether the button is pressed or not. If the status of the button equals 1, then we will know the button is being pressed, vice versa.
Page 28
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 28 / 86 STEP 4: By putting together all the functions mentioned as above, we will have the following program. What the program does is to detect the status of the button, and lights up the LED when the button is pressed.
Page 29
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 29 / 86 STEP 2: The brightness of the LED is linked to the input value of the knob. The knob is an analog input module, which means it generates a signal range from 0 - 1023, by which controls the brightness of the LED correspondingly.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 30 / 86 STEP 4:Now, all what we need is to link the input directly to the output so that the LED will be under control of the knob. Put the “analog read” function inside the “analog write” function, then put them all inside the “forever”...
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 31 / 86 Project 4: Electric Fan In this part, we will learn how to build an electric fan that runs at different speed. Before getting started, we will have a quick review of how to switch on or off the fan with the button module.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 32 / 86 1 × USB cable Connection Connect the button module (knob module in the second part) to the P0. Connect the fan module to the P1. Electric fan “eats” power! When connecting Boson modules, we should notice that due to the power consumption of modules such as fans or servos, an external power supply will be necessary.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 33 / 86 Program Task 1:Control the fan with the button module Goal: when the button is pressed, the fan starts running; when released, the fan stops. STEP 1: The button module connects to P0, which means we will need to use the “digital read”...
Page 34
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 34 / 86 condition of “1” that we preset at beginning, so the program under the statement will be executed. However, when it returns “0”, the program under the statement will be skipped. STEP 3: If the button is pressed, the fan will be turned on; otherwise it stays off. The “if-else” function under “logic”...
Page 35
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 35 / 86 Task 2: Set the fan running at different speed Goal: In this task, we will learn how to use a knob module to control the speed of the fan. When a larger analog input value the knob reaches, the faster speed the fan operates, vice versa.
Page 36
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 36 / 86 STEP 3: The fan module is now used as an analog output module, and its value is also ranged from 0-1023. The "analog write" function can be found under “Pins” STEP4:Put the “analog read” function into “analog write”, and then place all of them into the...
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 37 / 86 Chapter 4: A bit further Above we have learnt how to use the button module and knob module to control an electric fan. But there is still a long way to go, you may eager to explore more about the micro: bit. In this chapter, more sensors will be introduced to make your device smarter and more functional.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 38 / 86 1 × USB cable Connection Connect the sound sensor to P0; Connect the LED module to P1. Program To build an electronic candle, we must first know how the sound sensor works. We can connect the sound sensor to the micro: bit and see how strong our sound is by looking at the number displayed on the LED panel.
Page 39
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 39 / 86 Goal: When the analog value of sound sensor is less than 50, the on-board LED displays a number "1", indicating that the volume is low; when it is greater than 50, the on-board LED displays a number "2", indicating that the volume is high.
Page 40
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 40 / 86 STEP 3: Now, we will need to let the program determine the level of the voice. The operator "<" under the “Logic” does the job. As shown in the picture below, When the analog value of sound sensor is less than 50, the on-board LED displays a number "1", indicating that the volume is low;...
Page 41
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 41 / 86 STEP 2: However, that’s still not enough! When we blow air to the sensor, the volume will only exceed the preset value for a very short time. Sometimes it happens so fast that we won’t be able to notice the change.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 42 / 86 Project 2: Automatic Door Automatic doors can be found quite often in our daily life. When we are about to enter a convenient store, the door opens automatically and sometimes along with the sound “welcome~”.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 43 / 86 1 × servo module 1 × USB cable Connection Connect the servo to P0 Connect the button module (motion sensor in the second part) to P1 Program Task 1: Use the button module to control the servo...
Page 44
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 44 / 86 Goal: When the button is pressed, the servo rotates to 100°, and the LED penal of the micro: bit shows “O”; When released, the servo stays at the 0°, and the LED penal of the micro: bit shows “X”.
Page 45
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 45 / 86 STEP 3: By putting everything together, we will end up with a program shown as below. Need to notice that the servo has a relatively large power consumption, so we will need to connect to the power source through the external USB power port.
Page 46
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 46 / 86 Goal: we will replace the button with a motion sensor to control the servo. When someone approaches, the door opens. Otherwise it stays closed. STEP 1: Replace the button module with the motion sensor.
Page 47
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 47 / 86 STEP 4: By putting all above mentioned functions altogether, we will have the final program as below:...
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 48 / 86 Project 3: Music Box Various eye-catching music boxes in different shapes have always been popular in gift shops. You must have been deeply touched by their ornate appearances and clever designs. This lesson, We will help you in building a smart music box.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 49 / 86 1 × USB cable Connection Connect micro: bit to the computer and plug the headphone into the jack. Connect the motion sensor module to P1. Note: since P0 is occupied by the audio jack, it will no longer be available to connect other modules.
Page 50
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 50 / 86 STEP 2: Since the "dadadum" lasts for 4 beats, the “rest (ms)” function from “Music” is required for this part. The final program is as following:...
Page 51
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 51 / 86...
Page 52
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 52 / 86 volume of micro: bit. Task 2 : Smart music box Goal: We will build a smart music box in this task. When someone approaches, the micro: bit will play a piece of music of your choice; when no one is near, it stays quite..
Page 53
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 53 / 86 When above listed functions are combined altogether inside the “forever” loop, our micro:bit is able to play the first sentence of the “Twinkle, Twinkle Little Star”. But can it become even smarter? The motion sensor module will be our key again.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 54 / 86 Project 4: Colorful LED Strip After having learned how to build a smart music box, you may still want to explore more about the micro: bit. Music boxes in our daily life always wear colorful lights. Therefore, the project to make colorful LED strip will be introduced here to help you building a distinctive music box.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 55 / 86 1 × USB cable Connection Connect the RGB LED strip to P0. Program Task 1: Turn on the RGB LED strip Goal: In this part, we will learn how to use micro: bit to program the RGB LED strip.
Page 56
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 56 / 86 STEP 2: To start using the Neopixel function, we will first setup how many LEDs are included in the light strip and which pin it connects to. Will need to put these two parameter in the function block...
Page 57
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 57 / 86 Then, use the “show rainbow” function to setup light effect. This effect will only apply to LEDs that we defined in our last step. The parameter inside the function block is used to set the gradient...
Page 58
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 58 / 86 Goal: We have learned how to turn on and set light effect to the LED strip. But can we make a simple light animation, say make the LEDs turn on in a sequence? STEP 1: The key to create an animation with the LED strip is to change the parameter of the light effect.
Page 59
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 59 / 86 STEP 2: All the LEDs are starting turned off, which means the initial value of the variable "the number of LEDs to be lit" should be set to 0. Then, we will use a loop function to make the variable getting larger by 1 in each loop.
Page 60
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 60 / 86 STEP 4: To make the LED light up 1 by 1 in every 1 second, we will need to put the variable “the number of LEDs to be lit “inside the “LED range from” function, and put a “pause 1000ms” in the loop.
Page 61
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 61 / 86 STEP 2: When the value exceeds 40 and the total number of lighted LEDs is less than 7, one more LED lights up. The sound sensor will be continuously detecting the value.
Page 62
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 62 / 86 STEP 3:Put the condition “if the analog read P1 > 40” in the program that we build in the last part, we will have the final program as following:...
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 63 / 86 Chapter 5: Become an expert We have built so many funny works with different types of modules in previous chapters. You may now have a deeper understanding of the micro: bit. Is that enough for the exploration? The answer is doubtlessly NO! Hang on there and we will take another further step into the micro:bit.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 64 / 86 1 × USB cable Connection Connect the servo to P0 Program Goal: Fix the servo on the micro: bit, servo will keep pointing upright regardless how we tile board. Micro: bit detects the gesture automatically through the accelerometer. Gravitation force is also a...
Page 65
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 65 / 86 The values of the X, Y, and Z axes can be achieve from the function “acceleration” under "Input". When the mainboard lays flat on the desktop, gravitational force goes along up-and-down direction, so that the values of X and Y axis are close to zero and the Z axis is around 1000 (mm / S2), which roughly equals to the gravitational acceleration.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 66 / 86 vice versa. Since the output value of the accelerometer ranges between -2048 and 2048, and in our case, the maximum value it will reach is -500 and 500. we can make the -500 ~ 500 correspond to 0 ~ 180 degrees of the servo through the function“map”.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 68 / 86 Connection Connect the knob module to P1; Connect the RGB LED strip to P2. Program Goal: In this project, the smart music box and the LED strip be combined to realize the music performance with the LED strip when manually adjusting the rotation angle of the knob.
Page 69
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 69 / 86 STEP 1: The logic diagram below will help you to build a general idea about how to realize a DJ PANEL. STEP 2: In this step, we will learn how to edit the “if else” function. Firstly, place the “if else” from “Logic”...
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 70 / 86 STEP 3: In this step, the “and” function and the operator “<” under “Logic” will be applied to judge the input value range of P1. Functions from “NeoPixel” will be used to control the lighting effect of the GRB LED strip.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 71 / 86 Project 3: Remote Doorbell For those who have a doorbell in their homes, it may be quite a common issue that sometimes the ring is not loud enough to travel over rooms. To solve this problem, a wireless alarm may be helpful.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 72 / 86 1 × USB cable Connection We will need two micro: bit in this project, one is attached to the door bell and sends out message (transmitting end), the other receives the signal and controls the LED indicator (receiving end).
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 73 / 86 Connection diagram of micro: bit receiver: Program Task1: Program the transmitter STEP 1: We have been quite familiar with the button module and the motion sensor, however, how can we send out their status through over radio? To achieve this, we will learn how exactly the radio works in the following session.
Page 74
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 74 / 86 We need to first understand the following 3 the most commonly used functions under “Radio”: “set group”: Function description: There are a total of 255 different radio channels. The transmitter and the receiver should be set in the same group to be able to communicate.
Page 75
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 75 / 86 STEP 3: Put the function blocks together. The complete program will be looked like as following. Task 2: Program the receiver STEP 1: The receiver is able to execute different action based on the received signal. The flow chart below will help you in building the program.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 76 / 86 Exercise Add a “Please wait” notice to the doorbell when someone rings the bell.
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 77 / 86 Project 4:Escape the maze Micro:bit is trapped inside the maze with a time bomb! Poor micro:bit cannot move by itself. However, it sends out signal through the LED panel to lead the way. We need to help him escape...
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 78 / 86 1 × USB cable Connection Connect the RGB LED strip to P1. Program STEP 1: First, we will need to let micro:bit randomly generate some movements to simulate the maze. Also, micro:bit should be able to know which way it tilts. If the two directions match, 1 point will be added to the final score, which means that we are 1 step closer from escaping the maze.
Page 79
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 79 / 86 We will use number 0 ~ 3 to represent four target directions respectively. The function "pick random" under “Math” help us to get a random number from 0 ~ 3. STEP 2: Show the arrow image on the LED panel. The arrow should correspond to the randomly generated target direction we got from above.
Page 80
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 80 / 86 STEP 3: Use the “on xxx gesture” function under “input” to sense which direction the micro: bit tilts. The number inside the function loop represents four different directions.
Page 81
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 81 / 86 STEP 4: Put everything we mentioned above inside an “if-then” loop to compare if the two directions match. Also, don't forget the “forever” loop to keep the game always on going.
Page 82
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 82 / 86 We will be explaining the functions that we need to achieve it. Note: Micro: bit needs extra time to process the data, so the clock will run a bit slower than what we set.
Page 83
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 83 / 86 STEP 6: A scoring mechanism will be needed to tell us whether we made it to escape. The following flow chat may give you an idea of how it works.. Remember to reset the score at the beginning of the program.
Page 84
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 84 / 86 STEP 8: Here is how the final program looks like...
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 85 / 86 Exercise Try to make the clock tick faster to increase the difficulty; Program the LEDs to make them flash at a pace based on the remaining time; Unintuitive challenge! Set different directions in pairs and see if you can still finish the game. For...
Page 86
Welcome to the DFRobot blog: www.dfrobot.com/blog-tag-microbit.html 86 / 86 Your journey of micro: bit wouldn’t stop here. If you would like to share your ideas with us, please visit the DFRobot blog and share your idea with us! Welcome to the DFRobot blog! www.dfrobot.com/blog-tag-microbit.html...
Need help?
Do you have a question about the BOSON KIT and is the answer not in the manual?
Questions and answers