Ardublock: Lessons for Hummingbird Ardublock/Arduino


These modules will help you learn to write programs in Ardublock to control the Hummingbird. You may be new to the Hummingbird, or you may have already worked with the Hummingbird using Scratch, Snap!, or the CREATE Lab Visual Programmer. Ardublock is a block-based version of the Arduino programming language, which is text-based. In Ardublock, you snap together blocks that represent programming commands. These blocks are then converted to Arduino code and loaded onto your Hummingbird board via a USB cable. After the program has been loaded onto your Hummingbird, you can unplug the USB cord, and the program will continue to run!

Keys Available

Keys are available to educators.

Getting Started

Start by watching the videos shown below.

The first video will show you how to connect the Hummingbird outputs, the lights and motors that make a robot do something, and the Hummingbird inputs, the sensors that a robot can use to gather information about its environment. The second video provides a quick overview of the Ardublock programming environment.

Get The Comic!

Before you start programming in Ardublock, you will need to have the Arduino programming language and the Ardublock package installed; instructions are available here. Your Hummingbird board will need to be in Arduino mode; instructions are available here.

Preparing to Use Ardublock

Open the Arduino programming language.

You will see a window that looks something like this. This is where your Arduino code will appear when you run your Ardublock code.

Important Note: The contents of this window will be overwritten when you convert your Ardublock code to Arduino code. If you have existing Arduino programs that you do not wish to lose, create a new Arduino file (File->New), and then close the other Arduino files.

Set your board type to Hummingbird Duo by selecting Tools, then Board, and then Hummingbird Duo.

Set the Arduino’s serial port. Go to Tools->Port and select the correct port; if there are two options, it is almost never the first option.

Launch Ardublock by selecting Tools and then Ardublock.

The Ardublock window is shown below. A yellow block named loop appears automatically. This block repeats whatever is inside it as long as the Hummingbird has power. Every Ardublock program must have a loop block, and all the actions that you want the robot to repeat must be within this block.

Blocks to control the Hummingbird can be found on the Hummingbird Duo menu.

Saving Your Work

It is very important to save your work often! Otherwise, you might lose something important. To save a new program in Ardublock, click on the Save As button. Give your project a name and then click Save. Once your project has a name, you can save it by clicking the Save button.

Using the Hummingbird 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.

Module 1: LEDs

Single Color LEDs

A single color 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.

Attach a single color LED to port 1.

The first block on the Hummingbird Duo menu is the LED block. This block requires two numbers, or parameters. The top parameter is the number of the Hummingbird port to which the LED is attached (1-4). The second parameter in the LED block is the intensity of the LED. This is a number between 0 and 255. 0 means that the LED is off, and 255 means that the LED is at maximum brightness.

Drag the LED block into the loop block.

Next, click on the Upload to Arduino button in Ardublock.

You will see that the text in the Arduino window changes. Your Ardublock program has been converted to Arduino commands! For example, the LED block has become a function named setLED(). As your Ardublock program is converted, you will also be prompted to save the new Arduino program, so give it a useful name.

In the Arduino window, click on the Upload button. Monitor the status of the upload at the bottom of the Arduino window. When the program has finished uploading, it should say “Done uploading.” At this point, the LED in port 1 should be on.

Exercise 1.1

Change the intensity of the LED from 255 to 0 and upload the program again. This should make the LED turn off. Notice that you can make this change from either the Ardublock program or the Arduino program. Try out other numbers between 0 and 255 to explore the different levels of brightness that are possible.

Adding Delays

To blink an LED in Ardublock, we need the LED block and also a delay block, which can be found on the Control menu. Both delay blocks pause the program for a certain period of time. We will use the delay MILLIS block, which pauses the program for a number of milliseconds.

Add the delay MILLIS block to your program and set it to pause the program for half a second.

Next, add another LED block and another delay MILLIS 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 Hummingbird board! However, the Hummingbird does 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.2

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. Remember to connect the Hummingbird to the computer with the USB cord to upload your new program.

Building Tip

LEDs are quite small, but you can use a styrofoam ball to diffuse the light. This will make the light appear larger.

Tri-Color LEDs

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 TRI-COLOR LED block. This block requires four parameters. The top parameter is the number of the port to which the tri-color LED is attached (1 or 2). The other parameters control the amount of red, green, and blue light from 0 to 255. As an example, the block below makes a tri-color LED in port 1 turn green.

Exercise 1.3

