Soc ninja title

Programming an ATMEGA2560 with an Arduino UNO/Mega

How to program an ATMEGA2560 with an Arduino UNO/Mega

Hi. This is just a reference guide that i made motivated by the lack of clear information around Internet in the matter of programming an stand-alone Atmel ATMEGA chip different from the "standard" ATMEGA 328P-PU found in the Arduino UNO board.

While it is true that you can find all this information in other websites ( i will try to mention all of them here, too ), none of them clearly explain, step by step, how to do it without other external hardware programmers or special software. So, here we go.

Hardware requirements

  • The chip, in this case an ATMEGA 2560, with the basic components for it to work: a 16Mhz oscillator and two 22pF capacitors. Optionally and also usually recommended, a 10K resistor for the reset pin.
  • An Arduino UNO or Arduino Mega. Will work with other boards, but i only have those ones.

Software requirements

  • Arduino IDE
  • A text editor

Wiring

The wiring is pretty simple and is already explained in the ArduinoISP sketch. Apart from the oscillator ( connected to XTAL1 and XTAL2 ) , capacitors (connected to each XTAL and to the GND ) and resistor ( connected to the RESET and VCC ), connect the following depending on the board ( UNO or Mega )

Name ATMEGA 2560 PIN To Arduino UNO PIN Or Arduino Mega PIN
Reset 30 => 10 / 53
MOSI 21 => 11 / 51
MISO 22 => 12 / 50
SCK 20 => 13 / 52
GND GND => GND / GND
+5V VCC => VCC / VCC

Programming the bootloader

The first step is clear. Assuming that our ATMEGA2560 is empty, we will program the Arduino bootloader. This step is, in fact, optional, since the chip could be programmed with our sketch directly, but the Arduino bootloader eases the process of reprogramming by using only the serial RX/TX ports.

Start the Arduino IDE program. Connect just the Arduino UNO / Arduino Mega, and load the File -> Examples -> ArduinoISP sketch.

Select the appropiate board under Tools and the default AVR ISP mkII as programmer. Select the serial port and finally compile and upload the sketch as you would normally do.

Now that we programmed our Arduino with the ArduinoISP, it's time to program our litlle chip.

Disconnect the Arduino and proceed to connect the previous wiring PINs. Then reconnect the Arduino board.

Find the file called "boards.txt" located in your Arduino installation directory. In my case, the path is "arduino-1.6.5/hardware/arduino/avr/boards.txt". Open it with your favourite text editor and locate the ATMEGA2560 section.

The key line is the following:

mega.menu.cpu.atmega2560.bootloader.high_fuses=0xD8

We have to replace the value of "0xD8" with "0xD9". This is so, because in stand-alone ATMEGA2560 chips, if the BOOTRST fuse is not set, the chip will be correctly programmed, but the programs will never run. So the line could look like this ( we can keep the original line commented ):

# mega.menu.cpu.atmega2560.bootloader.high_fuses=0xD8
mega.menu.cpu.atmega2560.bootloader.high_fuses=0xD9

Save the changes and now, in our Arduino IDE program, select the Arduino Mega 2560 board and processor, the Arduino as ISP programmer and finally, from Tools, choose Burn the bootloader. If everything is done correctly, the process will take some seconds and will finish with no errors. If you see some errors, i always like having the compilation and upload errors shown ( you can enable the output in the program's preferences ).

Programming sketches

Here we have two options: we can keep the previous wiring and configuration, and upload the sketch using the "Upload the sketch using a programmer" option under the "File" or "Tools" menu, depending on the version.

Or we can just use the RX, TX and RST wiring and upload the sketch as if it was a normal Arduino Mega board, selecting the AVRISP mkII programmer, Arduino Mega 2560 board and programming it.

I haven't tried the second method, but it might work if you first remove the ATMEGA328P-PU from the Arduino UNO board ( otherwise, it will be programmed ). Ideally you should use an external programmer compatible with the SKT500v2 protocol connected to the RX, TX and RST pins.

Tips and problems

Some people, and myself, reported having problems when using the same 5V power source from the Arduino. If you get problems about the programmer or board not responding, recheck the wirings and try using another power source for the chip ( battery, another Arduino, etc. ).

I keep receiving comments describing the situation where the sketch still doesn't run after following the instructions. Please enable the verbose mode (enable the checkboxes in the Preferences dialog for compiling and uploading) in Arduino, and check again the fuse settings applied correctly. You'll see them in avrdude command output. If it doesn't show the correct fuse value, you either incorrectly edited the boards.txt file or you edited the wrong file.




If you found anything useful enough and you want to thank us for that, please consider donating to people who need it, like the NGO OXFAM. Thank you !


Comments

Comments are manually approved. Just be a bit patient :-)

ME 1 year, 9 months ago

Very helpful post. The bit about setting the hfuse bit so that code will execute solved my problem! Thanks again.

Link | Reply
Current rating: 1

soc 1 year, 9 months ago

Glad that you found it useful. Thanks for the feedback :-).

Link | Reply
Current rating: 2

