Using the Mega2560 as ISP for Atmega328P

I've been trying to upload a sketch to my standalone atmega328 on a breadboard, using the Mega2560 as an ISP. I think I've seen about all the tutorials out there, but it's still not working.

The chip I bought has already had a bootloader (UNO) burnt to it, so that should be fine. Now all I ought to do is upload the ArduinoISP sketch, connect the 328 and be done with it.

Let's start with my setup, after uploading the sketch, I've connected:

Mega2560  | atmega328
----------+------------
SS [53]   | Reset [1]
MISO [50] | MISO [18]
MOSI [51] | MOSI [17]
SCK [52]  | SCK [19]
          |
5V        | VCC [7] and AVCC [20]
GND       | GND [8] and GND [22]

Also note that I'm not using the internal oscillator, so I connected a 16MHz crystal and 2 22pF capacitors. And to test for responsiveness, I connected an LED to pin 19, which should start blinking when code is uploaded.

I selected the Arduino Uno as the board, since it has the Uno bootloader, selected "Arduino as ISP" as the programmer, and tried to upload:

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00

Various sources report that the auto-reset should be disabled, but they disagree on how this is to be done. I've tried connecting a capacitor from Reset to GND (instructed here), Reset to 5V (instructed here). The various sources also seem to disagree on the value, but as the only one I had was a 100uF, this is what I used (also used in the second link).

However, the error code (0x00) doesn't seem to correspond to this issue, as can be seen here. According to that page, the error that is associated with a reset issue should be 0x15.

To check my connection, I tried burning a new bootloader. This went just fine. The LED started blinking and the arduino IDE reported success....

Any ideas what I could be doing wrong?

EDIT:

Just added a 10K pull-up resistor to pull pin 1 (Reset) of the 328 to 5V, but no difference. Also tried some other bootloaders besides the Uno. All of them result in a blinking LED, after the bootloader has been successfully burnt. However, flashing a sketch still results in an error. The error code is different this time though: 0x1c.

Sorry... You have a bootloader on a 328, so you want to configure a mega2560 as an ICSP to burn a bootloader on the 328 which already has one?

Have you tried just uploading a sketch via a USB to serial converter?

I don't want to configure it to upload a bootloader per se, but the configuration is the same as that needed to upload a sketch. At least, this is how I understood it.

So the problem is not burning the bootloader, it's uploading the sketch. And I don't have a USB to serial converter, so I'd prefer not to have to buy one if not strictly necessary

So use the Mega's USB/Serial adapter to download a sketch.
Connect Mega/s Reset to Gnd, that takes the 2560 out of the picture.
Then Connect Mega boards Rx to Rx pin on the chip, and Tx to Tx.
Start the download, press Reset on your chip when the IDE shows "Compiled xxx of 32xxx bytes" or similar.
Can be easier to see if you select File:Preferences and turn on Verbose Outputs.

Thanks! I will try this as soon as I get back to my setup. Just to be clear, I have to connect the 2560 directly to ground? Through a small resistor surely? I'd like to know what the effect of doing this would be. Is it equivalent to holding the reset button? Does this mean that the AVR chip is completely excluded from the circuitry and therefore the signals are just "passing through"?

Then, when uploading the sketch, I don't have to configure the 2560 as ISP first? I can just select Arduino Uno as the board, use the normal programmer, and upload?

I really hope this will work. And if it does, I'll notify right here for others in the future. There is only so little (proper) documentation that covers this stuff.

Reset to Gnd. Same as pressing & holding the Reset button. Chip goes into reset state, all IO becomes inputs. Rx & Tx from USB/Serial chip are then free to talk another device without the 2560 trying to talk on the lines.

"The chip I bought has already had a bootloader (UNO) burnt to it, " so you are just using the serial interface to download a sketch to what the IDE thinks is an Uno.
(unless you trashed the bootloader with your earlier attempts).

You should really get yourself an FTDI Basic or equivalent for future projects.

I've done exactly as instructed by CrossRoads (thanks again for the support), but still no luck. I've taken a photo of my setup:


Link to Dropbox

