Forum Rule: Always post complete source code & details to reproduce any issue!
Page 1 of 3 1 2 3 LastLast
Results 1 to 25 of 73

Thread: Teensy Quick Reference: Code Examples, Tips and Tricks

  1. #1
    Senior Member
    Join Date
    Oct 2012
    Location
    Portland OR
    Posts
    724

    Teensy Quick Reference: Code Examples, Tips and Tricks

    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/37531...l=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...ltage-Question

    How fast is the DAC? https://forum.pjrc.com/threads/26036...ll=1#post49821

    Are lock bits supported? Can I run T3.1 at 72 MHz? http://forum.pjrc.com/threads/25407-...ll=1#post44401

    Can I use Teensy in a commercial product? http://forum.pjrc.com/threads/799-Fi...ull=1#post1783
    ...and also http://forum.pjrc.com/threads/24063-...ll=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-...nt-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...-USB-connector

    How can code tell which Teensy is selected at compile time? https://forum.pjrc.com/threads/25395...ll=1#post94035

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

    printf / scanf with floating point (otherwise not enabled as of Teensyduino 1.21 )
    https://forum.pjrc.com/threads/27827...-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-...ll=1#post44197

    Restart CPU without reboot http://forum.pjrc.com/threads/24304-...5253#post45253

    Reset and Reboot http://forum.pjrc.com/threads/24304-...nsyduino%28%29
    http://forum.pjrc.com/threads/25306-...ll=1#post45344

    Read/Write 8 bits in parallel https://forum.pjrc.com/threads/17532...ll=1#post21228

    Setting an I/O pin to Open Drain http://forum.pjrc.com/threads/25393-...ll=1#post44249

    Increase I/O Slew Rate (default = 7 ns, fast = 0.85 ns) https://forum.pjrc.com/threads/28735...ll=1#post75044

    Capacitive Touch Sensing example using touchRead() http://dangerousprototypes.com/forum...?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-...ll=1#post43873

    Complementary PWM output with settable dead time: http://forum.pjrc.com/threads/24992-...ll=1#post44563

    Edge capture with DMA from user 'tni'
    https://forum.pjrc.com/threads/37148...l=1#post124805

    Measure noise level of 16-bit ADC http://forum.pjrc.com/threads/25111-...ll=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/25395...l=1#post116099

    Software reboot http://forum.pjrc.com/threads/24304-...nsyduino%28%29

    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-...ll=1#post43724

    Coding in Assembly (separate ASM file) https://forum.pjrc.com/threads/27938...3-1-or-Teensy2

    MicroSD Card Guide http://forum.pjrc.com/threads/16758-...-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...ll=1#post86850

    Note that analogWrite() goes from 0 up to 256, not 255. https://forum.pjrc.com/threads/33473...ll=1#post99345

    Mouse Boot Protocol https://forum.pjrc.com/threads/33601...-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...l=1#post114355

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

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

    ADC Library by Pedvide http://forum.pjrc.com/threads/25532-...for-Teensy-3-1

    Entropy Library for Random Numbers from Timers http://forum.pjrc.com/threads/25369-...ll=1#post44135

    Low Power Library (Beta) (Sleep, DeepSleep, Hibernate) http://forum.pjrc.com/threads/23660-...ll=1#post42776

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

    Audio Library (Beta) for T3 Audio Adapter Board http://forum.pjrc.com/threads/24793-...ll=1#post39468
    https://github.com/PaulStoffregen/Audio
    Change ADC/DAC sample rate https://forum.pjrc.com/threads/45610...l=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-...-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-...or-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-...for-Teensy-3-1

    USB Host Library http://www.circuitsathome.com/tag/usb-host-shield
    ...and also http://forum.pjrc.com/threads/13433-...ll=1#post36118

    Simple Task Library https://forum.pjrc.com/threads/33357...E-task-library

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

    C++ STL (Standard Template Library) https://forum.pjrc.com/threads/23467...ll=1#post69787

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

    Hardware mods to operate T3.2 on 1.8V https://forum.pjrc.com/threads/35592...l=1#post149208

    Pinouts of Teensy 3.x and other boards https://docs.google.com/spreadsheets...t#gid=95008987

    Using All Teensy3.x Pins with a Socket http://forum.pjrc.com/threads/26071-...-with-a-socket
    Breakout All Teensy3.x Pins https://www.tindie.com/products/logl...y-31-breakout/
    Teensy 3.1 Sheilds http://forum.pjrc.com/threads/25620-...ld-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/logl...y-31-breakout/
    Pico's Teensy 3.x to Arduino shield + nRF24L01+ radio breakout https://www.tindie.com/products/pico...otyping-area-/
    Pesky Products LSM9DS0 micro T3.x shield (9 axis) https://www.tindie.com/products/oneh...-micro-shield/
    Pesky Products LSM9DS0 mini T3.x shield (9 axis + ALT) https://www.tindie.com/products/oneh...1-mini-shield/
    Petit Studio's Teensy 3.x development board and Arduino shield (all pins) https://www.tindie.com/products/fret...rd-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...ll=1#post65310

    Developing with Eclipse http://forum.pjrc.com/threads/1474-A...or-development

    Optimizing code size (FLASH space) http://forum.pjrc.com/threads/25196-...-on-Teensy-3-x

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

    Oscilloscope advice: http://forum.pjrc.com/threads/25730-...ll=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...-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/KIC...Software+Suite
    Fritzing simple/easy for beginners http://fritzing.org/home/
    Last edited by JBeale; 01-09-2018 at 08:15 PM. Reason: add tni's DMA capture example

  2. #2
    Senior Member
    Join Date
    Jan 2013
    Posts
    966
    Very nice! This should by all means be a sticky thread!

  3. #3
    Administrator Paul's Avatar
    Join Date
    Oct 2012
    Posts
    439
    Quote Originally Posted by Headroom View Post
    This should by all means be a sticky thread!
    Done.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,660
    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.

  5. #5
    Senior Member
    Join Date
    Oct 2012
    Location
    Portland OR
    Posts
    724
    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 by JBeale; 03-20-2014 at 12:58 AM.

  6. #6
    Senior Member
    Join Date
    Mar 2013
    Location
    Austin TX
    Posts
    429
    High-speed I2C library (Wire & Wire1): http://forum.pjrc.com/threads/21680-...ry-for-Teensy3

    ADC library: http://forum.pjrc.com/threads/24399-...C-as-a-library

    Eclipse: http://forum.pjrc.com/threads/1474-A...or-development

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

    Optimizing build size: http://forum.pjrc.com/threads/25196-...-on-Teensy-3-x

    Software reboot: http://forum.pjrc.com/threads/24304-...nsyduino%28%29

    I would include some RTOS links, but I'm not sure what is considered best or most current.
    Last edited by nox771; 03-20-2014 at 01:53 AM.

  7. #7
    Senior Member
    Join Date
    Jun 2013
    Location
    Montréal
    Posts
    473

  8. #8
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,825
    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?

  9. #9
    Senior Member
    Join Date
    Mar 2013
    Location
    Austin TX
    Posts
    429
    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-U...ital-I-O-delay
    There is a latency test program: http://forum.pjrc.com/attachment.php...9&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...9&d=1358182899

  10. #10
    Senior Member duff's Avatar
    Join Date
    Jan 2013
    Location
    Las Vegas
    Posts
    1,027
    Here is another useful post for inline assembly here

  11. #11
    Senior Member
    Join Date
    Jan 2013
    Location
    San Francisco Bay Area
    Posts
    659
    MicroSD guide: http://forum.pjrc.com/threads/16758-...-MicroSD-guide

    Something on IntervalTimer would be good, but the posts I saw might be more confusing, since it was about the creation of the library: http://forum.pjrc.com/threads/17543-...or-Teensy-3-0/

  12. #12
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    4,385
    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 by MichaelMeissner; 03-20-2014 at 05:53 PM.

  13. #13
    Senior Member
    Join Date
    Mar 2013
    Location
    Austin TX
    Posts
    429
    IIRC this one:
    has been superceded by IntervalTimer, which is part of core and now has a reference page: http://pjrc.com/teensy/td_timing_IntervalTimer.html
    Trying to manually work with PIT might cause problems.

  14. #14
    Senior Member
    Join Date
    Oct 2012
    Location
    Portland OR
    Posts
    724
    @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.

  15. #15
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,660
    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-...4400#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-Fi...ull=1#post1783
    http://forum.pjrc.com/threads/24063-...ll=1#post34451

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

    http://forum.pjrc.com/threads/25101-...for-Teensy-3-1

  16. #16
    Senior Member pictographer's Avatar
    Join Date
    May 2013
    Location
    San Jose, CA
    Posts
    710
    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.

  17. #17
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,825
    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 by stevech; 03-25-2014 at 10:50 PM.

  18. #18
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,660
    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?

  19. #19
    Senior Member
    Join Date
    Mar 2013
    Location
    Austin TX
    Posts
    429
    Hmm.. <puts on flameproof suit>
    The first Eagle link should be a link to DesignSpark

  20. #20
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,825
    Quote Originally Posted by nox771 View Post
    Hmm.. <puts on flameproof suit>
    The first Eagle link should be a link to DesignSpark
    Nah, DipTrace (free or cheap version). Great stuff. diptrace.com

  21. #21
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,825
    How to restart (not reboot) Teensy 3.x, since the library to do that as in Teensy 2 doesn't exist yet.
    see
    post #21 on thread http://forum.pjrc.com/threads/24304-...5253#post45253

  22. #22
    Senior Member+
    Join Date
    Jul 2013
    Posts
    295

  23. #23
    Senior Member pictographer's Avatar
    Join Date
    May 2013
    Location
    San Jose, CA
    Posts
    710
    Oscilloscope advice:
    http://forum.pjrc.com/threads/25730-...ll=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.

  24. #24
    Senior Member pictographer's Avatar
    Join Date
    May 2013
    Location
    San Jose, CA
    Posts
    710
    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.

  25. #25
    Senior Member pictographer's Avatar
    Join Date
    May 2013
    Location
    San Jose, CA
    Posts
    710

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •