Hardware Reference
In-Depth Information
Variants Files
The Arduino Leonardo is a good example. The Teensy from Paul Stoffregen and the ATmega32u4 breakout board
from Adafruit both contain the same chip as the Leonardo, but have different form factors. The number of pins and
locations don't match, so creating a board variants file helps map the pins to the common locations more easily. Like
pin 13 used for blink. Pin 7 maps to an LED on the ATmega32u4 breakout board. Adding a variant file causes those
mappings to be the same. The variants file makes it much easier to create a custom board that is Arduino compatible.
These variants files are placed into a folder named after the board or the pin layout. Then, inside boards.txt ,
the variants entry needs to be added to:
boardname.build.variant=myboardpins
The files can be placed either into the application hardware/arduino/variants folder or in sketches/hardware/
myat32u4/variants .
Arduino comes with several predefined variants that support the existing Arduino boards, but this chapter will
examine the section specific to the Leonardo variants. Among the other variant folders (mega, micro, standard), there
is a new one called Leonardo . That folder contains pins_arduino.h . This is used at compile time as a reference for the
pin mappings and board-specific features.
Variant Types and Naming
The Arduino Leonardo has 12 analog inputs, but only 5 are mapped on the silk screen. However, all 12 are defined
in the variants file. This means you can use the features—even though they are not labeled—by reading the variants.
The SPI pins are not labeled, but can be accessed via the ICSP header. Here is the section where these capabilities are
defined:
I2C is defined as pins 2 and 3 on the ATmega32u4 chip, as shown in Listing 1-6.
Listing 1-6. Variant File i2C Mappings
static const uint8_t SDA = 2;
static const uint8_t SCL = 3;
SPI is defined as pins 17, 16, 14, and 15 on the ICSP header, as shown in Listing 1-7.
Listing 1-7. SPI Pin Mappings
// Map SPI port to 'new' pins D14..D17
static const uint8_t SS = 17;
static const uint8_t MOSI = 16;
static const uint8_t MISO = 14;
static const uint8_t SCK = 15;
The analog pins are defined and mapped on the ATmega32u4 to the pins shown in Listing 1-8.
Listing 1-8. Analog Pin Mappings
// Mapping of analog pins as digital I/O
// A6-A11 share with digital pins
static const uint8_t A0 = 18;
static const uint8_t A1 = 19;
static const uint8_t A2 = 20;
static const uint8_t A3 = 21;
 
Search WWH ::




Custom Search