Arduino Programming

Approx. Time: 
45

The Arduino programming language is the main language used to program Arduino and Arduino-based microcontroller boards like Hummingbird Duo. The language is a simplified version of the Java programming language applied to the specific needs of creating programs for microcontrollers. It is much easier than Java to get started with, and due to the code converters built into the CREATE Lab Visual Programmer and Ardublock, it is even possible to begin by composing a program in a graphical environment as a starting point.

User Guide

  1. Using the Arduino Environment to Program Duo
    1. Installation
    2. Switching from Hummingbird Tethered Mode
    3. Uploading Programs
  2. Creating Programs
    1. Hummingbird Arduino Library Description
    2. Example Programs
  3. Switching Back to Hummingbird Tethered Mode
  4. Duo's Hummingbird and Arduino sides
    1. Using top side Hummingbird connectors
    2. Using bottom side Arduino pins and third party Arduino shields
    3. Mixing top and bottom connectors
  5. Hummingbird Arduino Library Source

1. Using the Arduino Environment to Program Duo

The Arduino environment was developed in tandem with the Arduino programming language to provide a combined editor and compiler for Arduino programs. You can also use it with the Ardublock extension to create programs in a visual environment.

1. Installation

Before you begin, follow our tutorial for installing Arduino, Ardublock, and the Arduino Hummingbird library

2. Switching from Hummingbird Tethered Mode

Watch the video below, or follow our tutorial for converting Hummingbird Duo to Arduino mode:

Videos blocked? View or download them here!

3. Uploading Programs

Once your Hummingbird is in Arduino mode, uploading is a breeze:

1. Make sure you have set your board type to Hummingbird Duo:

2. Set your serial port to the serial port of your Hummingbird Duo. Go to Tools->Port and select the correct port (if there are two options, it is almost never the first option):

3. Press the Upload button and wait for the status message to cycle between Compiling Sketch, Uploading..., and Done Uploading. You do not need to press the reset button, the software should auto-reset your Hummingbird Duo and upload the file automagically.

If uploading fails after two or three tries, try pressing the reset button as the computer is uploading the program. This button is located on the underside of the Hummingbird.


2. Creating Programs

1. Hummingbird Arduino Library

Initialization

void init()

Fully initializes the Hummingbird. By default it will set as outputs all LED and Vibration motor pins (Arduino pins 2 to 12), it will also enable intensity control for LEDs (using Timer 3) and for vibration motors (using Timer 1). This is the recommended initialization method if you use Hummingbird Duo only with top side connectors. 

void initOnlyMotorsAndServos()

Initializes just the SPI communication with Hummingbird’s integrated motor/servo controller. All regular Arduino pins except for digital I/O pin 8 are left alone. Pin 8 is the slave select line for SPI communication with the motor/servo controller and is set to output. This is the recommended initialization method if you use Hummingbird Duo with Arduino shields, even if you also connect some kit components to the top side connectors.

void init(boolean turnOnLEDFading, boolean turnOnVibrationSpeed, boolean turnOnLEDandVibrationOutputs)

Provides user control over specific initialization steps. Parameters:

  • turnOnLEDFading: Initializes software interrupt on Timer 3 that provides intensity control of the LEDs. If this is not initialized, the LED functions only turn LEDs on or off and Timer 3 is free for user control.
  • turnOnVibrationSpeed: Initializes speed control over the vibration motors, using Timer 1. If this is not initialized, the vibration motors can only be turned on or off and Timer 1 is free for user control.
  • turnOnLEDandVibrationOutputs: Sets Arduino pins 2-7 and 9-13 as outputs. 

LEDs

void setLED(byte port, byte intensity)

Sets the intensity of the LED on port 1, 2, 3, or 4. Parameters:

  • portThe LED’s port: 1, 2, 3, or 4.
  • intensityThe intensity of light from the LED, 0 is off, 255 is fully on. If LED fading is turned off, all non-zero values will result in the LED being fully on.

void setStatusLED(byte intensity)

Sets the intensity of the green status LED (also tied to ‘L’ LED on the Arduino side). Parameter:

  • intensityThe intensity of light from the LED, 0 is off, 255 is fully on. If LED fading is turned off, all non-zero values will result in the LED being fully on.

void setTriColorLED(byte port, byte red, byte green, byte blue)

Sets the color and brightness of the tri-color LED on port 1 or 2. Parameters:

  • portThe LED’s port: 1 or 2.
  • redThe intensity of red light from the LED, 0 is off, 255 is fully on. If LED fading is turned off, all non-zero values will result in the red LED being fully on. 
  • greenThe intensity of green light from the LED, 0 is off, 255 is fully on. If LED fading is turned off, all non-zero values will result in the green LED being fully on.
  • blueThe intensity of blue light from the LED, 0 is off, 255 is fully on. If LED fading is turned off, all non-zero values will result in the blue LED being fully on.

void turnOnLEDFade() and void turnOffLEDFade()

Turns on or off fading control over the LEDs.

Motors and Servos

void setMotor(byte port, int power)

Sets the power to a gear motor on port 1 or 2. Parameters:

  • portThe gear motor’s port: 1 or 2.
  • powerThe power sent to the motor, the range is -255 to 255: 0 is off, 255 is full speed in one direction, -255 is full speed in the other direction. The motor has a dead zone where it will not spin for values between approximately -30 to 30.

void setServo(byte port, byte degrees)

Sets the angle of the servo on ports 1, 2, 3 or 4. Parameters:

  • portThe servo’s port: 1, 2, 3, or 4.
  • degreesThe servo angle, specified in degrees, with a range of 0 to 180.

void setVibration(byte port, byte intensity)

