-
Notifications
You must be signed in to change notification settings - Fork 0
Teensy Workshop
The purpose of this tutorial is to provide a cursory introduction to the Teensy ecosystem for use in practical control applications.
A Teensy is a versatile programmable microcontroller that can be used for countless projects. A microcontroller is essentially a tiny computer which can be programmed and interact with the outside world. Microcontrollers are typically programmed in C or C++ and their code is generally classified as “embedded software”. Most microcontrollers have direct access to I/O pins - electrical pins which can be set to output a specific voltage or read a specific voltage. In the context of electric vehicles, microcontrollers are an integral part of interpreting the state of the system, reading driver input, and performing actions according to what is needed. Although they don’t carry the brunt of any work, they are the logic which dictates how other systems operate. Our fuel cell vehicle, Maxwell, had 3 arduino-compatible microcontrollers to control various aspects of the car. Arduino-style microcontrollers are generally suitable for prototyping only. Many of the same ideas and technologies extend to industrial and commercial settings, though.
If you’re already familiar with Arduinos, Teensys, and basic electronics, skip ahead to the challenges. Otherwise, keep on reading. And, of course, we highly recommend using google when unsure about anything because if you have a question regarding Teensy, Google is almost guaranteed to have the answer.
Source: https://www.pjrc.com/teensy/
There are several Teensy development boards available, which come in 32-bit or 8-bit architectures. All Teensys are programmed through a micro-USB port and have several input and output (I/O) pins that are capable of using analog and digital signals. A digital signal uses discrete binary logic - either on or off, or HIGH
and LOW
. An analog signal, on the other hand, is a continuous voltage signal (think of a multimeter) that ranges from 0V to 3.3V or 5V depending on the microcontroller.
For our purposes, we will be using the 32-bit Teensy 3.2 with a 72 MHz Cortex-M4 processor that operates on 3.3V logic. Thus, for digital signals, ‘off’ corresponds to 0V, and ‘on’ corresponds to 3.3V (± some threshold); analog signals range from 0V to 3.3V. It is also important to know that the Teensy pins are tolerant to input signals of up to 5V; anything above that will likely damage the microcontroller.
For example, turning a pin on via
-
digitalWrite( #, HIGH );
causes the voltage of pin # to be 3.3V. Likewise, reading a pin via -
int a = digitalRead( # );
causes a to be 0 if pin # is below the threshold voltage or to be a 1 if pin # is above a threshold voltage.
The Teensy platform operates on C/C++ development, and is compatible with the Arduino IDE through the Teensyduino extension. However, the Arduino IDE is impractical to use for development in the long-run, and Teensyduino requires more extensive setup with our ROS environment. As a result, we will be using the PlatformIO extension in Visual Studio Code, which provides cross-platform development for embedded systems. Refer to the PlatformIO website for installation and setup.
Now, we will move onto our first demo: manipulating a linear actuator. First, create a new file titled ‘linearActuatorDemo.ino’. Then, copy and paste the code for the tutorial that can be found here.
This program begins by executing the void setup
loop and goes line by line sequentially. Once it reaches the end of setup
, it will move on to loop
and go line by line. When it finished loop, it’ll just go back to the beginning of loop and repeat forever. Note that in this example, there is nothing inside loop
, therefore only the code in setup
will execute once and the program will terminate.
Most commands are fairly straightforward. Here is a short explanation for commands used in this program:
-
attach(###)
- assigns an output pin to an object. In this case, the motor. -
writeMicroseconds(###)
- outputs a PWM signal with the desired pulse width duration in microseconds. Here, 1500 microseconds corresponds to the motor shaft staying still. -
delay(###)
- waits for ### ms before proceeding to the next line
Here are some common, useful commands that you may also find useful:
-
digitalWrite(#, HIGH/LOW)
- sets digital pin # high or low -
digitalRead(#)
- reads whether digital pin # is high or low and returns 1 or 0 respectively -
analogRead(#)
- reads the voltage of analog pin # on a scale from 0 to 1023 (linearly maps from 0 to 5V) -
analogWrite(#, value)
- writes a “PWM” signal to pin # with duty cycle = value/1023
The best way to get acquainted with the language is to read a couple example Arduino programs and then write your own, Googling as you go. You can find a comprehensive list of Arduino examples of various applications here.
Teensys are pretty cheap - especially the Chinese knock-offs (or “clones” as people call them). Don’t be afraid to just try something. That said, there’s a couple things to always avoid:
- Never directly connect the GND, 5V, VIN, 3.3V, or any such power pins together.
- Try to avoid directly connecting any pin directly to any other pin - this is typically not something you would need to do anyway, but if you do, make sure you are not trying to digitalWrite different values to the same wire. If possible, always put a resistor in the path between 2 otherwise linked pins (this includes a switch connected to GND on one side).
- Always unplug power (includes USB) when rewiring - not only can it break the Teensy, it can even break your laptop (this actually happened to last year’s president (and this year's co-president) though in a slightly more bad-ass way)
- Never work with stripped wires dangling around - if any wires touch or touch a metal surface or anything like that, again you could be in a bad position. Try to keep all wires securely socketed in a breadboard. Avoid alligator clips when possible.
When designing your circuits, consider searching online for circuits which accomplish similar goals if you’re not confident in your electrical skills, or ask us for help and we’d be glad to help you arrive at a circuit that’ll do what you want.
Material inspired by Gerry Chen, Duke class of 2019