Arduino®-Leonardo Introduction & Set-up Tutorial

 

NOTE: Arduino 1.01 and the Arduino Leonardo have been released! We’ll be updating these pages soon to reflect the release version of the Leonardo features. The most relevant is there’s no need to do the “uncomment” process since Leonardo is part of the boards list in Adruino 1.01.

This is the first of a series of tutorial pages on how to set-up the new Arduino Leonardo, and compatible, boards.

The Arduino “Leonardo” platform uses the new USB-enabled ATmega 32U4 MCU, with a very powerful bootloader environment that emulates both a USB mouse and keyboard (standard HID devices, no drivers needed), as well as a virtual COM port!

It’s also completely code-compatible with Arduino sketches, as long as you don’t start getting into the weeds, messing with hardware timers or any other hardware-specific modifications! (the same goes for the Mega series, which also use a completely different microcontroller than the basic ATmega328 in the Uno or Duemilanove)

Along with the emulated USB HID devices, another really cool feature of the Leonardo is that it effectively has two COM ports!  One is the emulated VCP port through the USB connection, the other is the hardware UART on the microcontroller itself.  You can independently “talk” to two separate devices; one via USB, the other via serial or Bluetooth, for example.

Arduino 1.0 includes completely new commands for the Leonardo such as “Mouse.move(xReading, yReading, 0);” and “Keyboard.write(inChar);”, which moves the mouse and types keyboard characters. Very neat for PC interface projects! (note that these new commands do nothing on the UNO, Duemilanove, or older Arduino boards…)

At the moment, the Leonardo codebase in Arduino 1.0 is still considered to be in “beta” development.  Arduino 1.0 Release Candidate 2 (RC2) had the Leonardo directly available as board option, but has since been temporarily commented out while the Arduino IDE is optimized for the ATmega 32U4.  Regardless, it still works fine for test and development purposes.

If you have not yet installed Arduino, please refer to the Arduino Programming IDE Installation page first, then come back here!

To enable Leonardo support, one needs to simply un-comment the Leonardo section in the “arduino-1.0\hardware\arduino\boards.txt” file.  A pre-configured boards.txt file is offered below this example, or just open the file in Notepad++ or something similar, and look for the following entry:

##############################################################

#leonardo.name=Arduino Leonardo
#leonardo.upload.protocol=arduino
#leonardo.upload.maximum_size=28672
#leonardo.upload.speed=1200
#leonardo.bootloader.low_fuses=0xde
#leonardo.bootloader.high_fuses=0xd8
#leonardo.bootloader.extended_fuses=0xcb
#leonardo.bootloader.path=diskloader
#leonardo.bootloader.file=DiskLoader-Leonardo.hex
#leonardo.bootloader.unlock_bits=0x3F
#leonardo.bootloader.lock_bits=0x2F
#leonardo.build.mcu=atmega32u4
#leonardo.build.f_cpu=16000000L
#leonardo.build.core=arduino
#leonardo.build.variant=leonardo

##############################################################

To “activate” the Leonardo in the Arduino Board menu, just remove the number signs(#) in front of each line starting with “leonardo.”, and save the file.  If you like, you can move the whole Leonardo entry to the top of the file, and it will conveniently appear first in the Arduino Board menu.

If you prefer, here is an already-modified boards.txt file which can simply be copied over the old one: boards.txt

Knowing how to work with the boards.txt file is actually quite handy since it allows you to re-order, or more importantly remove, items in the Board list.  In most cases, you won’t need the myriad of boards listed, which are just cluttering up the list.  Just make a back-up copy of the original boards.txt, and then delete the entries you know you won’t need. (the baseline “Duemilanove with ATmega 328″ is a good one to keep for most compatible boards)


Moving on (we’ll assume you have Arduino 1.0 installed), when you first connect a Leonardo-compatible board via USB, your PC will ask you to load some drivers.  The drivers are located in the “\arduino-1.0\drivers\” directory.  Point the installer to that directory, and follow the OS prompts.  Please note that with Windows XP, service pack 3 is required for the drivers to work correctly.

Every time thereafter when you plug a Leonardo board in, or upload a new sketch, your system will add three new devices to the system profile; a USB mouse, USB keyboard, and a COM port.  Conversely, when you remove the board, or reset it (ie upload), the three devices will be (temporarily) removed.  This is why you’ll get the Windows new USB-device “bing-bong” sound every time you upload a new sketch.

If you have a StealthDuino! Leonardo, you can find specific code examples on the bottom of the product page, here: http://cal-eng.com/?wpsc-product=stealthduino-atmega-32u4-leonardo

If you have a BreadBoarder 32U4 Leonardo, there’s a few code examples on the bottom of the product page as well: http://cal-eng.com/?wpsc-product=breadboarder-32u4

If you’d like to see examples of how the HID mouse and keyboard sketches work, follow this link to the: Arduino®-Leonardo HID Keyboard & Mouse Tutorial and Advanced Examples

Comments are closed.