Teensy Quick Reference: Code Examples, Tips and Tricks

JBeale

Well-known member
The information below generally applies to Teensy 3.0, 3.1 and 3.2, unless otherwise noted.

---------------- FREQUENTLY ASKED QUESTIONS --------------------

Why can't my PC see or program my Teensy? Make sure you have a full USB cable and not a 2-wire charging-only cable.

How to completely erase a T3.x? Press program button for exactly 15 seconds (+/- 2 sec)
https://forum.pjrc.com/threads/3753...ader-reprogram?p=116941&viewfull=1#post116941

What is the PWM frequency range and resolution? http://www.pjrc.com/teensy/td_pulse.html

What Timers are available? (12, in hardware) https://forum.pjrc.com/threads/28714...ll=1#post73823

What is the internal reference voltage? (1.2V on T3, 2.56V on T2, none on LC)
https://forum.pjrc.com/threads/35446-Teensy-Internal-Reference-Voltage-Question

How fast is the DAC? https://forum.pjrc.com/threads/2603...-1-using-Arduino?p=49821&viewfull=1#post49821

Are lock bits supported? Can I run T3.1 at 72 MHz? http://forum.pjrc.com/threads/25407...nd-Reprogramming?p=44401&viewfull=1#post44401

Can I use Teensy in a commercial product? http://forum.pjrc.com/threads/799-Firmware-loader-for-Teensy-3?p=1783&viewfull=1#post1783
...and also http://forum.pjrc.com/threads/24063-Paul-Teensy-in-a-commercial-product?p=34451&viewfull=1#post34451

Where are the Teensy pinouts? http://www.pjrc.com/teensy/pinout.html

How much drive current at pin? (9 mA, 25 mA abs.max)
https://forum.pjrc.com/threads/5455-Teensy-3-0-resistors-and-DC-current-per-I-O-pin

Where are the Teensy [1,2,3] schematics? http://www.pjrc.com/teensy/schematic.html

Where are the CPU datasheets? https://www.pjrc.com/teensy/datasheets.html

Can I overclock beyond 96 MHz? (yes, just edit your "boards.txt")
For example, uncomment "# teensy31.menu.speed.120opt=120 MHz optimized (overclock)" in C:\Program Files (x86)\Arduino\hardware\teensy\avr\boards.txt

How can I wire USB into Teensy without a connector? https://forum.pjrc.com/threads/19336-How-to-repair-a-broken-off-Teensy-3-0-USB-connector

How can code tell which Teensy is selected at compile time? https://forum.pjrc.com/threads/2539...-Tips-and-Tricks?p=94035&viewfull=1#post94035

---------------- CODE EXAMPLES --------------------

printf / scanf with floating point (otherwise not enabled as of Teensyduino 1.21 )
https://forum.pjrc.com/threads/27827-Float-in-sscanf-on-Teensy-3-1

Better choices for timing than delay() https://www.pjrc.com/teensy/td_timing.html

Using the built-in watchdog timer (changes mk20dx128.c ) http://forum.pjrc.com/threads/25370-Teensy-3-0-Watchdog-Timer?p=44197&viewfull=1#post44197

Restart CPU without reboot http://forum.pjrc.com/threads/24304...)-vs-_restart_Teensyduino()?p=45253#post45253

Reset and Reboot http://forum.pjrc.com/threads/24304-_reboot_Teensyduino()-vs-_restart_Teensyduino()
http://forum.pjrc.com/threads/25306-Teensy-3-1-Reset?p=45344&viewfull=1#post45344

Read/Write 8 bits in parallel https://forum.pjrc.com/threads/1753...-GPIO_PDIR-_PDOR?p=21228&viewfull=1#post21228

Setting an I/O pin to Open Drain http://forum.pjrc.com/threads/25393...sy-3-1-processor?p=44249&viewfull=1#post44249

Increase I/O Slew Rate (default = 7 ns, fast = 0.85 ns) https://forum.pjrc.com/threads/2873...ed-on-Teensy-3-1?p=75044&viewfull=1#post75044

Capacitive Touch Sensing example using touchRead() http://dangerousprototypes.com/forum/viewtopic.php?t=4606#p45520
T3/T3.1 touch sensing pins are: 0, 1, 25, 32, 33, A1, A2, A3, A4, A5, A8, and A9

