Naming conventions

The Atmel company (now owned by Microchip Technology) named many of its controllers "Atmega-xxx".

The Arduino people only call the boards equipped with Atmega2560 and Atmega1280 "MEGA".

The other ones are equipped with ATmega328, ATmega168, ATmega8. Leonardo: ATmega32u4.

When should you go for an Arduino MEGA?

The answer is easy:
  1. You need more than 6 analog inputs (o.k. there are some of the "small" ones offer 8 analog inputs)
  2. The sum of required digital and analog pins exceeds 20
  3. You need more than 30 kBytes of flash memory
  4. You need more than 2 kBytes of SRAM
  5. You need more than 1 kByte of EEPROM
    (In some cases you might complement or replace the internal EEPROM with an external one (e.g. 24LC256 located on a breadboard or by means of an SD card which requires an extra shield.)

How do you know your project exceeds the limits?

  1. counting
  2. counting
  3. just ask the compiler
  4. use the freeRam function
  5. check all read and write addresses to EEPROM

ATmega2560 or ATmega1280?

In most cases 128 kBytes of FLASH memory will be more than enough, so a smaller and hopefully cheaper ATmega1280 will do.
But where to get one? Most distributers don't offer them any more.
If you can't find one go for If you are lucky you might get that item be even cheaper than a standard Arduino UNO.

I upgraded to ATmega2560 but nothing works

First of all you have to do is select the ATmega2560 in Tools -> Board.

With some early versions of the Arduino IDE there were errors in the boards.txt file. It might help to follow this instructions:

Edit your boards.txt file and change

What are the names and numbers of my I/O pins now?

That's a good question.

When you are lucky you find it in the library, e.g. in the SD-card-Library:

  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  pinMode(10, OUTPUT);     // change this to 53 on a mega
In many cases nobody will tell you.

When you were using pin A0 as a digital pin it could be addressed by pin-14 on with the Arduino UNO. But as the ATmega has a pin-14 itself it will be a different number.

With the compiler directive #if you can write code that calculates the proper pin numbers:

void setup() {
  for (uint8_t pin=0; pin<20; ++pin) {
    // here the translation for the analog pins, 
    // which were misused as digital pins 
    int pinx = pin;
    #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
    // see also footnote below
    if (pin >= 14) pinx = pin + 54 - 14;
    // end of transformation. now pinx will be uses instead of pin 
    pinMode(pinx, OUTPUT);
    digitalWrite(pinx, HIGH);

When using A0 as a digital pin which will be the correct pin no?
Arduino Digital-Pin
UNO 14
Leonardo 18

By the way:
where will I find the additional 4 pins that are new on the Leonardo?

new digital pin on the ICSP connector controller port
15 SCK PB1
17 SS PB0


Sometimes, on the internet you will find code like this:
    #ifdef __AVR_ATmega2560__ || __AVR_ATmega1280__
or even
    #ifdef __AVR_ATmega2560__ || __AVR_ATmega1280__ || __AVR_ATmega1281__ || __AVR_ATmega2561__ || __AVR_ATmega640__
instead of
    #if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
    #if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) defined(__AVR_ATmega1281__) || defined(__AVR_ATmega640__)
This code is incorrect. Still, the gcc compiler will accept it but the preprocessor that transforms the #ifdef will ignore everything following the first or (||).

Syntax of conditional compiling:

The #ifdef directive has the following syntax:
#ifdef constant-expression newline
see also: (list of macros)

That means:
- no matter how long an expression will be it must be contained in one single line.
- only one constant expression is permitted.
After transforming the source by the preprocessor nothing will be left of all these macros.
If we add this line to our Arduino program:

the gcc compiler will give you an error message telling you it found an undeclared variable name.

contact: nji(at)