Programming Robots Study Group – Lab 2

Wikis > Robotics > Programming Robots Study Group – Lab 2


You’ll want to solder the pins on the Teensy long edges. Each side will use 12 pins. Be careful to keep the pin insulation flush against the bottom of the board for the best fit. You may want to insert the pins into a solderless breadboard to hold the pin-strips in place.

You will also need a laptop with the Arduino IDE (integrated development environment). If you are using the Teensy, you’ll need the Teensy specific version. Most exercises should work on any Arduino.

What is the Arduino?

Arduino is an open source microcontroller platform released in 2005. It’s been quite successful in the market as a hobbyist microcontroller with more than a million official and derivative boards already sold. The market is still increasing.

Some important links

Why use the Arduino?

We are using the Raspberry Pi as our reference main processor. Why wouldn’t we use that exclusively? Several reasons:

  • The Pi isn’t real time, so devices like wheel encoders don’t work well.
  • It’s a common and simple way to extend the power of the robot with lots of reference code and electronics.
  • It’s simpler to program and easy to prototype hardware.
  • We don’t have an image yet for Pi 2 including ROS.

Having said all that, the ROS sessions apply to the Pi. Thus, the ROS overview, command line as well as node coding all apply to Pi. It’s common to mix a smarter processor with lower power dedicated processors.

Why are we alternating the 2 processors and 2 approaches?

  • We are taking a hands on approach with the Arduino and electronics. The simple and visceral building of electronics is juxtaposed to the cerebral ROS node coding.
  • You’re always on a battery budget with untethered devices. Arduino fills that need.
  • Arduino is a small, fast enough, no operating system machine suitable for real time.
  • Arduinos work well for sensor processing. In future revisions we’re likely to use several.
  • Satellite Arduinos can send data to the Pi, which while it’s not quick on response, packs processing power.
  • In our effort the Pi is the big brain, so the robot will be capable of some autonomy. And the Pi 2 is a decent processor for the brain.
  • As a successful roboticist adventuring in mechatronics, you will need a variety of skills: coding, electrical circuits, mechanical, CAD design, etc. You may struggle in one area but can reach out to others for advice. We leverage a cross pollination of skill sets.

Arduino Pins

Arduino has many pins, some of which serve multiple functions. These pins provide power, send control signals and read information from external devices. The pins are the critical path for control in a microcontroller.

Vcc pin

This is electrical power output from the Arduino board. It’s not enough to power a DC motor but it is enough to run simple low power DC circuits like the ones in our exercises.

Gound (GND) pin

Ground is the return path for electrons in a circuit.

General Purpose Input/Output (GPIO) pins

These pins can read or write electrical signals such that they may be read or set programmatically. This makes them available to our software for decision making and control. Several uses are mapped to a subset of these pins.

Analog to digital conversion (ADC)

The Atmega controllers used for the Arduino contain an onboard 6 channel analog-to-digital (A/D) converter. The converter has 10 bit resolution, returning integers from 0 to 1023. While the main function of the analog pins for most Arduino users is to read analog sensors, the analog pins also have all the functionality of general purpose input/output (GPIO) pins (the same as digital pins 0 – 13).

Consequently, if a user needs more general purpose input output pins, and all the analog pins are not in use, the analog pins may be used for GPIO.

Pulse Width Modulation (PWM)

Pulse Width Modulation or PWM is used to control servos and motors. PWM can also be used to encode data for transmission. Thus with PWM we encode a digital value in an analog signal.

PWM produces a square wave. The signal is high for some fraction of the wave and low for the rest, then repeats. The modulation part is how much high versus low in a cycle. The wave frequency is constant.

In the bulb dimming exercise, the dimness of the bulb is a trick of the eye. Assuming the led is fast enough, it actually blinks. Assuming our eye can detect change fast enough we’d be able to see the LED blink. Between the 2, PWM causes the led appear dim.

Resistor color coding

Resistors have bands around them to encode their values.

Capacitor chart

Capacitors may have values printed on them or encoded as colors.

Arduino Shields

An Arduino shield is a board which contains particular functionality designed to be easy to be add to the Arduino. Functionality includes GPS, ZigBee, Motor Controllers, FM Radio, MP3 players, etc.

Arduino IDE for Teensy

The Teensy uses an older version of the Arduino IDE.

For native environment development

To get Arduino development running from VirtualBox