Waveform generation from table using DAC and PDB (T3.1) http://forum.pjrc.com/threads/25275...)-and-DAC-Buffer?p=43873&viewfull=1#post43873

Complementary PWM output with settable dead time: http://forum.pjrc.com/threads/24992-phase-correct-PWM?p=44563&viewfull=1#post44563

Edge capture with DMA from user 'tni'
https://forum.pjrc.com/threads/37148-Measuring-pulse-width-interrupts?p=124805&viewfull=1#post124805

Measure noise level of 16-bit ADC http://forum.pjrc.com/threads/25111-Decreasing-noise-on-Teensy-ADC?p=42891&viewfull=1#post42891

Readout MAC Address http://forum.pjrc.com/threads/91-teensy-3-MAC-address

128-bit unique identification (ID) number per chip https://forum.pjrc.com/threads/25522...ll=1#post72175

Compile Time information https://forum.pjrc.com/threads/2539...ips-and-Tricks?p=116099&viewfull=1#post116099

Software reboot http://forum.pjrc.com/threads/24304-_reboot_Teensyduino()-vs-_restart_Teensyduino()

Waking up a USB host computer https://forum.pjrc.com/threads/26649...h-a-teensy-3-0

Inline Assembly http://forum.pjrc.com/threads/25317-Assembly-coding-for-Teensy3-1?p=43724&viewfull=1#post43724

Coding in Assembly (separate ASM file) https://forum.pjrc.com/threads/27938-Using-Assembler-with-Arduino-and-Teensy3-1-or-Teensy2

MicroSD Card Guide http://forum.pjrc.com/threads/16758-Teensy-3-MicroSD-guide

Remap SCK from Pin 13 http://forum.pjrc.com/threads/25349-Remapping-pin-13

Double precision constants must end with 'L' otherwise they are made into single-precision floats
https://forum.pjrc.com/threads/31233-float-and-double?p=86850&viewfull=1#post86850

Note that analogWrite() goes from 0 up to 256, not 255. https://forum.pjrc.com/threads/33473-RGB-LED-Common-Anode?p=99345&viewfull=1#post99345

Mouse Boot Protocol https://forum.pjrc.com/threads/33601-Mouse-boot-protocol

---------------- LIBRARIES --------------------

List of Teensy Libraries http://www.pjrc.com/teensy/td_libs.html

SdFat Beta library support for T3.5/3.6 SDIO https://github.com/greiman/SdFat-beta https://forum.pjrc.com/threads/36737-Try-SdFat-forTeensy-3-5-3-6?p=114355&viewfull=1#post114355

DMA SPI library https://github.com/crteensy/DmaSpi https://forum.pjrc.com/threads/27909-DmaSpi-for-teensyduino-1-21

High-speed I2C library (Wire & Wire1) http://forum.pjrc.com/threads/21680-New-I2C-library-for-Teensy3

ADC Library by Pedvide http://forum.pjrc.com/threads/25532-ADC-library-update-now-with-support-for-Teensy-3-1

Entropy Library for Random Numbers from Timers http://forum.pjrc.com/threads/25369...Number-Generator?p=44135&viewfull=1#post44135

Low Power Library (Beta) (Sleep, DeepSleep, Hibernate) http://forum.pjrc.com/threads/23660...For-The-Teensy-3?p=42776&viewfull=1#post42776

