by on
Arduino

(NOTE: PJRC HAS SINCE UPDATED THE LIBRARY TO INCLUDE THE LEONARDO)

I have been teaching Physical Computing for Artists with the Arduino Uno for over a year now and this year I decided to move to the Leonardo with the hope that a USB-Midi based core will be finished sometime soon. Its cheaper than the uno and it has a serial port that isn’t busy and best of all it doesn’t reboot every time you talk to it.

I usually introduce background tasks using the Timer1 library since it makes timers manageable for the novice. However the existing Timer1 and Timer3 libraries are not currently avaliable for the leonardo (or any of the 4 32u4 based processors). Fortunately there is a “port” of the library for the Teensy2.0 at http://www.pjrc.com/teensy/arduino_libraries/TimerOne.zip, for some reason the leonardo is missing from the boards supported but no matter the fix is pretty easy and now I can get back to teaching.


diff ~/Downloads/TimerOne/config/known_16bit_timers.h TimerOne/config/known_16bit_timers.h 
11,22c11,34
< 
< // Teensy 2.0
< //< 
< // Teensy 2.0
< //
< #elif defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY)
<   #define TIMER1_A_PIN   14
<   #define TIMER1_B_PIN   15
<   #define TIMER1_C_PIN   4
<   #define TIMER1_ICP_PIN 22
<   #define TIMER1_CLK_PIN 11
<   #define TIMER3_A_PIN   9
<   #define TIMER3_ICP_PIN 10
< 
---
> // mega34u4
> // Ehem: it would be nice to figure out the sparkfun micro-pro
> // and the adafruit u4 stuff as well.
> #elif defined(__AVR_ATmega32U4__) 
>     // Teensy 2.0
>     #if defined(CORE_TEENSY)
>         #define TIMER1_A_PIN   14
>         #define TIMER1_B_PIN   15
>         #define TIMER1_C_PIN   4
>         #define TIMER1_ICP_PIN 22
>         #define TIMER1_CLK_PIN 11
>         #define TIMER3_A_PIN   9
>         #define TIMER3_ICP_PIN 10
>     // otherwise assume Leonardo/micro/esplora
>     #else 
>         #define TIMER1_A_PIN   9
>         #define TIMER1_B_PIN   10
>         #define TIMER1_C_PIN   11
>         #define TIMER1_ICP_PIN 4
>         #define TIMER1_CLK_PIN 12
>         #define TIMER3_A_PIN   5
>         #define TIMER3_ICP_PIN 13
>     #endif
> 

http://www.suspectdevices.com/reference/Timer1.tgz

One Response to “Using the Timer1 and Timer3 libraries with the Arduino Leonardo.”

  1. Pepf

    Thanks for the post!
    It triggered me to look up the pins for the arduino micro-pro (sparkfun variant or any other cheap knock-off of this one ;)) to make Timer 3 and 1 work as expected. I actually only tested Timer 3 because I needed it, but I looked up the pins for Timer 1 as well, so hopefully this will work for people ;)
    I took the pins from the datasheet, which you can find right here:
    http://www.atmel.com/Images/Atmel-7766-8-bit-AVR-ATmega16U4-32U4_%20Datasheet.pdf

    #define TIMER1_A_PIN 29
    #define TIMER1_B_PIN 30
    #define TIMER1_C_PIN 12
    #define TIMER1_ICP_PIN 25
    #define TIMER1_CLK_PIN 26
    #define TIMER3_A_PIN 31
    #define TIMER3_ICP_PIN 32

Leave a Reply

  • (will not be published)