Simvik

Arduino Mega pinout

The Arduino Mega (ATmega2560) pinout: what each pin does, from GPIO, analog inputs and PWM to UART, I2C and SPI, with the notes that catch people out.

The board

  • Microcontroller: ATmega2560, 8-bit AVR
  • Clock: 16 MHz
  • Flash: 256 KB (8 KB is the bootloader)
  • SRAM / EEPROM: 8 KB / 4 KB
  • Logic level: 5 V
  • Digital pins: 54 (D0–D53), 15 with PWM
  • Analog inputs: 16 (A0–A15), 10-bit: 0–1023
  • Serial: 4 UARTs: Serial (D0/D1, shared with USB), Serial1 (D19/D18), Serial2 (D17/D16), Serial3 (D15/D14)
  • I2C (Wire): SDA D20, SCL D21 (also on the SDA/SCL pins by AREF)
  • SPI: MISO D50, MOSI D51, SCK D52, SS D53
  • External interrupts: D2, D3, D18, D19, D20, D21
  • On-board LED: D13 (LED_BUILTIN)

Pins

PinFunctionsNotes
0D0, PE0, RX0Serial receive, wired to the USB serial chip.
1D1, PE1, TX0Serial transmit, wired to the USB serial chip.
2D2, PE4, ~PWM, OC3B, INT4analogWrite() PWM from Timer3 (OC3B), about 490 Hz. External interrupt INT4: attachInterrupt(digitalPinToInterrupt(2), handler, mode).
3D3, PE5, ~PWM, OC3C, INT5analogWrite() PWM from Timer3 (OC3C), about 490 Hz. External interrupt INT5: attachInterrupt(digitalPinToInterrupt(3), handler, mode).
4D4, PG5, ~PWM, OC0BanalogWrite() PWM from Timer0 (OC0B), about 980 Hz.
5D5, PE3, ~PWM, OC3AanalogWrite() PWM from Timer3 (OC3A), about 490 Hz.
6D6, PH3, ~PWM, OC4AanalogWrite() PWM from Timer4 (OC4A), about 490 Hz.
7D7, PH4, ~PWM, OC4BanalogWrite() PWM from Timer4 (OC4B), about 490 Hz.
8D8, PH5, ~PWM, OC4CanalogWrite() PWM from Timer4 (OC4C), about 490 Hz.
9D9, PH6, ~PWM, OC2BanalogWrite() PWM from Timer2 (OC2B), about 490 Hz.
10D10, PB4, ~PWM, OC2AanalogWrite() PWM from Timer2 (OC2A), about 490 Hz.
11D11, PB5, ~PWM, OC1AanalogWrite() PWM from Timer1 (OC1A), about 490 Hz.
12D12, PB6, ~PWM, OC1BanalogWrite() PWM from Timer1 (OC1B), about 490 Hz.
13D13, PB7, ~PWM, OC0A, LEDanalogWrite() PWM from Timer0 (OC0A), about 980 Hz. Drives the on-board LED (LED_BUILTIN).
14D14, PJ1, TX3Serial3 transmit.
15D15, PJ0, RX3Serial3 receive.
16D16, PH1, TX2Serial2 transmit.
17D17, PH0, RX2Serial2 receive.
18D18, PD3, INT3, TX1External interrupt INT3: attachInterrupt(digitalPinToInterrupt(18), handler, mode). Serial1 transmit.
19D19, PD2, INT2, RX1External interrupt INT2: attachInterrupt(digitalPinToInterrupt(19), handler, mode). Serial1 receive.
20D20, PD1, INT1, SDAExternal interrupt INT1: attachInterrupt(digitalPinToInterrupt(20), handler, mode). I2C SDA for the Wire library.
21D21, PD0, INT0, SCLExternal interrupt INT0: attachInterrupt(digitalPinToInterrupt(21), handler, mode). I2C SCL for the Wire library.
22D22, PA0
23D23, PA1
24D24, PA2
25D25, PA3
26D26, PA4
27D27, PA5
28D28, PA6
29D29, PA7
30D30, PC7
31D31, PC6
32D32, PC5
33D33, PC4
34D34, PC3
35D35, PC2
36D36, PC1
37D37, PC0
38D38, PD7
39D39, PG2
40D40, PG1
41D41, PG0
42D42, PL7
43D43, PL6
44D44, PL5, ~PWM, OC5CanalogWrite() PWM from Timer5 (OC5C), about 490 Hz.
45D45, PL4, ~PWM, OC5BanalogWrite() PWM from Timer5 (OC5B), about 490 Hz.
46D46, PL3, ~PWM, OC5AanalogWrite() PWM from Timer5 (OC5A), about 490 Hz.
47D47, PL2
48D48, PL1
49D49, PL0
50D50, PB3, MISOHardware SPI MISO for the SPI library.
51D51, PB2, MOSIHardware SPI MOSI for the SPI library.
52D52, PB1, SCKHardware SPI SCK for the SPI library.
53D53, PB0, SSSPI SS: keep it an output while the board is the SPI controller, or SPI stops.
A0A0, PF0, ADC0Analog input: analogRead(A0) reads ADC channel 0. Also a digital pin: pinMode(A0, OUTPUT) and digitalWrite(A0, HIGH) work.
A1A1, PF1, ADC1Analog input: analogRead(A1) reads ADC channel 1. Also a digital pin: pinMode(A1, OUTPUT) and digitalWrite(A1, HIGH) work.
A2A2, PF2, ADC2Analog input: analogRead(A2) reads ADC channel 2. Also a digital pin: pinMode(A2, OUTPUT) and digitalWrite(A2, HIGH) work.
A3A3, PF3, ADC3Analog input: analogRead(A3) reads ADC channel 3. Also a digital pin: pinMode(A3, OUTPUT) and digitalWrite(A3, HIGH) work.
A4A4, PF4, ADC4Analog input: analogRead(A4) reads ADC channel 4. Also a digital pin: pinMode(A4, OUTPUT) and digitalWrite(A4, HIGH) work.
A5A5, PF5, ADC5Analog input: analogRead(A5) reads ADC channel 5. Also a digital pin: pinMode(A5, OUTPUT) and digitalWrite(A5, HIGH) work.
A6A6, PF6, ADC6Analog input: analogRead(A6) reads ADC channel 6. Also a digital pin: pinMode(A6, OUTPUT) and digitalWrite(A6, HIGH) work.
A7A7, PF7, ADC7Analog input: analogRead(A7) reads ADC channel 7. Also a digital pin: pinMode(A7, OUTPUT) and digitalWrite(A7, HIGH) work.
A8A8, PK0, ADC8Analog input: analogRead(A8) reads ADC channel 8. Also a digital pin: pinMode(A8, OUTPUT) and digitalWrite(A8, HIGH) work.
A9A9, PK1, ADC9Analog input: analogRead(A9) reads ADC channel 9. Also a digital pin: pinMode(A9, OUTPUT) and digitalWrite(A9, HIGH) work.
A10A10, PK2, ADC10Analog input: analogRead(A10) reads ADC channel 10. Also a digital pin: pinMode(A10, OUTPUT) and digitalWrite(A10, HIGH) work.
A11A11, PK3, ADC11Analog input: analogRead(A11) reads ADC channel 11. Also a digital pin: pinMode(A11, OUTPUT) and digitalWrite(A11, HIGH) work.
A12A12, PK4, ADC12Analog input: analogRead(A12) reads ADC channel 12. Also a digital pin: pinMode(A12, OUTPUT) and digitalWrite(A12, HIGH) work.
A13A13, PK5, ADC13Analog input: analogRead(A13) reads ADC channel 13. Also a digital pin: pinMode(A13, OUTPUT) and digitalWrite(A13, HIGH) work.
A14A14, PK6, ADC14Analog input: analogRead(A14) reads ADC channel 14. Also a digital pin: pinMode(A14, OUTPUT) and digitalWrite(A14, HIGH) work.
A15A15, PK7, ADC15Analog input: analogRead(A15) reads ADC channel 15. Also a digital pin: pinMode(A15, OUTPUT) and digitalWrite(A15, HIGH) work.
GNDGNDGround (0 V).
GNDGNDGround (0 V).
GNDGNDGround (0 V).
GNDGNDGround (0 V).
GNDGNDGround (0 V).
5V5V5 V supply: from USB, or from the regulator when the board is powered through VIN.
5V5V5 V supply: from USB, or from the regulator when the board is powered through VIN.
5V5V5 V supply: from USB, or from the regulator when the board is powered through VIN.
IOREFIOREFTells shields the I/O voltage of the board (5 V). Not a supply to draw from.
3.3V3V33.3 V supply from the on-board regulator. Keep the load small.

Good to know

  • Each I/O pin can source or sink about 20 mA (40 mA is the absolute maximum); keep the whole chip under 200 mA.
  • analogWrite() works on the pins marked ~PWM: about 490 Hz, or 980 Hz on the Timer0 pins. Timer0 also runs millis() and delay().
  • attachInterrupt() works on D2, D3, D18, D19, D20 and D21; A8–A15 and some other pins also have pin-change interrupts.
  • D0 and D1 carry Serial to the computer. Leave them free while you use the Serial Monitor.
  • The port tags give the register bit behind each pin: D13 is PB7, so PORTB |= (1 << PB7) drives it high without digitalWrite().