Sunday, March 10, 2013

Teensy Basics 1: Overview and Setup

Overview
The Teensy is a small, inexpensive microcontroller platform that - when used with the Teensyduino add-on for Arduino - can be considered part of the larger Arduino family.


A microcontroller can be thought of as a small, programmable computer with it's own program space (ROM), working memory (RAM), non-volatile memory (EEPROM), input ports and output ports.

Unlike a laptop or a smartphone, a microcontroller does not come with speakers, visual display, keyboard or mouse built in - we need to build any input and output peripherals and connect them as a circuit to the microcontroller.

The Arduino is an open-source microcontroller platform and really revolutionised prototyping and development by becoming ubiquitous. This was partly due to a low cost entry point, easy to program IDE (integrated development environment), excellent documentation and a strong community with a willingness to share projects and code.

The Teensy - at least as we will be using it - belongs in this family and is related to the Arduino in terms of functionality and programming. We will be using the Teensy because it is cheap and easy to integrate into music project.



Examples
There are many examples of projects that use the Teensy board. I will list just three:

A26F Atari 2600 Synth - A MIDI interface for the Atari 2600 console
The Stomper - MIDI controller for DJ software
LED Video Wall - massive LED wall display





Setup
To setup your computer for use with the Teensy microcontroller platform, you will need:
• A teensy 2.0 board
Arduino software for your OS
Teensyduino software for your OS
Teensy loader software for your OS

Download and install the Arduino software, followed by the Teensyduino and Teensy loader software. During the install for the Teensyduino software, you will be asked to locate your Arduino software. Additionally, please install all components of the Teensyduino software.

Once you have installed the above three packages, we need to set up the Arduino IDE. Launch Arduino.

Go to Tools > Board > Teensy 2.0. 


Go to Tools > USB type MIDI. 


Go to Tools > CPU Speed > 16MHz.


In general, the Arduino software and the Teensy software interact like this:
1) You write your code in Arduino
2) You upload your code using the appropriate command in Arduino
3) This automatically launches the Teensy loader
4) You may need to press reset on the Teensy board in order to upload the program
5) The Teensy loader software will inform you of a clean reboot of the board



Teensy Board Layout and Specifications


Here we can see the Teensy board and its pin layout. The top left pin is ground and the top right pin is 5V. The remaining pins along the left hand side are numbered 0 - 10, whilst the remaining pins along the right hand side are labeled 11 - 21 AND A0 - A10.

The pin numbers from 0 - 21 refer to these pins operating as "digital" pins - these pins can be either inputs or outputs, and can respond to or control signals that are either HIGH or LOW, GROUND or 5V, 0 or 1. This includes things like buttons and switches as inputs, and LED lights as outputs.

The pin numbers from A0 - A10 are analog inputs. These pins can read a voltage that is anywhere from 0V to 5V, and return this voltage as a data value. These pins can be used for things like potentiometer knobs, sliders, light dependent resistors and so on - things that aren't just on or off, but have a continuous value from 0 - 5V.

Note that there are four more pins inside of the Teensy board, marked 22 A11, 23, 24 and REF. 22 A11 is an analog input pin AND a digital pin whereas 23 and 24 are digital pins.

In summary, the Teensy has 25 IO pins, 12 of which are analog input pins and 13 of which are digital only pins. The analog input pins can act as digital pins as well.

The Teensy has roughly 30KB of ROM program space and roughly 2KB of RAM working memory. The Teensy has an onboard LED that is connected to digital pin 11.




Hello World! 
Now that we have set up the Arduino / Teensy environment set up, we can start writing code for the Teensy.

When testing a computer programming language, it is "customary" to write a hello world program - something that displays the words "hello world" on a screen. With microcontrollers, it is customary to write a blinking LED program - the hello world equivalent.

When writing Teensy code, we use functions to execute specific tasks. There are two very special structural functions that contain our code and form the basis of our program. In fact, every Teensy and Arduino program has to have these functions otherwise they won't compile and upload. 

These functions are called setup() and loop().

The setup() function is executed only once by the Teensy when power is applied to it or it is reset by pushing the reset button. In setup(), we might do things like tell our Teensy that we are using certain pins in certain ways.

The loop() function is the main part of our program, and is looped indefinitely. In loop(), we would perform the majority of tasks of our program.

The trick to programming is to break it down into lots of tiny actions and steps, and to think logically about the flow of things. To make our blinking LED code, we need to do a number of things:
• We need to tell the Teensy which digital pin is our LED pin
• We need to tell the Teensy that that pin should act as an output pin
• We need to turn our LED on
• We need to wait
• We need to turn our LED off
• We need to wait
• The last four steps need to loop

So, let's write this out as code!


We are using three main functions in this code. Let's take a closer look at them:
• pinMode(11, OUTPUT);
This function is telling digital PIN 11 that it should behave as an OUTPUT, because our LED is connected to pin 11

• digitalWrite(11, HIGH) or digitalWrite(11, LOW);
This function is setting PIN 11 either HIGH (5V) or LOW (0V / ground). When PIN 11 is set to HIGH, this turns the LED on. When PIN 11 is set to LOW, this turns the LED off

• delay(200);
This function pauses our Teensy from executing any code at all for 200 milliseconds.

And that's it! Once you've typed out the code, go to File > Upload.


Keyword Summary
teensy setup, digital output, onboard LED, setup(), loop(), pinMode(), digitalWrite(), delay()


Demo Video

0 comments: