Hummingbird and micro:bit
Now you can use the micro:bit to control your Hummingbird! You can write programs in the block-based MakeCode environment and then download them onto the micro:bit. Then you can disconnect the robot from the computer but keep running your program! To use the Hummingbird with micro:bit, you will need a Hummingbird micro:bit adapter. Because it is browser-based, MakeCode is compatible with Windows, Mac, Linux, and Chromebooks.
Getting Ready to Use Hummingbird with micro:bit
Using the Outputs
Getting Ready to Use Hummingbird with micro:bit
First, use this tutorial to switch your Hummingbird into micro:bit mode.
Next, attach the micro:bit shield to the bottom side of the Hummingbird.
Insert the micro:bit into the shield. Connect the AC power adapter to the Hummingbird board and the micro:bit USB cord to the micro:bit.
Go to makecode.microbit.org. If you need a new, empty project, go to Projects and then select New Project. Give your project a name in the text box at the bottom-middle of the screen.
Click on Advanced at the bottom of the list of MakeCode menus, and then scroll down to select Extensions.
Enter the website https://github.com/BirdBrainTechnologies/pxt-hummingbird-duo in the search bar and press enter. Then click the hummingbird package to add it.
Now you have a Hummingbird menu in MakeCode! You are ready to start writing programs that use both micro:bit and Hummingbird.
The micro:bit simulation is not compatible with the Hummingbird, so we suggest that you minimize the picture of the micro:bit to leave more room for your programs.
Programming with MakeCode
To write a program in MakeCode, you first select a menu, then a block from that menu. The block will appear in the Scripts area, and you can add it to one of your scripts. Script is just another word for a program that you write for Hummingbird and micro:bit.
A new MakeCode program for micro:bit automatically contains two items from the Basic menu. Blocks inside on start will run when you download the code to the micro:bit or turn the micro:bit on. After that, blocks inside forever will run over and over as long as the micro:bit has power.
IMPORTANT: Any MakeCode program that uses the Hummingbird must include the Start Hummingbird block from the Hummingbird menu. The Start Hummingbird block must be placed inside on start.
To download a program onto the micro:bit, click the purple Download button at the bottom of the screen. If the .hex file does not download directly onto the drive labelled MICROBIT on your computer, you will need to move the .hex file to this drive.
Using the Outputs
First, you will learn to use the lights, which are also called LEDs. The Hummingbird kit contains two types of LEDs, single color LEDs and tri-color LEDs. Single color LEDs have two wires, while tri-color LEDs have four wires.
There are also LEDs on the micro:bit itself. The surface of the micro:bit contains 25 tiny LEDs that you can use to make pictures and patterns!
Module 1: Blinking LEDs
micro:bit LED Array
In the Basic menu, there are two blocks that enable you to create simple pictures using the 25 tiny LEDs on the micro:bit. The show icon block allows you to select from a list of pictures, while the show leds block lets you pick which LEDs should be on and which should be off.
For example, this code will display a heart on the micro:bit. Here, we have chosen to place the show icon block inside on start because we only need that block to be executed once. As long as nothing else is happening with the LEDs, all we need to do is to turn on the pattern at the beginning of the program.
Exercise 1.1: Use the show icon block to display a bunny on the micro:bit. Can you use the show leds block to make a better bunny?
Single Color LEDs
A single color Hummingbird LED can be connected to one of four ports labeled “LEDS” on the Hummingbird board. Since you have four ports, you can connect up to four single color LEDs. The black LED wire should be connected to the ‘-’ terminal, while the colored wire should be connected to the ‘+’ terminal.
The Set LED block on the Hummingbird menu is used to control a single color LED.
To use this block, first select the port to which the LED is attached (one to four). Then set the brightness of the LED from 0% and 100%. 0% means that the LED is off, and 100% means that the LED is at maximum brightness. The code below will set an LED in port 1 to maximum brightness.
Exercise 1.2: Change the brightness of the LED from 100% to 0% and upload the program again. This should make the LED turn off. Try out other numbers between 0 and 100 to explore the different levels of brightness that are possible.
To blink an LED, we need the Set LED block and also a pause block, which can be found on the Basic menu. The pause block pauses the program for a certain number of milliseconds.
Add a Set LED block and a pause block to the forever block. Turn the LED on at maximum brightness for half a second.
Next, add a second Set LED and a second pause block to turn the LED off for half a second. Now your program will repeatedly turn the LED on for half a second and then turn it off for half a second. This should make the LED blink! Upload the program to give it a try.
Once you have uploaded your program, the LED should continue to blink even when you unplug the USB cord. This is because your program is actually running on the microcontroller in the micro:bit! However, the Hummingbird and micro:bit do need power, so you will still need the power adapter or a battery pack. As long as the board has power, the program will continue to run until you upload a new program to replace it.
Exercise 1.3: What happens when you remove the second pause block? Why do you need the second pause? Remember to connect the micro:bit to the computer with the USB cord every time you download a new program!
Exercise 1.4: Connect a second LED to your Hummingbird. Modify your program so that the second LED is off when the first is on and on when the first LED is off.
Exercise 1.5: Make the micro:bit LED display blink between two different pictures.
Building Tip: LEDs are quite small, but you can use a styrofoam ball to diffuse the light. This will make the light appear larger.
Module 2: Tri-Color LEDs and Events
A tri-color LED can be attached to one of the two ports labeled “TRI-COLOR” on the Hummingbird board. The four wires of the tri-color LED are red, green, blue, and black. The black wire should be connected to the ‘-’ terminal. The red wire should be connected to the ‘R’ terminal, the green to the ‘G’ terminal, and the blue to the ‘B’ terminal.
The tri-color LED actually has three tiny lights inside it. One is red, one is green, and one is blue. This is important for programming the tri-color LED with the Set Tri-LED block on the Hummingbird menu. This block requires four pieces of information, or parameters. The top drop-down menu is used to select the number of the port to which the tri-color LED is attached (One or Two). The other parameters control the amount of red, green, and blue light from 0 to 100. As an example, the block below makes a tri-color LED in port 1 blink between green and red.
Exercise 2.1: Make a tri-color LED repeat a sequence of three different colors.
Exercise 2.2: Make a tri-color LED blink on and off in your favorite color. The tri-color LED should blink two times every second. It should blink evenly - in other words, it should be off for the same amount of time that it is on.
The micro:bit has two buttons labelled ‘A’ and ‘B.’ You can write scripts that will run when one button is pressed, or when both are pressed together.
To do this, use the on button pressed block on the Input menu.
This block has a drop down menu that allows you to select button A, button B, or both (A+B). Any blocks inside the on button pressed will run each time the selected button or buttons are pushed.
For example, when the A button is pressed, this script display a smiley face on the micro:bit for 1 second. Note that to use the button more than once, we need to turn the LEDs off at the end of the script.
Exercise 2.3: Write a script that turns the tri-color LED on in your favorite color when the A button is pressed. When you press the B button, it should turn off.
Exercise 2.4: Use the A and B buttons to turn on different combinations of LEDs. When both buttons are pressed, all the lights should go out.
Module 3: Using the Motors
The Hummingbird kit works with three different kinds of motors: vibration motors, servo motors, and gear motors.
A vibration motor can be attached to one of the two ports labeled “VIBRATION” on the Hummingbird board. It does not matter which wire is attached to ‘+’ and which is attached to ‘-.’
The Set Vibration Motor block is used to control the vibration motor. This block requires two parameters. The first is the port attached to the vibration motor (One or Two) and the second is the intensity of vibration from 0% to 100%. The code below will turn on the vibration motor at half the maximum intensity when the program starts.
Exercise 3.1: Write a program that repeatedly turns both a vibration motor and a single color LED on for four seconds and then off for four seconds.
Building Tip: Small, very light items such as googly eyes can be attached to a vibration motor so that they move when the motor vibrates.
The gear motor can be attached to either of the ports labeled “MOTORS” on the Hummingbird board. It does not matter which wire is attached to ‘+’ and which is attached to ‘-.’
Important Note: Do not cut the zip ties off your motors. Without the zip tie, the wires may break off the motor.
The Set Motor block is used to control the gear motor. This block requires two parameters. The first is the port attached to the motor. The second is the motor speed. This number can be any whole number from -100 to 100.
As an example, the script below repeatedly turns the motor on for two seconds and then off for two seconds.
Exercise 3.2: Use a small screwdriver to attach a wheel to the motor so that you can see it move more easily (or just place the wheel on the motor without using a screw). Try several speeds between 0 and 100. Describe how the speed affects the motor. Try several negative speeds. What does it mean for the speed to be negative?
Important Note: You may notice that your motor rotates clockwise for positive numbers, while your neighbor's rotates counterclockwise for the same number. This just means that you and your neighbor chose '+' and '-' differently when you plugged in your motors. Either way is fine!
Exercise 3.3: Write a program that sets the speed of the motor to a random value every three seconds. You will need to use the random block from the Math menu. By default, the block generates a random number between 0 and 4, but you can change the 4 to any integer.
A servo motor is a motor that moves to a particular angle. The Hummingbird servo motor can rotate to any angle from 0° to 180°.
The servo motor has a small plug. This plug should be connected to one of the four sets of pins in the "SERVOS" section of the Hummingbird board. Each set of three pins is one servo port. The black wire should be connected to the ‘-’ pin, the red wire to the ‘+,’ and the yellow wire to the ‘S.’
Use hot glue to attach a popsicle stick to the white plastic circle on the servo motor. This will help you to see the movement of the servo motor more easily. If you don't want to use hot glue, you can tape a feather to the plastic circle.
The Set Servo block is used to move the servo motor. It requires two parameters. The first parameter is the port attached to the servo (One to Four) and the second is the angle (0° to 180°). For example, the script below will move the servo back and forth between 0° and 90°.
Exercise 3.4: Make the servo move repeatedly through a series of four different positions. Make the tri-color LED a different color at each position.
Exercise 3.5: Use the buttons to move the servo to three different positions.
Building Tip: Before you start building with a servo, always set it to 90°. This will make sure that the servo can move both left and right once it is attached to your robot.
Module 4: Loops with Variables
The main forever block in MakeCode is a loop, a programming structure that repeats actions within a program. The forever loop is great for blinking an LED, but another type of loop can be helpful to get a fading effect. This loop is called a for loop, and you can find it on the Loops menu.
The for block uses a variable. A variable is just a name that represents a value. By default, the name of the variable is “index,” though you can click the drop-down menu for an option to rename it. The parameter determines the number of times that the for loop repeats the blocks inside it.
An example program using the for block is shown below. Try out this program and see your LED repeatedly increase in brightness. You can find the red index block on the Variables menu.
The for block uses the index variable to keep track of how many times the commands inside the loop have been repeated. The logic of the loop is shown in the flowchart below. When the program reaches the for block, it sets the variable index to 0 and then checks if the value of index is less than or equal to the maximum, which is 100 here. If it is, the blocks inside the for are executed, and the value of index is increased by 1. Then the program returns to the test at the top of the loop and checks again whether index is less than or equal to 100. This process continues until that test fails. When index is not less than or equal to 100, the loop ends and the program moves on to the block below the for block. In this case, the program pauses for two seconds, and then the for loop starts all over again.
Exercise 4.1: Connect a second LED and modify your program so that one LED decreases in intensity as the other increases. You will need the subtraction block from the Math menu.
Exercise 4.2: Write a script that moves the servo slowly from 0° to 180°. For an extra challenge, make the servo move slowly from 0° to 180° and then slowly back to 0° (you will need two for loops).
Exercise 4.3: Write a program to make the tri-color LED repeatedly transition from red to blue.
Exercise 4.4: Are you ready to use all the outputs? Write a program that includes the following:
- At least three LEDs
- At least two different kinds of motors
- At least one for loop
- Explore using the blocks on the Music menu to add sound! To hear the sounds, plug your earbuds into the headphone jack on the micro:bit adapter.
Using the Inputs
The LEDs and motors are output devices. Programs send commands to these devices to make something happen. The Hummingbird sensors, on the other hand, are input devices. They send information to the program. The program can use this information to control a light or a motor or to make a decision. The micro:bit also contains sensors; for example, the micro:bit accelerometer senses when you tilt the device.
All of the sensors are attached to the Hummingbird in the same way. A sensor can be attached to any one of the four ports labeled “SENSORS.” Each sensor has a red wire, a black wire, and a yellow wire. The red wire should be connected to ‘+,’ the black to ‘-,’ and the yellow to ‘S.’
Module 5: Controlling Outputs with the Rotary Knob and the micro:bit Accelerometer
All sensors use the Get Sensor block on the Hummingbird menu. This block requires only one parameter, the port of the sensor. This block has a different shape because it measures a value and passes that value back to the program. In computer science, passing a value back to the program is called returning a value. The value of the Get Sensor block is always in the range 0-100 (arbitrary units). This value represents a voltage between 0 and 5 V.
Because the Get Sensor block returns a number between 0 and 100, you can place it inside the Set LED block to set the intensity of the light.
Exercise 5.1: Connect a rotary knob and a single color LED to the Hummingbird (port 1 for both), and then try out the script shown above. What happens as you turn the rotary knob back and forth?
Exercise 5.2: Use the knob to control a gear motor and the amount of red in a tri-color LED (the amount of green and blue can be whatever you want). These components should change at the same time.
The rotary knob is useful for controlling the LEDs because the range of the Get Sensor block (0-100) is the same as the input range for these components. If you place the knob block inside the Set Servo block as shown, the servo will only move from 0° to 100°.
You can make the servo move from 0° to 180° using arithmetic blocks from the Math menu. These blocks enable you to add, subtract, multiply, and divide numbers.
One way to use the Get Sensor block to produce an angle between 0° and 180° would be to multiple it by 1.8. Unfortunately, it is not possible to use decimal numbers with micro:bit. If you try it, you will see the error shown below.
To avoid decimal numbers, first multiple the sensor value by 18 and then divide by 10. Note that when you place one arithmetic block inside another, the operation in the innermost block will be performed first. In the expression below, the sensor block will be multiplied by 18, and then the result will be divided by 10. In math terms, each arithmetic block acts like a pair of parentheses; the expression in the script below is ((18*sensor)/10).
Exercise 5.3: Try out the script shown above and make sure that the servo can move from 0° to 180°.
Exercise 5.4: What do you think the script shown will do? Make a prediction before you try it out.
You can also use the micro:bit accelerometer to control LEDs and motors. You can measure the micro:bit’s acceleration using the acceleration block on the Input menu. The accelerometer can measure the tilt of the micro:bit in three directions, which are called x, y, and z. You can use the drop-down menu in the acceleration block to select one of these directions.
The value of the acceleration block is always between 0 and 1024. To use it to control a Hummingbird output, you need to use the division operator to reduce this value to a range within 0 to 100. For example, the script below will use the three directions of acceleration to control the three light elements of the tri-color LED.
Exercise 5.5: Try out the script shown above. How do you need to tilt the Hummingbird to change the acceleration in the x, y, and z directions?
Exercise 5.6: Use the micro:bit accelerometer to control the angle of a Hummingbird servo.
Module 6: Making Decisions with the Light Sensor
The light sensor measures the amount of light around it. Like the rotary knob, you can find its value with the Get Sensor block on the Hummingbird menu. This value will be between 0 and 100.
Exercise 6.1: Attach the light sensor to the Hummingbird board and use it to control the speed of a gear motor. What happens when you cover and uncover the light sensor? For an extra challenge, use math blocks so that the motor moves at maximum speed in the negative direction when the light sensor value is 0 and at maximum speed in the positive direction when the light sensor value is 100.
Making a Decision
You can use a sensor to enable a robot to make a decision. For example, suppose you want to turn on an LED if the light sensor detects that it is dark. The if then else block on the Logic menu is a decision block. To explain how this block works, consider this example.
The if then else block has three parts. At the top beside the if is a Boolean block, which is a block that can be either true or false. You can create a Boolean block using the comparison operators from the Logic menu.
The Boolean block in our sample program checks whether the value of the light sensor is less than 20. If this is true, the block inside the then section of the if then else is executed, and the LED turns on. If the Boolean block is false, the block inside the else section of the if then else is executed, and the LED turns off. The value that a Boolean block uses to make a decision is called the threshold. In the sample program, the threshold is 20. If the value of the light sensor is less than this threshold, the program decides that it is dark.
Exercise 6.2: Try out the sample program shown above. You may need to adjust the threshold based on the lighting in your room. If the program does not behave as you expect, try modifying the threshold.
Exercise 6.3: Write a program that blinks a red LED and moves a servo motor when a bright light is detected. When there is no bright light, the LED should be off and the servo should not move.
Exercise 6.4: The Boolean block in the if then else is checked each time the program repeats the decision. However, the Boolean block is not checked during the statements inside the then or else parts of the block. Try out this script. In this script, the motor should move in one direction when the light sensor detects that it is dark and the other direction when it is bright. When you run this program, you will notice that the program does not respond quickly to a change in the amount of light. How can you modify this program to make it respond more quickly to a change in the value of the light sensor?
Module 7: While Loops with the Distance Sensor
The Hummingbird distance sensor emits infrared waves. If there is an object near the sensor, infrared waves bounce off the object and return to the sensor. The angle of reflection is used to measure how far away the object is (more info here).
Exercise 7.1: Attach the distance sensor to the Hummingbird board and use it to control a single color LED. How does the brightness of the LED change as you move an object closer to the distance sensor?
Important Note: The distance sensor can detect objects roughly 8-100 cm from the sensor; it works best in the range of 20-60 cm.
Exercise 7.2: Write a script that turns on a vibration motor when an object is close to the distance sensor. Otherwise, the vibration motor should be off. What is an appropriate threshold for the distance sensor? You may need to experiment to find a good threshold.
An if then else uses a Boolean block to choose one of two possibilities. You can also use Boolean blocks in loops, such as the while do block (Loops menu) in MakeCode. The while do block uses a Boolean block to determine how many times the blocks inside it will be executed. The blocks inside the loop will be repeated as long as the Boolean block is true. When the Boolean block is false, the program will move on to whatever block is below the while loop.
For example, the loop below will blink a tri-color LED as long as nothing is close to the distance sensor. When the value of the sensor is greater than or equal to 20, the LED will stop blinking and turn green.
Exercise 7.3: What will happen when you run the code shown below? Make a hypothesis, and then test it.
Exercise 7.4: Create an alarm to detect people (or cats) approaching your computer! When a someone is nearby, use lights and motors to alert you to their presence. The lights and movement should continue until you turn the knob sensor clockwise. You will need to reset the knob sensor to the counterclockwise position before using the alarm again.
Exercise 7.5: Create a simple version of whack-a-mole. Use two servos as your “moles.” Each servo will correspond to one sensor: servo 1 to the distance sensor and servo 2 to the light sensor. The program should randomly select a servo and turn it to 90° to make the mole pop up. The mole should stay up until the user triggers the corresponding sensor. For example, the light sensor servo should stay at 90° until the user covers the sensor. Then the servo should move back to 0°. After the servo returns to 0°, add a random delay of 1-3 seconds before another mole pops up. After you have this basic game working, how can you add more moles? What other features can you add?
Module 8: Communicating with Radio Blocks
Important Note: For this section, you will need two micro:bits.
If you have two or more micro:bits, they can communicate with each other! This means that you can create robots that interact with one another. To do this, you will use the blocks in the Radio menu.
To send radio messages to one another, two (or more) micro:bits must be set to the same radio group. In the on start block for each micro:bit, use the radio set group block to assign both micro:bits the same radio number. Here, the radio group number is 120.
A micro:bit can send a message using the radio send string block or the radio send value block. Assume that for the script below, the Hummingbird light sensor is attached to port 1. When the light sensor detects that it is dark, the micro:bit sends the string “dark.” Otherwise, it sends the string “light.”
A second micro:bit can listen for the messages sent by the first one. If the first micro:bit is using radio send string, then the second should use the on radio received receivedString to listen for messages. When it receives a string, it can check to see which of the two possible messages it has received. The script below turns on an LED when it receives the message “dark.” The LED turns off if it receives the message “bright.”
Exercise 8.1: Try out the scripts shown above with your micro:bits. The block with the quotation marks can be found on the Text menu under Advanced. The receivedString block is on the Variables menu. If you have two micro:bits but not two Hummingbirds, just move on to the next exercise.
Exercise 8.2: Program one micro:bit to send a different radio message when each combination of its buttons (A, B, A+B) is pressed. Program a second micro:bit to respond to these messages. Each radio message should cause a servo connected to the second Hummingbird to move to a different angle.
Exercise 8.3: Use the accelerometer in one micro:bit to control the speeds of two gear motors attached to Hummingbird connected to a second micro:bit. You can use the code below as a starting point.
- The first micro:bit is sending two accelerometer values. Because it is sending two values, it calls them “X” and “Y” so the robot that receives them knows which is which.
- The second micro:bit should make the two motors move in the same direction when it receives an “X” value and in different directions when it receives a “Y” value. The on radio received block shown received both the name and the value sent by the first micro:bit.
- You can use your work to make a simple rover that is controlled by the first micro:bit! This tutorial may be helpful for building a rover.