Arduino Mega 2560 pinout: every pin, its port and its timer
Beginner
Or see every lesson.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
The Arduino Mega 2560 is the board for a project that ran out of pins: 54 digital pins, 16 analog inputs and four serial ports, on an ATmega2560 at 16 MHz. Its shield headers are an Uno’s, so an Uno shield fits, and everything past them is what the bigger chip bought. This page lists every hole with the chip pin behind it, which pins do PWM and on which timer, the four serial ports, the interrupt pins, I2C and SPI, and the power pins and their limits.
The circuit above is a Mega in Mokxi with three of its serial ports talking to themselves: each one’s TX is jumpered to its own RX, and the fourth port reports what came back. The table is read from the same board model the simulator runs, and where the simulator does less than the board, the page says so.
Every pin, with its ATmega2560 pin
The top header reads, left to right, SCL, SDA, AREF, GND, 13 down to 0, then 14 to 21. The bottom header reads IOREF, RESET, 3V3, 5V, GND, GND, VIN, then A0 to A15. The double row on the short edge is 22 to 53 in pairs, 22 and 23 first, with a 5V and a GND hole at the end. SDA and SCL beside AREF are the same two traces as pins 20 and 21, not A4 and A5 as on an Uno.
The chip pins are not in order, and that is the real board’s doing: 22 to 29 count up through port A, 30 to 37 count down through port C, and 42 to 49 count down through port L. A sketch never sees it, because digitalWrite carries the table, but one that writes PORTA itself does. A0 to A15 are also digital pins 54 to 69.
| Pin | Chip pin | What it can do | Watch out for |
|---|---|---|---|
| SCL | PD0 | I2C SCL: the same trace as 21. | Used by any I2C device. |
| SDA | PD1 | I2C SDA: the same trace as 20. | Used by any I2C device. |
| AREF | - | The analog reference input. | Not a GPIO. Never above 5 V. |
| GND | - | Ground. Several holes, one ground. | - |
| 13 | PB7 | PWM on timer 0, 976 Hz. The L LED, lit when HIGH. | Timer 0 also runs millis(). |
| 12 | PB6 | PWM on timer 1, 490 Hz. | - |
| 11 | PB5 | PWM on timer 1, 490 Hz. | - |
| 10 | PB4 | PWM on timer 2, 490 Hz. | - |
| 9 | PH6 | PWM on timer 2, 490 Hz. | - |
| 8 | PH5 | PWM on timer 4, 490 Hz. | - |
| 7 | PH4 | PWM on timer 4, 490 Hz. | - |
| 6 | PH3 | PWM on timer 4, 490 Hz. | - |
| 5 | PE3 | PWM on timer 3, 490 Hz. | - |
| 4 | PG5 | PWM on timer 0, 976 Hz. | Timer 0 also runs millis(). |
| 3 | PE5 | PWM on timer 3, 490 Hz. An interrupt pin (INT5). | - |
| 2 | PE4 | PWM on timer 3, 490 Hz. An interrupt pin (INT4). | A good first choice for a button. |
| 1 | PE1 | TX: Serial out, to the USB-to-serial chip. | Leave free, or uploads and the serial monitor break. |
| 0 | PE0 | RX: Serial in, from the USB-to-serial chip. | Leave free, or uploads and the serial monitor break. |
| 14 | PJ1 | Serial3 TX. | - |
| 15 | PJ0 | Serial3 RX. | - |
| 16 | PH1 | Serial2 TX. | - |
| 17 | PH0 | Serial2 RX. | - |
| 18 | PD3 | Serial1 TX. An interrupt pin (INT3). | - |
| 19 | PD2 | Serial1 RX. An interrupt pin (INT2). | - |
| 20 | PD1 | I2C SDA. An interrupt pin (INT1). | Used by any I2C device. |
| 21 | PD0 | I2C SCL. An interrupt pin (INT0). | Used by any I2C device. |
| IOREF | - | Tells a shield the board’s logic level: 5 V. | Not a GPIO. |
| RESET | - | Reset: low restarts the chip, as the button does. | Not a GPIO. |
| 3V3 | - | A small 3.3 V output for a sensor, 50 mA at most. | Not for a motor or a servo. |
| 5V | - | 5 V out, from USB or the regulator. Two holes. | Not a GPIO. |
| VIN | - | Power in for the onboard regulator, 7 to 12 V. | Mokxi ignores it; the board is on USB. |
| A0 | PF0 | Analog. Digital 54. | - |
| A1 | PF1 | Analog. Digital 55. | - |
| A2 | PF2 | Analog. Digital 56. | - |
| A3 | PF3 | Analog. Digital 57. | - |
| A4 | PF4 | Analog. Digital 58. | Not I2C on a Mega. |
| A5 | PF5 | Analog. Digital 59. | Not I2C on a Mega. |
| A6 | PF6 | Analog. Digital 60. | - |
| A7 | PF7 | Analog. Digital 61. | - |
| A8 | PK0 | Analog. Digital 62. A pin-change interrupt. | - |
| A9 | PK1 | Analog. Digital 63. A pin-change interrupt. | - |
| A10 | PK2 | Analog. Digital 64. A pin-change interrupt. | - |
| A11 | PK3 | Analog. Digital 65. A pin-change interrupt. | - |
| A12 | PK4 | Analog. Digital 66. A pin-change interrupt. | - |
| A13 | PK5 | Analog. Digital 67. A pin-change interrupt. | - |
| A14 | PK6 | Analog. Digital 68. A pin-change interrupt. | - |
| A15 | PK7 | Analog. Digital 69. A pin-change interrupt. | - |
| 22 | PA0 | Digital. | - |
| 23 | PA1 | Digital. | - |
| 24 | PA2 | Digital. | - |
| 25 | PA3 | Digital. | - |
| 26 | PA4 | Digital. | - |
| 27 | PA5 | Digital. | - |
| 28 | PA6 | Digital. | - |
| 29 | PA7 | Digital. | - |
| 30 | PC7 | Digital. | - |
| 31 | PC6 | Digital. | - |
| 32 | PC5 | Digital. | - |
| 33 | PC4 | Digital. | - |
| 34 | PC3 | Digital. | - |
| 35 | PC2 | Digital. | - |
| 36 | PC1 | Digital. | - |
| 37 | PC0 | Digital. | - |
| 38 | PD7 | Digital. | - |
| 39 | PG2 | Digital. | - |
| 40 | PG1 | Digital. | - |
| 41 | PG0 | Digital. | - |
| 42 | PL7 | Digital. | - |
| 43 | PL6 | Digital. | - |
| 44 | PL5 | PWM on timer 5, 490 Hz. | - |
| 45 | PL4 | PWM on timer 5, 490 Hz. | - |
| 46 | PL3 | PWM on timer 5, 490 Hz. | - |
| 47 | PL2 | Digital. | - |
| 48 | PL1 | Digital. | - |
| 49 | PL0 | Digital. | - |
| 50 | PB3 | Digital. SPI MISO, also on the ICSP header. A pin-change interrupt. | - |
| 51 | PB2 | Digital. SPI MOSI, also on the ICSP header. A pin-change interrupt. | - |
| 52 | PB1 | Digital. SPI SCK, also on the ICSP header. A pin-change interrupt. | - |
| 53 | PB0 | Digital. SPI SS. A pin-change interrupt. | - |
Power pins and their limits
VIN feeds the onboard 5 V regulator, as the barrel jack does, and Arduino recommends 7 to 12 V there. 5V is the regulated rail, on the bottom header and again at the end of the double row. 3V3 is a small output rated at 50 mA, for a sensor. IOREF is 5 V and tells a shield the board’s logic level. All the GND holes are one ground.
Arduino rates each pin at 20 mA, and the ATmega2560’s absolute maximum is 40 mA per pin. Fifty-four outputs is more LEDs than the regulator wants to feed at once, so a big display wants its own supply. In Mokxi the board is always powered from USB, VIN is an input it ignores, and the 5 V rail is an ideal source that cannot sag.
PWM pins and their timers
Fifteen pins do PWM with analogWrite: 2 to 13 and 44 to 46. Pins 4 and 13 are on timer 0 at 976 Hz, which also runs millis(); the rest are at 490 Hz on timers 1 to 5, three pins to a timer except timers 1 and 2, which have two each here. Mokxi runs every one on the same timer at the same rate as the board.
Arduino’s Servo reference says a Mega drives up to twelve servos without touching PWM, and that more than twelve stop PWM on 11 and 12. That is one more thing a Mega does differently from an Uno, where the first servo costs 9 and 10.
Four serial ports
Serial is pins 0 and 1, through the ATmega16U2 to the USB socket, as on an Uno: leave them free for uploads. Serial1 is 18 (TX) and 19 (RX), Serial2 is 16 and 17, and Serial3 is 14 and 15, each a hardware UART of its own, so a GPS module, a Bluetooth module and a second board can all have one.
In Mokxi Serial reaches the serial monitor directly and pins 0 and 1 stay plain pins. Serial1 to Serial3 drive their pins with real frames at the baud rate you set, which is what the circuit above shows: change one port’s rate in the sketch and its line comes back garbled, as on the bench.
Interrupts, I2C, SPI and the analog pins
attachInterrupt works on 2, 3, 18, 19, 20 and 21. The interrupt numbers are not the chip’s INT numbers: interrupt 0 is pin 2, which is INT4, and interrupts 2 to 5 are pins 21 down to 18, which are INT0 to INT3. Write digitalPinToInterrupt(pin) and let the table do it. Pin-change interrupts, which SoftwareSerial needs to receive, are on 10 to 15, 50 to 53 and A8 to A15 only.
I2C is 20 for SDA and 21 for SCL, which the SDA and SCL holes beside AREF also reach. SPI is 50, 51, 52 and 53 for MISO, MOSI, SCK and SS, and the ICSP header carries the same three data lines. In Mokxi both buses run in software on those pins, because the chip’s I2C and SPI blocks are not modeled. A0 to A15 read ten bits against the 5 V rail, and the Mega has an internal 2.56 V reference an Uno does not.
Questions
Which Arduino Mega pins are PWM?
2 to 13 and 44 to 46, fifteen in all. 4 and 13 run at 976 Hz and the other thirteen at 490 Hz.
What are the Arduino Mega I2C pins?
20 is SDA and 21 is SCL, also brought out as SDA and SCL beside AREF. Not A4 and A5: an Uno sketch or shield that expects I2C there has to move.
Which pins are SPI on an Arduino Mega 2560?
MISO is 50, MOSI is 51, SCK is 52 and SS is 53. MISO, MOSI and SCK are on the ICSP header as well.
Which Mega pins can I use for interrupts?
2, 3, 18, 19, 20 and 21. 18 and 19 are also Serial1 and 20 and 21 are I2C, so 2 and 3 are the ones that are usually free.
Which pins are Serial1, Serial2 and Serial3 on a Mega?
Serial1 is 18 (TX) and 19 (RX), Serial2 is 16 and 17, and Serial3 is 14 and 15. Cross them over to a module: its TX to the Mega’s RX.
Which Mega pin should I use for a button?
Any of 22 to 53, with `INPUT_PULLUP` and the button to ground, or 2 or 3 if it needs an interrupt.
Keep going
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.