manicure 9 months, 1 week ago

WOW just what I was looking for. Came here by searching for manicure

Link | Reply
Currently unrated

BalloonGuy 1 year, 9 months ago

Hello,

Thank you very much for this tutorial, there are very few out there that talk about programming an ATMEGA2560 standalone chip.

Like you stated, I've used ICSP to upload code to my ATMEGA2560 chip (I'm using it with an 8 MHz external crystal to get Vcc to 3.3 V). However, like you said above, if I don't change the boards.txt fuse to 0xD9, it looks like the program is uploading but nothing is happening (I'm uploading a simple blink function). However, when I try to change the boards.txt file, I get the following error:

"
Property line '?# See: http://code.google.com/p/arduino/wiki/Platforms' in file C:\Program Files (x86)\Arduino\hardware\arduino\avr\boards.txt is invalid

Error compiling for board Arduino/Genuino Uno.
"
(I'm using the Uno as the programmer). Do you have any idea why this is happening and any suggestions to go about this?

Thanks again!

Link | Reply
Currently unrated

soc 1 year, 9 months ago

Hi BaloonGuy,

Thanks for the feedback.
Uhm, the only thing that comes to my mind is that maybe the format of the file must be Unix type line endings.
What editor did you use? Try with Notepad++ or similar (restore the original boards.txt first, from a reinstallation i.e).

I only work with Mac and Linux.

Link | Reply
Current rating: 1

BalloonGuy 1 year, 9 months ago

Thanks for the quick update! I use Windows, I'm not sure if that is the reason for the issue. I use Notepad to edit the text, and didn't change anything else. Interestingly enough, when I try to change it back to the default it still gives me the same error. Could it be the OS?

Link | Reply
Currently unrated

soc 1 year, 9 months ago

Hi again,

Please, try what i told you. Use a better editor. Look for Notepad++. Then edit the original file, since it seems like you somehow truncated the original boards.txt file (using a bad text editor, like notepad.exe).

Link | Reply
Currently unrated

MMICh 8 months ago

This solution - using notepad++ to change the boards.txt file has worked for me. Thanks SOC for suggestion!

Link | Reply
Currently unrated

soc 8 months ago

Glad it worked for you, too :-)

Link | Reply
Currently unrated

drsdre 1 year, 6 months ago

Unfortunately in my case the bootloader burning fails with:
avrdude: Yikes! Invalid device signature.
Double check connections and try again, or use -F to override this check.

I need to reflash the bootloader as during a direct sketch upload it failed and now the Mega2560 is more or less dead in the water.

Any tips?

Link | Reply
Currently unrated

soc 1 year, 6 months ago

Hi,

What's the full output message? Does it compare the device signature against 0x000000 i means that either the chip is broken (not likely) or not working properly (double check the wiring, and the oscillator).

Link | Reply
Currently unrated

Ravi 1 year, 5 months ago

Thanks for the post. Actually it is working for me. As usual it burns bootloader. uploads sketch. but does not run. I even tried this on the arduino mega board in case if there is a fault in my home made atmega2560.

Link | Reply
Currently unrated

soc 1 year, 5 months ago

Did you check that the fuse is changed and working (i.e. enable debug info, or use avrdude manually):

mega.menu.cpu.atmega2560.bootloader.high_fuses=0xD8

Try with the simple blinking LED sketch.

Link | Reply
Currently unrated

Ravi 1 year, 5 months ago

Hi Thanks for your quick response. Yes I changed that 8 into 9 using notepad++. The blink sketch uploaded on the mega doesnt blink. Even i have burn boot loader again to make it usable.Thanks

Link | Reply
Currently unrated

Ravi 1 year, 5 months ago

Hi,SOC,
It is perfectly working now.! May be i might have loose wiring before.
My home made ATmega2560 is also working! I am so happy. Thank you so much for your help!

Link | Reply
Currently unrated

soc 1 year, 5 months ago

So glad you managed to make it work :-).

Link | Reply
Currently unrated

Ana Clara 1 year, 4 months ago

Hi , mine is not working, this is the erro

"avrdude: Yikes! Invalid device signature.
Expeted signature 1E 98 01
Double check connections and try again, or use -F to override
this check."
Any guess ?

Link | Reply
Currently unrated

soc 1 year, 4 months ago

Hi Ana,

Can you enable the verbose output and check if the detected device signature is 0x000000 ?
If so, the common problems are:

- Wiring: double-check all the connections, oscillator, etc.
- Power source: use an external power source, NOT the same from the Arduino.

Good luck, and tell us.

Link | Reply
Current rating: 5

Nicolaas 1 year, 4 months ago

Hi, thanks so much for this tutorial!
I have an ATMega2560 on a custom PCB board and I connected everything just like you explained and changed the fuse setting. I tried uploading a blink but it won't work.. Since everything is on a PCB, also the LEDs loose wiring is not the case.

Link | Reply
Currently unrated

soc 1 year, 4 months ago

Hi Nicolaas,

The comments are moderated. I approved both but will remove the second one if you want.