Snooze (Duff's low power library) https://github.com/duff2013/Snooze
All Things Low Power https://forum.pjrc.com/threads/32454-All-things-Low-Power

Audio Library (Beta) for T3 Audio Adapter Board http://forum.pjrc.com/threads/24793-Audio-Library?p=39468&viewfull=1#post39468
https://github.com/PaulStoffregen/Audio
Change ADC/DAC sample rate https://forum.pjrc.com/threads/4561...n-ADC-slower-)?p=150049&viewfull=1#post150049

FastLED Library for LED strips: WS2810, WS2811, LPD8806, Neopixel... http://fastled.io/ and https://github.com/FastLED/FastLED/wiki/Overview

Artnet for OctoWS2811 LED strips (DMX over Ethernet) (needs WIZ820io + T3 adaptor ) : http://forum.pjrc.com/threads/24688-Artnet-to-OctoWS2811

Interval Timer using interrupts http://pjrc.com/teensy/td_timing_IntervalTimer.html
See also: Metro Library http://pjrc.com/teensy/td_libs_Metro.html and ElapsedMillis http://pjrc.com/teensy/td_timing_elaspedMillis.html
(older "Periodic Interrupt Timer Library" now depreciated?) http://forum.pjrc.com/threads/17543-PITimer-PIT-(Periodic-Interrupt-Timer)-library-for-Teensy-3-0/

Pulse Position Library 0.02 us precision Tx/Rx (T3.1) http://www.pjrc.com/teensy/td_libs_PulsePosition.html

CAN Bus Library (T3.1) http://forum.pjrc.com/threads/25101-CAN-Bus-Library-for-Teensy-3-1

USB Host Library http://www.circuitsathome.com/tag/usb-host-shield
...and also http://forum.pjrc.com/threads/13433-USB-Host-Status?p=36118&viewfull=1#post36118

Simple Task Library https://forum.pjrc.com/threads/33357-Is-there-a-good-SIMPLE-task-library

Teensy 2 Libraries https://www.pjrc.com/teensy/td_libs.html

C++ STL (Standard Template Library) https://forum.pjrc.com/threads/23467-Using-std-vector?p=69787&viewfull=1#post69787

---------------- HARDWARE SHIELDS & ADD-ONS --------------------

Hardware mods to operate T3.2 on 1.8V https://forum.pjrc.com/threads/35592-Teensy-Low-Voltage-Operation?p=149208&viewfull=1#post149208

Pinouts of Teensy 3.x and other boards https://docs.google.com/spreadsheet...YksMG306_FpWdJcniSRR6aGNNYQ/edit#gid=95008987

Using All Teensy3.x Pins with a Socket http://forum.pjrc.com/threads/26071-Using-all-Teensy3-x-pins-with-a-socket
Breakout All Teensy3.x Pins https://www.tindie.com/products/loglow/teensy-31-breakout/
Teensy 3.1 Sheilds http://forum.pjrc.com/threads/25620-Teensy-3-1-to-Arduno-Shield-DIY-Adaptor

PJRC's Prop Shield (10 axis+audio,LED drv.) http://www.pjrc.com/store/prop_shield.html
PJRC's audio adapter http://www.pjrc.com/store/teensy3_audio.html
PJRC's WIZ820io and SD card adapter http://www.pjrc.com/store/wiz820_sd_adaptor.html
PJRC's Octows2811 (neopixel) adapter http://www.pjrc.com/store/octo28_adaptor.html
PJRC's double high breakaway pins for shields needing more clearance http://www.pjrc.com/store/header_14x1_d.html
PJRC's 14 pin female header socket http://www.pjrc.com/store/socket_14x1.html
KurtE's Teensy 3.1 breakout board https://github.com/KurtE/Teensy3.1-Breakout-Boards
TallDog's breakout board to access all pins https://www.tindie.com/products/loglow/teensy-31-breakout/
Pico's Teensy 3.x to Arduino shield + nRF24L01+ radio breakout https://www.tindie.com/products/pico/rfx-teensy-3x-nrf24l01-carrier-board-w-prototyping-area-/
Pesky Products LSM9DS0 micro T3.x shield (9 axis) https://www.tindie.com/products/onehorse/lsm9ds0-teensy-31-micro-shield/
Pesky Products LSM9DS0 mini T3.x shield (9 axis + ALT) https://www.tindie.com/products/onehorse/lsm9ds0-teensy-31-mini-shield/
Petit Studio's Teensy 3.x development board and Arduino shield (all pins) https://www.tindie.com/products/freto/teensy-3-breakout-board-and-shield/
Pixelmatrix's SmartMatrix shield for 16x32 and 32x32 LEDs http://docs.pixelmatix.com/SmartMatrix/shieldref.html (Adafruit also)

---------------- DEV TOOLS --------------------

TyQT for more than one Teensy on a PC https://forum.pjrc.com/threads/27825-Teensy-Qt?p=65310&viewfull=1#post65310

Developing with Eclipse http://forum.pjrc.com/threads/1474-Anyone-using-Eclipse-for-development

Optimizing code size (FLASH space) http://forum.pjrc.com/threads/25196-How-to-get-a-little-bit-more-FLASH-room-on-Teensy-3-x

PC Host communication: USB Latency Test discussed in http://forum.pjrc.com/threads/7826-USB-to-digital-I-O-delay

Oscilloscope advice: http://forum.pjrc.com/threads/25730-Oscilloscope-advice?p=47209&viewfull=1#post47209

---------------- PCB LAYOUT TOOLS --------------------

There are many options, but some free-of-charge versions are often used to do open hardware:

Eagle CAD most often used(?) http://www.cadsoftusa.com/download-eagle/?language=en
Design Spark PCB shows ads http://www.rs-online.com/designspark/electronics/eng/page/designspark-pcb-home-page
DipTrace (free ver: 300 pin limit) http://diptrace.com/downloads/download-diptrace/
KiCad open source, unrestricted http://www.kicad-pcb.org/display/KICAD/KiCad+EDA+Software+Suite
Fritzing simple/easy for beginners http://fritzing.org/home/
 
Last edited:
I'm pretty sure it's going to get cluttered, but John will be editing the main message.

If there's a desire to have the clutter deleted, maybe like once a week, John can send a request to me or Robin. Actually best to send to Robin... she does most of the daily behind-the-scenes forum maintenance that nobody sees, mostly deleting and banning the spammers who get through the human verification but lurk for weeks or months before spamming, in hopes of evading "new user" filters.
 
Suggestions welcome! I don't have enough time or attention span to cover the big and rapidly growing world of Teensy by myself. You could post your suggestion here; and I could copy it into my top post. If overlong threads are really a problem, you could delete your own post (can you?) or I could delete it (if Paul's OK with that). Or we could have a second separate thread just for suggestions.

