Invalid pin mapping in Arduino Leonardo?

I am using an Arduino Leonardo R3 board. I work on Windows 7 Professional. I am using Arduino software 1.0.5.

I am experiencing an unexpected beahviour with the sketch that follows.

Expected behaviour I would like to have Digital Pin 1 toggling between HIGH and LOW every 2 seconds, together with the LED.

Actual behaviour The LED blinks with a 2 seconds cycle as expected. Digital Pin 1 shows a costant value of 0.18V. Digital Pin 13 toggles between HIGH (5.1V) and LOW (0V) every two seconds.

According to http://arduino.cc/en/Hacking/PinMapping32u4:

  • Digital Pin 1 is mapped to pin 21; Digital Pin 13 is mapped to pin 32;

Can you provide any explanation?

int i;

void setup() { Serial.begin(9600); i = 0; pinMode(13, OUTPUT); pinMode(21, OUTPUT); // According to http://arduino.cc/en/Hacking/PinMapping32u4 // pin 21 should correspond to Digital Pin 1 }

void loop() { int state = LOW; Serial.println(i++); if(i % 2) state = HIGH;

digitalWrite(13, state); digitalWrite(21, state); delay(2000); }

I can't remember: does the Leonardo use digital pins 0/1 for Serial output, like the Uno does? I think maybe not, because it has the software UART, but I'm not sure.

EDIT: The pin mapping diagram you linked to does show pins digital 0/1 as TX and RX, so that says to me that the answer is "yes". That's why you're having trouble, then: the Serial library is using those pins at the same time that you are. Switch to another digital pin and you should be fine.

Use the Arduino pin numbers when writing Arduino code. Use 1 for Digital Pin 1 and 13 for Digital Pin 13.

Arduino Leonardo pin 13 -> Port C bit 7, physical pin 32 Arduino Leonardo pin 21 -> Port F bit 4, physical pin 39, same physical pin as analog input pin A3

johnwasser: Use the Arduino pin numbers when writing Arduino code. Use 1 for Digital Pin 1 and 13 for Digital Pin 13.

Uhh... durr. Yeah. And that ;-)

If you want to use the analogue pins as digital on a Leonardo use the macros as they don't start at 14 like an UNO.

pinMode(14, INPUT); // works on UNO but not Leonardo
pinMode(A0, INPUT); // also works on UNO, must use this for Leonardo

Where could I find the Arduino pin number reference?

ftrotta: Where could I find the Arduino pin number reference?

Here: http://forum.arduino.cc//index.php?topic=151646.msg1138645#msg1138645

ftrotta: Where could I find the Arduino pin number reference?

The definitive reference is the pins_arduino.h file for the variant you are compiling. For Leonardo that is:

hardware/arduino/variants/leonardo/pins_arduino.h

and that says:

// ATMEL ATMEGA32U4 / ARDUINO LEONARDO
//
// D0               PD2                 RXD1/INT2
// D1               PD3                 TXD1/INT3
// D2               PD1     SDA         SDA/INT1
// D3#              PD0     PWM8/SCL    OC0B/SCL/INT0
// D4       A6      PD4                 ADC8
// D5#              PC6     ???         OC3A/#OC4A
// D6#      A7      PD7     FastPWM     #OC4D/ADC10
// D7               PE6                 INT6/AIN0
//
// D8       A8      PB4                 ADC11/PCINT4
// D9#      A9      PB5     PWM16       OC1A/#OC4B/ADC12/PCINT5
// D10#     A10     PB6     PWM16       OC1B/0c4B/ADC13/PCINT6
// D11#             PB7     PWM8/16     0C0A/OC1C/#RTS/PCINT7
// D12      A11     PD6                 T1/#OC4D/ADC9
// D13#             PC7     PWM10       CLK0/OC4A
//
// A0       D18     PF7                 ADC7
// A1       D19     PF6                 ADC6
// A2       D20     PF5                 ADC5
// A3       D21     PF4                 ADC4
// A4       D22     PF1                 ADC1
// A5       D23     PF0                 ADC0
//
// New pins D14..D17 to map SPI port to digital pins
//
// MISO     D14     PB3                 MISO,PCINT3
// SCK      D15     PB1                 SCK,PCINT1
// MOSI     D16     PB2                 MOSI,PCINT2
// SS       D17     PB0                 RXLED,SS/PCINT0
//
// TXLED            PD5
// RXLED            PB0
// HWB              PE2                 HWB