I need more details to help you. What does it mean "id doesn't work"? Does it program succesfully but then doesn't work, or it gives error when programming,...?

Link | Reply
Currently unrated

Nicolaas 1 year, 4 months ago

Yes you can delete the second one. On the ide it says it has successfully uploaded the code but then it doesn't show on the board

Link | Reply
Currently unrated

soc 1 year, 4 months ago

I see. Does the verbose output show that the fuse setting was effectively applied? If the programming process is OK, the only thing i know that can fail is the fuse setting...

Link | Reply
Currently unrated

Nicolaas 1 year, 4 months ago

I checked the verbose output, for some reason it was ignoring the change in the board.txt file. I switched from my Mac to a Windows computer and repeated the process. It finally works!! Thanks so much!

Link | Reply
Currently unrated

soc 1 year, 4 months ago

It was probably using another conf file. Glad to know you made it work finally.

Link | Reply
Currently unrated

Paul 1 year, 3 months ago

Hi,

Thank you very much for this post. It worked for me and I managed to blink a led using the ATMEGA programmed with the Arduino IDE.
Now, I try to figure out how to do the same using Eclipse and the Arduino plugin (Sloeber) ... But it seems like the plugin doesn't support an upload from a programmer. Do you know if there is a workaround or am I stuck with the Arduino IDE ?

Link | Reply
Current rating: 5

soc 1 year, 2 months ago

Hi Paul,

Glad to know it. I haven't used anythiing but the Arduino IDE to program ATMEGA chips, but one thing you probably can do is to compile the binary (or hex) file with Eclipse and manually upload it using the arduino tools (avrdude).
Just copy / paste the output you see when you upload an sketch with Arduino IDE in verbose mode and replace the the .cpp.hex file or wheatever with the one you generate with Eclipse.
Also, I know that Eclipse is a monster and highly customizable, so there's probably an option to use a custom command to compile/upload sketches; try to put this avrdude command there.
Sorry for not being more specific, but I never used those tools.
Good luck!

Link | Reply
Currently unrated

jantje 1 year ago

Since 3.1 there is support for programmers. You can select the programmer instead of the default upload protocol in the project properties->arduino
Jantje

Link | Reply
Current rating: 5

soc 1 year ago

I see, thanks for the feedback !

Link | Reply
Currently unrated

Milinda 1 year, 1 month ago

Your fuse value changes worked for me, this is a wonderful article, and you explain the process clearly. Thank you very much for sharing these info.

Link | Reply
Currently unrated

soc 1 year, 1 month ago

Thank you :-)

Link | Reply
Currently unrated

Sankalp Chapalgaonkar 1 year, 1 month ago

Hello SOC,
This post really helped me a lot.

Right now I am using the ATMega2560, I burned the bootloader successfully.

Now I'm trying to access the Pins of chip2560 via arduino uno as a ISP.
So I connected the following pin:

RX TO RX(I used pin12 for RX on 2560 chip)
TX TO TX(I used pin13 for TX on 2560 chip)
RESET TO RESET (I used pin30 for RESET on 2560 chip)
GND TO GND (I used pin32 for GND on 2560 chip)
VCC TO 5V(I used pin31 for VCC on 2560 chip)

And then connected a LED to pin 75 on atmega 2560 chip(which is basically digital pin 25).
Then uploaded the led blink code:

void setup() {
pinMode(25, OUTPUT);
}

void loop() {
digitalWrite(25,HIGH);
delay(100);
digitalWrite(25,LOW);
delay(100);
}

And then Program i uploading but still Led is not blinking.
Can you suggest a solution for this.

Link | Reply
Currently unrated

soc 1 year, 1 month ago

Hi Sankalp,
The comments are moderated manually, so i only approved this one not to have duplicates.

Your problem matches the typical behavior i insisted in: check the fuse value. If it's not the correct one, the sketch will upload OK but never run...
Check the other comments and you'll see.

Thanks for your feedback!

Link | Reply
Currently unrated

Bruno 6 months, 3 weeks ago

Hi,

It has helped me a lot and it is just working fine.
But I cant see anything on serial monitor.
Is there anything I can do to get the serial monitor working?

Thanks

Link | Reply
Currently unrated

soc 6 months, 3 weeks ago

Hi,

What do you mean. Your sketch uses the serial.print functions and it doesn't work?

Link | Reply
Currently unrated

Bruno 6 months, 3 weeks ago

Exactly.
I am using the first option (without removing the ATMEGA328P-PU).

Thanks for your feedback!

Link | Reply
Currently unrated

soc 6 months, 3 weeks ago

If you successfully programmed your sketch, you should then use the RX TW pins on the ATMEGA2560.
You need a USB to serial cable, or you could use the Arduino UNO with SoftwareSerial:

https://www.arduino.cc/en/Tutorial/SoftwareSerialExample

Just connect pins 10 and 11 to the TX and RX of the ATMEGA2560 ( pin 1 and 0 ).

Link | Reply
Currently unrated

New Comment

required

required (not published)

optional

Recent Tweets