Arduino’s latest incarnation – the Leonardo is now shipping. It is the first Arduino to use Atmel’s ATmegaXU4 series chip with built-in USB. This change is big and it has big benefits.
Early Arduinos required a serial port connection to your computer for programming. As the platform matured, the board acquired a USB to serial conversion chip. The latest version of the classic Arduino board – the Uno – still uses this method, although with the Uno a switch was made from an expensive FTDI conversion chip to using an ATmegaXU2 series microcontroller chip. This chip is a cousin to the U4 series, but lacks analog input pins.
Using a USB conversion chip was only a slight improvement over using a serial connection. It removed the requirement for a special conversion cable, but added significant cost to each and every board. With the Uno, the switch to using the ATmeag8U2 lowered the cost by a few dollars, but it seems kind of silly to use an entire microcontroller just to perform USB to serial conversion for another microcontroller of roughly the same capability.
This ironic situation is finally resolved with the introduction of the Leonardo. Not only does this AVR chip offer built-in USB connectivity, but it has many other benefits as well. This newest addition to the Arduino is not only cheaper but packs several additional features. I will be very surprised if the Uno continues in production much longer. I see no need for it at all – cheaper and better wins every time.
Without further ado, here are the differences and advantages of the Leonardo over classic Arduino boards such as the Uno and Duemilanove.
More Digital Pins
There are 4 extra digital pins. The Arduino Uno has 14 digital pins – D0-D13. On the Leonardo, these same 14 pins correspond to the same header locations along the side. To gain access to the additional four pins, the first three have been mapped to the ICSP header. Instead of the ICSP pins corresponding to pins D11-D13, they correspond to the first three new extra pins D14-D17. The specific mappings are:
D17 pin attachment points
D14 – MISO – PB3
D15 – SCK – PB1
D16 – MOSI – PB2
D17 – SS – PB0
The last pin, D17 (SS), does not have a corresponding pin on the ICSP header, nor anywhere on the board. In order to use it, you must solder a wire to either the end of the Rx LED or the via attached to it. This is the cathode end of the LED. See the photo for the attachment points.
More and Better PWM Pins
While only one extra PWM pin is available (the Leonardo has 7, the Uno has 6), the timers on the ATmegaXU4 are considerably more capable. On the ATmegaXU4 there are: one 8-bit timer, two 16-bit timers, and one 10-bit high speed (64MHz) timer. There are a total of 9 pins supporting independent timer output, 2 of the pins are shared, leaving 7 pins total for PWM.
Details for each timer are as follows:
- #bits = 8
- Clock speed = 16 MHz (system clock speed)
- Default prescale = 64
- Pins D3 & D11
Timers 1 and 3
- #bits = 16
- Clock speed = 16 MHz (system clock speed)
- Default prescale = 64
- Pins D5, D9 & D10
- Pin D11 can also be set to use timer 1 output (OC1C), but the Arduino library uses it for timer 0 (8-bit). To use it on timer 1, it must be done by setting the appropriate registers.
- #bits = 10
- Clock speed = 64 MHz
- Default prescale = 64
- Pins D6, D13
- Pin D10 (PB6) also has a timer 4 output (OC4B), but the Arduino library uses it for timer 1 (16-bit). To use it on timer 4, it must with the registers.
Of course as with the timers on other Arduinos, the PWM speeds and types can be customized by setting the appropriate registers. This tutorial can be very helpful if you are new to customizing the timers in AVR chips.
More Analog Pins
In the ATmegaxx8 based Arduinos, such as the Uno, there 6 analog input pins that can also be used as digital pins. In the Leonardo, there are a whopping 12 analog input pins which can also be used for digital pins. In order to maintain compatibility with existing Arduino shields, analog pins A6-A11 are located among the digital pins. They are marked with a dot on the top side of the board with the numbering drawn on the backside. The correspondence with their digital counterparts is as follows:
- A6 – D4
- A7 – D6
- A8 – D8
- A9 – D9
- A10 – D10
- A11 – D12
The original 6 analog input pins have new digital pins mappings as well. They are:
- A0 – D18
- A1 – D19
- A2 – D20
- A3 – D21
- A4 – D22
- A5 – D23
In order to use these analog pins, you must of course forgo using them as digital pins. However, since you also get four new digital pins, the trade-off costs very little, and is up to you how to allocate them.
The pins supporting I2C/TWI used to be the same as analog pins A4 & A5. In order to use this service, you had to forgo two precious analog pins. In the Leonardo, these protocol pins are now located at digital pins D2 & D3. They also still have their own special place in the output headers just like the Uno.
Serial Port Differences
The standard serial port at pins D0 (Rx) and D1 (Tx) have become a second serial port. The primary serial port is implemented as part of the USB interface and uses no digital pins. The Rx & Tx LEDs are tied to the first serial port and are not active when using the second one. In software, the primary serial port is still accessed using the object Serial. To access the second serial port, use a new object called Serial1 in the same manner.
There are also some differences in how the primary serial port acts when attached to your computer. Formerly, when the serial port was implemented in hardware, resetting the Arduino had no effect at the computer end. Now, the serial port is a creation of software. Therefore, when the Arduino is reset, the connection is broken and a new serial port is established. The Leonardo documentation provides details:
Since the Leonardo does not have a dedicated chip to handle serial communication, it means that the serial port is virtual — it’s a software routine, both on your operating system, and on the Leonardo itself. Just as your computer creates an instance of the serial port driver when you plug in any Arduino, the Leonardo creates a serial instance whenever it runs its bootloader. The Leonardo is an instance of USB’s Connected Device Class (CDC) driver.
This means that every time you reset the board, the Leonardo’s USB serial connection will be broken and re-established. The Leonardo will disappear from the list of serial ports, and the list will re-enumerate. Any program that has an open serial connection to the Leonardo will lose its connection. This is in contrast to the Arduino Uno, with which you can reset the main processor (the ATmega328P) without closing the USB connection (which is maintained by the secondary ATmega8U2 or ATmega16U2 processor). This difference has implications for driver installation, uploading, and communication; these are discussed below.
The other key difference with the virtual serial port is that when you open the serial monitor in the Arudino IDE, it doesn’t trigger a reset on your board. This change means you won’t see serial data that has already been sent since your sketch started. Again from the Leonardo guide:
Unlike the Arduino Uno, the Leonardo doesn’t restart your sketch when you open its serial port on the computer. That means you won’t see serial data that’s already been sent to the computer by the board, including, for example, most data sent in the setup() function.
This change means that if you’re using any Serial print(), println() or write() statments in your setup, they won’t show up when you open the serial monitor. To work around this, you can check to see if the serial port is open like so:
// while the serial stream is not open, do nothing:
while (!Serial) ;
One last change in serial behavior is the fact that it is a lot faster. If you used to rely on its slow speed to throttle writes to the monitor, you may need to add some delay statements to do that for you now.
Keyboard and Mouse Emulation
Now that the Leonardo supports USB directly, that means it can operate as an actual USB device. Two of the tricks it can do is to emulate either a computer mouse or a keyboard. It can even emulate both. The Arduino library has made it really easy to do by creating a Mouse class and a Keyboard class. This page provides complete documentation, but here are some highlights:
- Mouse.move – move the cursor by an X,Y amount, and even move the scroll wheel
- Mouse.click – simulate a click
- Mouse.press – press the button down and hold until release is called
- Mouse.release – release a mouse press
- Keyboard.press – simulate the press of a certain key until released; can be used to press a modifier key while typing another character
- Keyboard.release – release a key that has been pressed
- Keyboard.write – simulate a single character being typed
- Keyboard.print – simulate the typing of an entire string
Before using either of these emulation classes, you must use caution. If you do not have a way of turning them off, the Leonardo could prevent you from reprogramming it by sending keystrokes and mouse movements/clicks all the time. The Leonardo Guide has some guidelines to prevent such a mishap. Basically, you need some hardware means like a switch to shut off emulation. If you fail to provide for such a means beforehand, you may have to use a ICSP programmer to reflash your Leonardo – be sure to exercise care before playing with this new power!
USB CaveatsThere are some things to be aware of when converting over to using the Leonardo. First you must upgrade your Arudino IDE to version 1.01. It has a lot of great improvements and implements support of the Leonardo. Second, unless you are using Linux, you will need to install USB drivers for the Leonardo. The Leonardo guide provides directions.
The Leonardo is a great addition to the Arduino family. It sports more pins, faster PWM, lots of analog input pins and best of all true USB. On top of all that, it is cheaper than its predecessors. If you are in need of a new Arduino, I wouldn’t consider anything else.
[UPDATE: With the lower price of the Uno version of the Arduino, I now recommend it over the Leonardo in most cases, due to the USB reset complications of the latter. See the comments for more information.]
If you have any experiences or tips of your own to share, please do so in the comments below.