Lesson 1 – Single Color LEDs

Hummingbird Components

3 Single Color LEDs

Java Concepts

Creating a new program, declaring an object, using object methods

Teacher Materials

Get Access

The Hummingbird Robotics Kit enables you to create your own robots with lights, motors, and sensors connected to the Hummingbird board. In these lessons, you will learn how to write programs in Java to control the different Hummingbird components. Before you start, make sure that you have installed Java and set up the Hummingbird. You can also explore the different parts of the Hummingbird kit here.

In this lesson, you will write programs to turn single color LEDs on and off. A single color LED is a small light with two wires. The colored wire shows the color of the LED. The Hummingbird kit comes with red, green, and yellow LEDs.

If you want to complete this lesson with a robot, try building an animal or a jitterbug.

Use the terminal tool to plug three single color LEDs into LEDS port 1, 2, and 3 on the Bit. The colored wire should be connected to ‘+’ and the black to ‘-.’

Connect to the Hummingbird in the BlueBird Connector, and then open a new file in Java named HummingbirdSingleColorLED.java. Make sure this file is inside the Java project that includes the classes for the Hummingbird and micro:bit (Robot.java, Hummingbird.java, and Microbit.java). Create a main() function inside your new file.

To use the Hummingbird in Java, you must declare an object that represents the Hummingbird. This object is an instance of the Hummingbird class, which contains the methods that you will use to write programs with the Hummingbird’s lights, motors, and sensors. This line of code declares your Hummingbird by giving it a type (Hummingbird) and a name (bird). The bird object is then initialized to a new Hummingbird object.

The “A” in parentheses tells the program that your Hummingbird is device A in the BlueBird Connector.

You can use the // sign or /* and */ to add comments to your program. Comments do not affect how your program works, but they make it easier for other people to use your code (and for you to remember what it does!).

Once you have declared a Hummingbird object, you can use the lights, motors, and sensors by calling methods on that object. Methods are all the different functions that let you do things with your Hummingbird. For example, you can use the setLED() method to turn a single color LED on or off. This method requires two parameters. The first parameter is the number of the Hummingbird port to which the LED is attached. This will be a number between 1 and 3. The second parameter is the intensity, or brightness, of the LED from 0 to 100. 0 means that the LED is off, and 100 means that the LED is at maximum brightness.

The simplest way to use an LED is to turn it on, pause the program, and then turn it off. An example program is shown below. The pause() method pauses the program; it takes a single parameter that is a number of seconds. The stopAll() method turns off all the lights and motors of the Hummingbird. You should get in the habit of calling this method and disconnect() at the end of every program.

Exercise 1

Try out the sample code shown above. It sets LED 1 to maximum brightness. Try out other intensity values between 0 and 100 to explore the different levels of brightness that are possible.

Exercise 2

Modify your code to make the LED stay on for five seconds.

You have attached three LEDs to the Hummingbird board. To use LED 2, call the setLED() method with the first parameter set to 2. The code below turns on LED 1 for one second, then turns it off and turns on LED 2 for one second.

Exercise 3

Try out the program above. Then modify it to include all three LEDs.

Exercise 4

Write a program that turns on all three LEDs at intensity 100 for two seconds, then intensity 75 for two seconds, then intensity 50 for two seconds, and then intensity 25 for two seconds.

Back to Top