I hope it's clear enough... A list of the connections:

  • Ground on Arduino to ground-rail
  • 5V on Arduino to positive-rail
  • Pin 9 and 10 on the 328 to a 16MHz crystal
  • 22 pF capacitors from the crystal to ground
  • Pin 8 (GND) and 22 (GND) of the 328 to ground
  • Pin 7 (VCC) and 20 (AVCC) to 5V
  • Pin 1 (RESET) to a 10K pullup resistor
  • Pin 1 (RESET) to a switch connected to ground
  • Pin 2 (RxD) to RX0 on the Arduino and to an LED to see activity
  • Pin 3 (TxD) to TX0 on the Arduino and to an LED to see activity
  • Reset to Ground (both on the arduino; red wire looping).

In the IDE, I selected the Arduino Uno as board (it has the Uno bootloader), and I tried both AVR ISP and AVRISP mkII as programmers. On all occasions, I got the (by now familiarly depressing) message:

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00

I googled a little and found this post of someone with the same problems, although he is using a USB-to-serial breakout board. I also noticed that the (green) LED, connected to Rx, is always on. When I press the reset on my chip, the red LED (Tx) turns on for a while too. Note that this all happens while the Reset of the Arduino is shorted to ground.

I'm on the verge of buying a new chip, thinking that this one must have become faulty due to all my tampering. Or it might have been a bad one in the first place. Any ideas?

Again, thanks so much everyone for helping me out :slight_smile:

"tried both AVR ISP and AVRISP mkII as programmers."
They are not used when you are doing a serial download into a bootloaded part.
The 16U2 on the Mega is the USB/Serial interface and the programming is happening via the IDE talking to the bootload code over the serial interface (Rx/Tx).

The Mega Reset to Gnd keeps the Mega processor from interfering with the bootlaoder on the 328P talking to the IDE.

It's kinda late, I'll try and look at your picture closer tomorrow.

jorenheit:

  • Pin 1 (RESET) to a 10K pullup resistor
  • Pin 1 (RESET) to a switch connected to ground

Er... no.

Pin 1 (reset) on the Atmega328 needs to be connected via a pullup to VCC, and then to ground via a switch.

The Megas reset needs to be grounded to make it inactive. If you have done that to the 328 you will not be able to program it.

As you can't momentarily press the reset by software on the 328 (because that line is permanently low on the Mega to make it inactive in this configuration) you will need to press the reset button just before the IDE starts it's upload. After a couple of goes you will work out when it is trying to do this.

You should also get a USB to serial converter, either an FTDI from a reputable source (I.E not the cheapest you can find on eBay) or a CH340 based one.

ChilliTronix:
Er... no.

Pin 1 (reset) on the Atmega328 needs to be connected via a pullup to VCC, and then to ground via a switch.

I'm sorry if I'm saying something stupid right now, but how is this different from what I'm doing? I connected Pin 1 (reset) through a pullup to VCC (I omitted "to VCC", because that's implied by the pullup), and through a switch to ground.

CrossRoads:
They are not used when you are doing a serial download into a bootloaded part.
The 16U2 on the Mega is the USB/Serial interface and the programming is happening via the IDE talking to the bootload code over the serial interface (Rx/Tx).

What does this imply? It doesn't matter which programmer option I select?

I did some testing, and was hoping that you could comment on the results.

Rx0 on the Arduino is supposed to be linked directly to the USB-to-Serial converter (the 16u2), so I connected it to a LED on my breadboard and opened a serial connection. Every time I send a byte, the built-in LED (labeled Rx on the PCB) lights up as expected. However, the LED I connected to Rx0 (Pin 0) is always on, and doesn't change state when a byte is sent. It appears that no data is coming out of this port, so it's not surprising that the programming doesn't work...

Before I forget, a merry christmas to all of you :wink:

jorenheit:
I'm sorry if I'm saying something stupid right now, but how is this different from what I'm doing? I connected Pin 1 (reset) through a pullup to VCC (I omitted "to VCC", because that's implied by the pullup), and through a switch to ground.

That is correct for the Atmega328.

I thought you didn't have the ground connected for the reset for the Atmega2560 but I can see it now.

It looks OK and you have connected (or so it appears) RX to RX and TX to TX (which would normally be wrong but in this case is correct) so I have no idea what the problem is. You could try different jumper wires and you could also try reversing RX and TX to see if that makes any difference (it will not blow anything up).

Oh... and merry Christmas to you too!