But if I can keep things updated, most people can freely ignore the remainder of the thread posts- or go looking through them, if interested, or to find something I missed, etc.

I see the list as having two main purposes, (1) as reference, quickly find that useful bit of info/code you need but couldn't find, and (2) as inspiration; examples of what others are doing with Teensy that you might not have considered.
 
Last edited:
Is there a way that the additions such as post #6 can be added to post #1 by any user? Or by a moderator for this section?
 
Here is another one. I would actually rank this as one of the top things I've gotten off the forum. This has to do with when you migrate beyond using comm terminals and start writing your own custom host programs. For working with the comm ports (not HID), web searches generally direct to things like Boost.Asio, libctb, or similar. I've worked with a number of such libraries, and they all have problems (eg. linking in Boost is sort of like linking in the library of congress..)

In this thread: http://forum.pjrc.com/threads/7826-USB-to-digital-I-O-delay
There is a latency test program: http://forum.pjrc.com/attachment.php?attachmentid=149&d=1358182899

It's not a library, and it takes a bit of dirty code extraction, but in the latency_test.c file are transmit_bytes() and receive_bytes() routines which are setup to compile and work across Linux, Mac, and Win systems. The routines are very short, and orders of magnitude easier than trying to get something like Boost going. So in summary:

PC Host communication routines: http://forum.pjrc.com/attachment.php?attachmentid=149&d=1358182899
 
The touchRead function is not documented in the the Teensy library page. The touchRead function does capacitive reading on select pins and it allows you to create switches and analog meters with with nothing but an extra wire (providing you share a common ground with the Teensy). This shows a minimal use for touchRead: http://njhurst.com/blog/01356576041.

On both the Teensy 3.0 and 3.1 chips, the touch sense pins are: 0, 1, 25, 32, 33, A1, A2, A3, A4, A5, A8, and A9
 
Last edited:
@nox771: Thanks, that's good to know.. I updated again. I find the Teensyduino pages at http://pjrc.com/teensy/teensyduino.html are a little confusing as it's not immediately obvious from the top level what applies to all versions, or just Teensy 2 ,or just T3. In fact the top level doesn't mention Teensy 3 at all, and I assumed it was all old material.
 
Maybe add a "frequently asked questions" section?

Here's 2 answers for the price of one (Lock bits and 72 MHz support on Teensy 3.1)

http://forum.pjrc.com/threads/25407-Teensy-3-0-3-1-Lock-Bits-and-Reprogramming?p=44400#post44400

Another frequent question involves whether it's ok to use Teensy in commercial projects. I believe I've answered that several times. Here's a couple of the detailed ones:

http://forum.pjrc.com/threads/799-Firmware-loader-for-Teensy-3?p=1783&viewfull=1#post1783
http://forum.pjrc.com/threads/24063-Paul-Teensy-in-a-commercial-product?p=34451&viewfull=1#post34451

Also, maybe this CAN bus thread would be good to include in the libraries part?