You’ll want to have the virtual machine installed.

VirtualBox settings

  • Make sure the Teensy is plugged into a USB port on your computer.
  • Make sure the virtual machine is shut down.
  • If the light on the Teensy is blinking, press the reset button.
  • Select the virtual machine and settings.
  • Select USB and then click the + on the right side of the window
  • You should see an entry Unknown device … [0120]. Add that device. The values should be:
    • Name: Unknown device 16C0:0478 [0120]
    • Vendor ID: 16c0
    • Product ID: 0478
    • Revision: 0120
    • Remote: No
    • The remaining fields are blank
  • You need a second device
    • Name: Teensyduino USB Serial [0100]
    • Vendor ID: 16c0
    • Product ID: 0483
    • Revision: 0100
    • Manufacturer: Teensyduino
    • Product: USB Serial
    • Serial No.: 12345
    • Remote: No
    • The remaining fields are blank
  • Start the virtual machine

Start a terminal

  • In order to run the Teensy compatible Arduino IDE, type:

    You may see this error which you can safely ignore:

    Error parsing gtk-icon-sizes string: ''
  • From the menu, select Tools->Board->Teensy 2.0. Make sure you don’t select Teensy++ 2.0


  • Load your sketch, click upload. This will compile and upload your code. Keep an eye on message window for compile errors or other trouble. You will be prompted to press the reset button on the Teensy to start the process. Once the Arduino IDE has completed, the Teensy loader should popup and you will be prompted again to activate the code by pressing the Teensy reset button.

Blink, no wires

This part assumes you have a running Arduino IDE. Most Arduinos have a pin which is also connected to an on board LED. To exercise that LED and test out your software to Arduino connection, we’ll try a blink example.

  • Start the Arduino IDE for Teensy as described above.
  • Load the blink example sketch using the menu File->Examples->01.Basics->Blink.
  • If you are using the Teensy, change the line:
    int led = 13;
    – to –
    int led = 11;
    Other Arduino boards may require a different pin.
  • Save the sketch with a name like MyBlink.
  • To load your sketch, click upload and follow the instructions on the screen. Watch for errors in the message window.

Blink Plus Wires

Let’s do that same exercise but this time with an external LED.

  • Plug your Arduino into the breadboard. The Arduino should be not be powered so disconnect the USB cable if you’ve connected it.
  • Using a wire, connect your ground (GND) to the  pin ‘-‘ bus.
  • Using a wire, connect your Vcc (+5V) to the ‘+’ bus.
  • You will need a 1K Ohm resistor. Connect that to 2 open, completely unoccupied, rows.
  • Next, connect the long lead of the LED to the same row as one end of the resistor and the other to an open row.
  • Using a wire, connect pin 11 to other side of the resistor.
  • Using a wire, connect the short pin of the LED to the GND/ ‘-‘ bus.
  • Plug in the Teensy to your PC’s USB port, download the program and enjoy the light show.
  • The program line delay(1000) means wait for 1000 milliseconds which is 1 second. For fun, change the values and upload the code again!

Debug output

Let’s try some debugging.

  • We’ll start with the circuit and sketch described above in “Blink plus wires”.
  • We are going to use Serial functions to send debug information back to the IDE. Please read this section on the Teensy site.
  • Add this line to your setup() method:
    Serial.begin(9600); // USB is always 12 Mbit/sec
  • Add this line to your loop() method:
    Serial.println(“Hello World…”);
  • Make sure you’ve selected your serial port. If you are running the VM, it should be /dev/ttyACM0
  • Download the sketch then open your Serial Monitor  (Tools -> Serial Monitor).

You can debug without the IDE as well. We won’t cover it in this lab, but you can read about it here.

Translating a circuit and/or code

Say you want to you find a circuit diagram for a particular project. Often you’ll find information for a different board and software designed for that board. This is a typical use case.

The Teensy diagram.

To take a diagram from the different platform and translate you must:

  • Get the pin description for the source board and/or integrated circuit.
  • Get the pin description for the target board and/or integrated circuit.
  • Document the pins used in the source.
  • Decipher the the use of each pin. A pin may have multiple uses.
  • Map the use to the target.
  • We have a map to the Teensy pins. You’ll need to understand how those pins are used in the Arduino environment.
  • Let try this project to dim an LED with a potentiometer as a class exercise.


Did you like this? Share it: