The overview, with examples running, is on the board page and The Arduino Leonardo model (every register).
What it is
An ATmega32U4 in its 44-pin package, on the same AVR core as the Uno, with 32 KB of flash, 2560 bytes of SRAM and 1 KB of EEPROM. The registers are at their real addresses, so a sketch built here runs on the board on your desk. The reference page is The Arduino Leonardo model.
The board is 68.6 by 53.3 mm (an Uno's outline to a tenth of a millimeter), and the shield headers are an Uno's hole for hole. Like an Uno it does not go into a breadboard: wire it with jumpers from the headers.
Serial is the USB
This is the thing to understand about a Leonardo, and the thing that surprises people who arrive from an Uno.
On an Uno a second chip turns USB into a UART, and Serial is the
microcontroller's USART0. On a Leonardo there is no second chip: the USB device
is on the ATmega32U4 itself, and Serial is a CDC endpoint that the sketch's
own firmware opens. Serial1 is the real USART, and it comes out on header pins
0 and 1.
So in the editor:
- What you print to
Serialappears in the serial monitor. - What you print to
Serial1comes out of pin 1 as bits. Wire it to something (pin 0, a scope, another board) or it reaches nothing at all.
The built-in Serial program prints to both, so the difference is on screen.
Mokxi models the USB as a byte pipe at the real endpoint registers: enough
for a serial port, and nothing above it. There is no enumeration, no
descriptors and no host, so while (!Serial) {} returns at once here and waits
on a desk.
Keyboard.h, Mouse.h and Joystick.h build and run. With no computer on
the other end of the cable, each press, move or button change is printed to
the serial monitor as a line instead ([Keyboard] press Ctrl). On the board
on your desk the same program is a real USB keyboard, mouse or game
controller.
The program Mokxi builds carries a real USB serial device for the board on
your desk. Sent to a Leonardo, a Micro or a Pro Micro, it shows up as a serial
port, Serial reaches the Arduino IDE's serial monitor or any terminal,
while (!Serial) waits for one to open the port, and Upload can restart it
into its bootloader the way the Arduino IDE does.
Pins
Twenty on the shield headers and three on the ICSP header, numbered the way the silkscreen and the Arduino core number them:
0to13along the digital header, with theLLED on 13.A0toA5along the bottom.14,15and16on the ICSP header:MISO,SCKandMOSI(below).A6toA11are second names for digital pins 4, 12, 6, 8, 9 and 10, because on this chip the ADC reaches ports B and D as well as port F.
Seven pins do hardware PWM with analogWrite: 3, 5, 6, 9, 10, 11 and 13.
SDA and SCL are pins 2 and 3, not A4 and A5 as on an Uno. The two
holes beside AREF are the same metal as pins 3 and 2. That is the one place
the shared shield footprint is not the shared chip, and it is the first thing to
check when a sketch moves across.
SPI is only on the ICSP header, the 2x3 block of pads at the right edge,
as on the real board. Its MISO, SCK and MOSI pads are pins 14, 15 and 16,
and its other three pads are a second 5V, RESET and GND. Pins 11, 12 and
13 are not SPI on a Leonardo, so an SPI module wired there as on an Uno does
not answer: move its three bus wires to the ICSP pads.
A timer nothing else here has
Timer 4 on this chip is ten bits wide, its TOP is a register rather than a
mode, and its compare channels are A, B and D. There is no channel C,
because OCR4C is TOP. It drives pins 6 and 13.
analogWrite hides all of that, so the sketch looks ordinary. The built-in
Fade program puts an LED on pin 13 and another on pin 9, which is an
ordinary 16-bit timer, so both kinds of PWM hardware are running at once: 976 Hz
on 13 and 490 Hz on 9.
Interrupts
Five pins have an external interrupt: 3, 2, 0, 1 and 7. Pin 7 is the
interesting one. It is INT6, the only one this board brings out that is not
one of the first four pins, and the built-in Button program uses it.
Pass a pin through digitalPinToInterrupt() and attachInterrupt() does the
rest, as on the real board.
Stock Arduino libraries
A tutorial that includes Servo.h, Wire.h, SPI.h, EEPROM.h,
LiquidCrystal_I2C.h, Adafruit_GFX.h with Adafruit_SSD1306.h and DHT.h
compiles here as it stands. Each is Mokxi's own header under the upstream name,
written on the drivers for the parts in the bin, and none of it is the upstream
library's code.
Wire.begin()puts the I2C bus on2(SDA) and3(SCL).SPI.begin()puts the SPI bus on the ICSP header:SCKis 15,MISO14 andMOSI16.SSis 17, theRXlamp's pin, which has no pad, so give the module's chip select any free pin.EEPROM.huses the chip's own 1 KB of EEPROM, through its real registers. It keeps what you wrote across a press of the board's reset, and a fresh Run starts it erased, every byte255, because a Run is a new board.Adafruit_NeoPixel.hdrives a WS2812 strip. The bit is timed in AVR assembly, the way every AVR NeoPixel library does it, and at 16 MHz each high lands inside the data sheet's window. Interrupts are off while a frame goes out, somillis()loses a tick for every millisecond a long strip takes to send, as on the bench. The colors take three bytes an LED of the chip's 2560 bytes of RAM.
The code editor and compiling lists what each one covers and how it differs from the upstream library.
The firmware
Four built-in programs: Blink, Button, Serial and Fade.
An Uno sketch's source will usually build for the Leonardo. An Uno's compiled ELF will not run on it: different chip, different register map, different vector table. Build it for the Leonardo and it runs.
What is not modeled
The USB stack above the byte pipe: Keyboard, Mouse and Joystick print
their reports to the serial monitor rather than reach a computer. Serial here is a byte pipe with no protocol under it: no
enumeration, no descriptors, no endpoint configuration, no suspend or resume and no
1200-baud-touch reset, so the thing that makes a Leonardo a Leonardo (a sketch waiting
on while (!Serial)) is a decision this model makes rather than a handshake it runs.
SPI, TWI/I2C, the analog comparator, the temperature sensor, and the PLL that would clock timer 4 faster than 16 MHz. Shields are not physical parts here; wire the same circuit on the breadboard. The Leonardo ETH is not modeled. The Micro and the Pro Micro are the same chip on other pinouts, and are in the parts bin as boards of their own.
The addresses are the data sheet's and none is guessed at; the approximations are behaviors, and section 8 of the Arduino Leonardo model sorts every one of them. The three that show at the bench: the ADC has no error in it (no sample-and-hold, no nonlinearity, no noise, no offset), so a divider reads the same count every time where a real one wobbles; the 16 MHz crystal is exact, with no drift or jitter; and the 5 V rail is an ideal source that cannot sag, so a circuit that would brown a real board out runs cleanly here.
Related
- The board page: /boards/leonardo
- The contract document: the Arduino Leonardo model
- The same chip on a breadboard: the Arduino Micro and the Pro Micro
- Starter project: Leonardo fade