What do you think the script below will do? Make a prediction and then try it out. How do you turn the tri-color LED off? Look at the Arduino code and examine how it relates to the Ardublock program.

Exercise 1.4

Make a tri-color LED blink on and off in your favorite color at least ten times in five seconds. The tri-color LED should blink evenly – in other words, it should be off for the same amount of time that it is on. For an extra challenge, make these changes directly from the Arduino code!

Module 2: Loops with Variables

The main loop block in Ardublock is great for blinking an LED, but another type of loop can be helpful to get a fading effect. This loop is called repeat and count, and you can find this block on the Control menu.

This block uses a variable. A variable is just a name that represents a value. By default, the name of the variable is “variable name,” which isn’t very informative. Click on the pink hexagon and change the variable name to “count.” The repeat and count block repeats the blocks in the Commands space. The times parameter determines the number of repetitions.

An example program using the repeat and count block is shown below. Try out this program and see your LED repeatedly increase in brightness. To get an additional copy of the count variable, right-click on it and select Clone.

The repeat and count block uses the 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 repeat and count block, it sets the variable count equal to 1 and then checks if the value of count is less or equal to the times parameter, which is 255 here. If it is, the blocks in the Commands space are executed, and the value of count is increased by 1. Then the program returns to the test at the top of the loop and checks again whether count is less than or equal to 255. This process continues until that test fails. When count is not less than or equal to 255, the loop ends and the program moves on to the block below the repeat and count block.

The repeat and count block shown above generates this Arduino code. The variable name count is converted to _ABVAR_1_count. This is a for loop, which is a very common structure in text-based programming. The top line says that _ABVAR_1_count starts at 1 and the loop should repeat as long as _ABVAR_1_count is less than or equal to 255. _ABVAR_1_count++ means that the _ABVAR_1_count variable should be increased by 1 each time through the loop. The commands that the loop repeats are inside the { } brackets.

Exercise 2.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 Operators menu.

Exercise 2.2

Write a program to make the tri-color LED repeatedly transition from red to blue.

Module 3: Motors

The Hummingbird kit works with three different kinds of motors: vibration motors, servo motors, and gear motors.

Vibration 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 VIBRATION block is used to control the vibration motor. This block requires two parameters. The first is the port attached to the vibration motor (1 or 2) and the second is the intensity of vibration from 0 to 255. 0 means no vibration, and 255 means maximum vibration.

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.

Servo Motors

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°.

Important Note: When using the servo motor, you must also use the AC power adapter (or a battery pack). Otherwise, the Hummingbird board will not have enough power to run the motor.

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 SERVO block is used to move the servo motor. It requires two parameters. The first parameter is the port attached to the servo (1-4) and the second is the angle (0° to 180°).

Exercise 3.2

The script below is supposed to move the servo motor repeatedly to two different angles, but there is a problem. Try out this script, and figure out how to correct it.

Exercise 3.3

Write a script that slowly moves the servo back and forth between 0° and 180°.

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.

Gear Motors

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 ‘-.’ When using the gear motor, you must also use the AC power adapter (or a battery pack).

Important Note: Do not cut the zip ties off your motors. Without the zip tie, the wires may break off the motor.

The 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 -255 to 255. As an example, the script below repeatedly turns the motor on for two seconds and then off for two seconds.

Exercise 3.4

Use a small screwdriver to attach a wheel to the motor so that you can see it move more easily (or just place it on the motor without using a screw). Try several speeds between 0 and 255. 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.5

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 Operators menu. The block shown below generates a random integer between 0 and 9. What does this block become in Arduino code?

Exercise 3.6

Are you ready to use all the Hummingbird lights and motors? Write a program that includes the following:

  • At least three LEDs
  • At least two different kinds of motors
  • At least one repeat and count loop

Using the Hummingbird Inputs

The Hummingbird 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 make a decision.

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 4: Rotary Knob

The rotary knob is a sensor that measures how much you have turned the knob. To read the value of a sensor in Ardublock, you will use the SENSOR block. 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.

Printing to the Screen

To better understand this sensor, it is helpful to display its value on the computer screen so that you can see the value change. Open a new Ardublock program. In the Communications menu, you will find a block named serial println.

Place this block inside the main Ardublock loop. Remove the block named message.

You can’t place the SENSOR block directly in the serial println block. This is because Arduino treats different types of information differently. The SENSOR block is an integer, and the serial println block is expecting a string, which is a series of letters. To convert an integer to a string, you can use the glue block, which is located on the Communications menu.