http://forum.pjrc.com/threads/25101-CAN-Bus-Library-for-Teensy-3-1
 
USB Host
http://www.circuitsathome.com/tag/usb-host-shield
http://forum.pjrc.com/threads/13433-USB-Host-Status

FAQ Candidates:

  • How do I deal with this 3.3V vs 5V stuff?
  • How do I control something that needs more current than a Teensy I/O can source?
  • Why is the reset button sometimes needed and sometimes not?
  • How do I make a serial connection to a Teensy without the Arduino Serial Monitor? E.g. cu, screen, Putty, TeraTerm, etc.
  • Are there any full color graphic displays that are easy to use with a Teensy?
  • What's the cheapest display that's easy to use?
  • How do I make a custom keyboard from a Teensy? How are the buttons wired? How does the software send keystrokes?
  • How do I drive a cheap speaker with a Teensy? E.g. switch a transistor using PWM, varying the frequency. Low pass filter. Software. Etc.
  • What's a good rechargeable battery solution? What kind of voltage regulator? How is the battery wired? Where do I cut? Which way does the diode go? How do I prevent a USB charger from turning itself off?
  • Is my Teensy dead?
  • What's the difference between an Arduino and a Teensy in terms of software support?
  • What's the difference between an Arduino sketch and a C++ program?
  • What are the popular wireless options for a Teensy and what are the differences between them? E.g. wifi router, bluetooth module, wifi module, 315/433 MHz RF module, XBee module, GSM module, GPRS module.
 
Re: Wireless data radio, HopeRF RFM22 and RF22B modules and the excellent RF22 Arduino/Teensy library for the radio modules.

Relevant to the RF22 library for AVR and Teensy 2,3...
1. See this tread for solution to pin 13, 14 remapping to move SPI SCK to pin 14 so pin 13 can be used as LED. http://forum.pjrc.com/threads/25349-Remapping-pin-13

2. Some RF22 modules have an TX/RX antenna switch (6 pin DIP IC); Radio's GPIO0 and GPIO1 bits control the switch, normally. Some RF22 modules have these two GPIO bits wired opposite (TX/RX) versus others. The #ifdef in the RF22 library is hard-coded with an #ifdef 0 on this hardware specific choice. If wrong on the ifdef, the signals are 40dB too weak (antenna not used). Cure is to put the same code as in the library in the application but change the #ifdef to match hardware wiring of the GPIO bits.
This library code:
Code:
#if 0
    spiWrite(RF22_REG_0B_GPIO_CONFIGURATION0, 0x12) ; // TX state
    spiWrite(RF22_REG_0C_GPIO_CONFIGURATION1, 0x15) ; // RX state
#else
    // Reversed for HAB-RFM22B-BOA HAB-RFM22B-BO
    spiWrite(RF22_REG_0B_GPIO_CONFIGURATION0, 0x15) ; // RX state
    spiWrite(RF22_REG_0C_GPIO_CONFIGURATION1, 0x12) ; // TX state
#endif
can be copied into application and the #if 0 can be #if 1 depending on the hardware. I had RFM22B modules so #if 1 is needed.

Of course, a mix of RFM module hardware types would be messy in terms of this. Each RFM22 (SiLab's 4431 chip) returns a device type code that's in the data sheet for the chip. Mine were type = 8. Others are type 7.
When I get next batch I ordered from a different source, perhaps the type 7 vs. 8 can be used to decide at run time rather than compile time.

Modules with no TX/RX switch (e.g., low TX power) can just skip this code.

These changes made, my radios are working very well. Using the reliable datagram mode (ACKs for every unicast message), with node addressing.
 
Last edited:
Maybe links to the many Eagle files/libs people have posted would be good too add?

I personally do not use Eagle, so I'm not qualified to say which ones should be on this post. Maybe some Eagle users could recommend which threads have the best info?
 
Oscilloscope advice:
http://forum.pjrc.com/threads/25730-Oscilloscope-advice?p=47209&viewfull=1#post47209

There's at east one other thread that has good scope advice. If I recall, it might talk about other cheap options and their limitations.

A related question is how to build an oscilloscope from a Teensy and why this is a fine learning exercise, but probably won't get a very useful tool without significant investment in additional parts and very challenging analog design.
 
This forum software has its own FAQ, but it doesn't appear to be customized. Perhaps a link to this thread could be put there if not too much trouble.
 
Back
Top