Sets the intensity of the vibration motor on port 1 or 2. Parameters:

  • portThe vibration motor’s port: 1 or 2.
  • intensityThe speed of the vibration motor, 0 is off, 255 is fully on. If vibration motor speed control is turned off, all non-zero values will result in full speed for the motor.

void turnOnVibrationMotorSpeed() and void turnOffVibrationMotorSpeed()

Turns speed control on or off for vibration motors.

Sensors

float readInputVoltage()

Returns a reading in volts of the voltage of VIN. Voltages higher than 10V will read as 10V. 

int readSensorValue(byte port)

Reads the analog value at the port specified. Values can range from 0 to 1023, with 0 corresponding to 0V and 1023 corresponding to 5V. Parameter:

  • portThe sensor’s port: 1, 2, 3, or 4.

2. Example Programs

The Hummingbird Arduino library comes with ten example programs to help you get started. These can be accessed in the Arduino IDE by navigating to File->Examples->Hummingbird:

  • BlankSlate - This is a basic starter file that includes the minimum necessary code for a Hummingbird Duo program.
  • BlinkLED - Blinks the status LED and an LED on port 1.
  • FadeLED - Fades on an off the status LED and an LED on port 1.
  • KnobMotorControl - Uses a knob on sensor port 1 to control the speed and direction of a gear motor attached to motor port 1.
  • LEDThermometer - Uses a temperature sensor on port 4 to control the color of a tri-color LED on port 1.
  • SensorPrinter - Prints to serial the raw and converted values of all five standard Hummingbird Duo sensors (light, temperature, distance, sound, and the potentiometer) as well as the system voltage. Contains helper functions for converting temperature and distance.
  • SensorTrigger - Uses a sensor on port 1 to turn on or off an LED on port 1 and a vibration motor on port 1.
  • SerialHummingbirdInterface - Provides control of the Hummingbird’s outputs and reading of the Hummingbird inputs via the serial interface. View the code for the exact protocol used.
  • SimpleServo - Turns a servo attached to any of the servo ports back and forth.
  • SlowMotionServo - Slowly sweeps a servo on port 1 clockwise and counterclockwise.

3. Switching Back to Hummingbird Tethered Mode

Follow our tutorial for switching your Hummingbird Duo back into USB tethered mode, or watch the video:

Videos blocked? View or download them here!


4. Duo's Hummingbird and Arduino sides

Hummingbird Duo has connectors for components on both the top and bottom of the board. The top side, or Hummingbird connectors, are used for connecting to Hummingbird kit components like sensors, servos, LEDs, and motors. The bottom side connectors follow the standard Arduino header spacing and pinout and can be used for connecting to hundreds of Arduino shields. There are three ways to use Hummingbird as an Arduino; you can use solely kit parts and connect them to the Hummingbird top side connectors, you can use solely Arduino shields and the bottom side connectors, or you can use both.

1. Using top side Hummingbird connectors

The top side Hummingbird connectors are specifically designed to interface with Hummingbird kit parts, and the Hummingbird Arduino library described in section 3 is designed with this assumption in mind. You may also choose to interface non-kit parts to these connectors - for example, standard LEDs in the LED port, or non-kit motors/servos. Make sure to read the Hummingbird Duo datasheet for more information about using the connectors with non-kit components. 

2. Using bottom side Arduino connectors

The bottom side Arduino connector pinout is identical to that found on the Arduino Leonardo and is compatible with all Leonardo-compatible shields. There are two important considerations to using Hummingbird exactly as an Arduino Leonardo:

Integrated Motor/Servo Shield: The gear motors and servos are controlled by a secondary processor that is controlled by the main microcontroller via SPI. SPI can be shared between multiple shields/devices, but we needed to use one I/O pin as the "slave select" line. We are using Arduino pin 8 for this function. If you wish to use the integrated motor/servo capabilities of the Duo, rewire any shields that use pin 8 to an alternative I/O pin.   

Battery Voltage Detect: The Hummingbird Duo has an integrated battery voltage monitor that makes use of analog input pin A5. Use another analog port if you wish to retain this feature. You can use A5 as a traditional analog input: an external analog signal will safely override the battery voltage signal.

3. Mixing top and bottom side connectors

It is quite possible to use Arduino shiels and top side connectors simultaneously, but care must be taken to ensure that signals that map to both the top and bottom connectors are appropriately programmed and connected. It is good practice to connect only one component to each signal; for example, if you have an Arduino shield that makes use of Arduino I/O pin 6, you should not also connect the green element of the tri-color LED on port 2. Observe the following tables when mixing top side components with Arduino shields:

Digital I/O

Arduino Pin Hummingbird Use
0 Not used
1 Not used
2 Controls LED port 2
3 Controls LED port 1
4 Controls RGB port 1, green element
5 Controls RGB port 2, red element
6 Controls RGB port 2, green element
7 Controls RGB port 1, red element
8 Controls SPI slave select for motor/servo controller.
9 Controls Vibration motor 1
10 Controls Vibration motor 2
11 Controls RGB port 2, blue element
12 Controls RGB port 1, blue element
13 Controls Status LED on top side, ‘L’ LED on bottom
SDA Controls LED port 2
SCL Controls LED port 1

Analog In

Arduino Pin Hummingbird Use
0 Controls LED port 4
1 Sensor 1 input
2 Sensor 2 input
3 Sensor 3 input
4 Sensor 4 input
5 Battery voltage monitor

 

SPI

Arduino Pin Hummingbird Use
MOSI MOSI to motor/servo controller, can be used for additional modules
MISO Not used
SCK SCK to motor/servo controller, can be used for additional modules

 


 5. Source Code

The source code for the Hummingbird Arduino library is available on Github, and is also installed in the Arduino Sketchbook folder at libraries->Hummingbird.