Attach a rotary knob to sensor port 1 and upload this program to the Hummingbird. To see the values that the program is printing, click on the Serial Monitor button in Ardublock.

A window will pop up on your screen. In this window, the value of the rotary knob is being printed over and over.

Exercise 4.1

Turn the rotary knob back and forth. What are the maximum and minimum values of this sensor?

Important Note: The Hummingbird can only print the sensor values to the computer screen when it is connected to the computer via the USB cable.

Take a look at the Arduino code that corresponds to your program. It includes two functions named setup() and loop(). The setup() function contains commands that are only executed once when the program begins. These commands prepare the Hummingbird and set up the serial window so that you can print to it. The commands in the loop() function correspond to the blocks in your Ardublock program. The first one prints the sensor value to the screen.


Exercise 4.2

What do you think the Serial.println() command does? Remove this line from the program to test your hypothesis.

Important Note: You must close the serial window before you can upload a new program to the Hummingbird.

Controlling Lights and Motors

You can use the rotary knob to set the brightness of an LED. Since the maximum value of the rotary knob sensor is larger than the maximum possible brightness for an LED, you need to divide the sensor value by 4 to get a number in the range 0-255.

This Arduino code corresponds to the Ardublock program above. In this case, it might be easier to type the commands than to use the blocks!

Exercise 4.3

Use the rotary knob to control the angle of the servo from 0° to 180°. What number do you need to divide the sensor value by to produce an angle in this range? For an extra challenge, try to do this directly in Arduino.

Exercise 4.4

Use the rotary knob to control the color of a tri-color LED. When the rotary knob is at its minimum, the LED should be red. When the rotary knob is at its maximum, the LED should be blue. For an extra challenge, try to do this directly in Arduino.

Module 5: Light Sensor

The light sensor measures the amount of light around it. Like the rotary knob, you can find its value with the SENSOR block. This value will be between 0 and 1023.

Exercise 5.1

Attach the light sensor to the Hummingbird board and print the value of the sensor to the screen.  Measure the amount of light in your room, and then measure the value of the light sensor when you cover it with your hand. Record these values; you will need them later.

Exercise 5.2

Use the light sensor to control the speed of a gear motor. For an extra challenge, make sure the motor moves in both directions (i.e., clockwise for some sensor values and counterclockwise for others).

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-else block on the Control menu is a decision block. To explain how this block works, consider this example.

The if-else block has three parts. At the top is the test, which is a true or false statement. To form a test, you can use the comparison operators from the Tests menu. In computer science, the test is called a Boolean statement.

The test in our sample program checks whether the value of the light sensor is less than 200. If this is true, the block inside the then section of the if-else is executed, and the LED turns on. If the test is false, the block inside the else section of the if-else is executed, and the LED turns off. The value that a test uses to make a decision is called the threshold. In the sample program, the threshold is 200. If the value of the light sensor is less than this threshold, the program decides that it is dark.

Exercise 5.3

Try out the sample program shown above. You will need to choose a threshold that is appropriate for the lighting in your room. Take the average of the two values you recorded in Exercise 5.1; this should be an appropriate threshold. As you upload the program, notice how the if-else block is converted into Arduino code.

Exercise 5.4

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 5.5

The test for the if-else block 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 6: Distance Sensor

The Hummingbird distance sensor is an infrared sensor. It 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 6.1

Attach the distance sensor to the Hummingbird board and print the value of the sensor to the screen.  How does the reading 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 6.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. How can you choose an appropriate threshold for the distance sensor?

An if-else block uses a test to choose one of two possibilities. You can also use tests (Boolean statements) in loops. The repeat and count loop repeats the blocks inside it a certain number of times. A while loop, on the other hand, uses a test to determine how many times the blocks inside it will be executed. The blocks inside the Commands part of the loop will be repeated as long as the test is true. When the test 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 the value of sensor 1 is less than 500. When the value of the sensor is greater than or equal to 500, the LED will stop blinking and turn green.

You have now practiced using all the Hummingbird motors and lights, as well as three of the sensors. There are other sensors, including a temperature sensor and a sound sensor. These work very similarly to the sensors you have already used.

Exercise 6.3

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 6.4

Create a simple version of whack-a-mole; an example is shown below. 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 1 or 2 and then turn the appropriate servo 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?

Congratulations! You have completed Hummingbird Ardublock training. Have fun making robots!