Bootloader problem - Arduino Mega and Duemilanove

Hello,
Well, I am close to killing myself after sending over 10 days and miserably failing at trying to burn a bootloader in a new Atmega328p. Here is what I am doing.

  1. I have set up two Arduino boards for this purpose. Using Arduino mega as programmer and Duemilanove as slave (with new Atmega328p). I have followed the instructions given at http://arduino.cc/en/Tutorial/ArduinoISP in letter and spirit.

I get the following

avrdude: stk500_program_enable(): protocol error, expect=0x14, resp=0x50
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.

avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51

Besides that, I have tried every possible way of setting up the atmega on a breadboard with an oscillator and all the relevant circuitry and tried to burn the bootloader with no avail.
Downloaded winavr and tried to do it with command prompt, but failed. I have gone through possibly every post in every forum and youtube videos and what not. Tried putting resistor (or 10uf cap) between ground and reset pin on slave Duemilanove. Nothing happens. Checked the comport, cables and connections a million times over and over but can not burn bootloader.
Used IDE 1.0 at first, it gave a device ID error, now using IDE 0018 and the error message is what I copied up here.
I am new to Arduino and don't want to loose hope in it. Please, any help would be greatly appreciated. Please also let me know how to test if atmega328 is shipped bad to me.

Regards
Waseem

Besides, I have also followed instructions given in the following post
http://arduino.cc/forum/index.php/topic,99385.0.html

Setup was same that is, Arduino Mega connected to PC while Duemilanove (with the new Atmega328p) connected with Mega.
Results were frustrating. The IDE 0018 gave following error

avrdude: stk500_getsync(): not in sync: resp=0x0a
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51

I have tried this whole and the previous setup with both the new Atmega328p and the one that came with Duemilanove (it is surely working and taking up the sketches) but I can not get the bootloader burned on any atmega328p.

What to do??

Post a nice clear photo of your setup.

And don't kill yourself because of THAT.

But please familiarize yourself with the concept of getting the right tools for the job. Spending somewhere between 10-30$ for a programmer would most likely (*) have saved you from spending 10 days in self-inflicted hell.

(*) If you use windows that may or may not be different.

try to disable the autoreset on the arduino you want to program.you must add a 120 resistor from reset pint to 5V.
You should then be able to sucessefuly upload the bootloader then

Dear Mr. Gammon,
Here are the pics of the setup. Mega being the programmer and Duemilanove being the slave with the Atmega 328p insterted waiting to be bootloaded.

@madworm, yeh, the bootloader literally sucks big time. How can one possibly know what the right tools are for this job besides owning two arduinos?? It is supposed to work rightaway!

@HugoPT, I have already tried that. In the pics attached too, I have a 120ohm resistor connected to +5v and Resest. And here is the output error given by Arduino IDE 0018
avrdude: stk500_program_enable(): protocol error, expect=0x14, resp=0x50
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.

avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51

In the pics, I have tried putting the resistor and disabling reset on Duemilanove with and without connecting pin 10 of Mega to reset on Arduino.

Guys, I really need some help here.

Regards

Have you tried this approach?
See the video in this thread
http://arduino.cc/forum/index.php/topic,68183.0.html

Mega Duem
D10 Reset
D11 D11
D12 D12
D13 D13
Gnd Gnd
+5 +5

Load the Optiloader sketch on the Mega, put a blank part in the Duem.
Reset the Mega, it will read the blank part and put the correct bootloader based on the blank part's signature.

I also have a Duemilanove modified to bootload parts by adding a few pins to X3 next to the FTDI part, and making a little cable to connect them to the ICSP pins.
Took a while to setup the software on IDE -0022, but it does work.

http://www.geocities.jp/arduino_diecimila/bootloader/index_en.html

Nowadays I just use an AVR ISP tho. Nice tool to have.

If my eyes don't deceive me you appear to have swapped pins 10 and 11. Why did you do that?

Actually I withdraw that comment. On closer inspection you didn't ... I don't think.

From this page:

On the Arduino Uno, you'll need to connect a 10 uF capacitor between reset and ground (after uploading the ArduinoISP sketch).

You aren't using a Uno to program, but I suspect that disabling reset on the Mega using that method might help.

If running the optiloader sketch, disabling reset is not needed.

Right then. Let me describe what I did to get to this point:

$ avrdude -c arduino -p m328p -v -P /dev/tty.usbmodemfd141 -b 19200 


         avrdude: Version 5.8cvs, compiled on Jan 15 2010 at 17:27:01
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/usr/local/CrossPack-AVR-20100115/etc/avrdude.conf"
         User configuration file is "/Users/nick/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/tty.usbmodemfd141
         Using Programmer              : arduino
         Overriding Baud Rate          : 19200
         AVR Part                      : ATMEGA328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65     5     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Arduino
         Description     : Arduino
         Hardware Version: 2
         Firmware Version: 1.18
         Topcard         : Unknown
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.04s

avrdude: Device signature = 0x1e950f
avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as DE
avrdude: safemode: efuse reads as 5

avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as DE
avrdude: safemode: efuse reads as 5
avrdude: safemode: Fuses OK

avrdude done.  Thank you.

You will note that it detected the ATMEGA328P part, and read the fuses. So from here on, it is just formality I think.

First important point: Since we are using a Mega, and programming is done via SPI, we need to use the Mega SPI pins, not the Uno ones.

Mega      Duem
D53       Reset (SS)
D51       D11 (MOSI)
D50       D12 (MISO)
D52       D13 (SCK)
Gnd       Gnd
+5         +5

Next, I uploaded the "example" ArduinoISP sketch that shipped with 1.0 of the IDE onto the Mega.

Then, it appeared to work better (ie. at all) if I stuck a 10 uF capacitor between Reset and Gnd on the Mega.

It also appeared to need the Mega to be reset (ie. reset button) before trying anything. Wait a few seconds for the Tx/Rx lights to stop flashing, as the bootloader on the Mega tries to do serial comms.

Then I used avrdude on the command line, after working out which comm port the device was recognized on (in my case, /dev/tty.usbmodemfd141).

avrdude -c arduino -p m328p -v -P /dev/tty.usbmodemfd141 -b 19200

The -v just checks that it can communicate with the target device.

If you want to see more, replace -v by -vvvv and then you see stuff like this:

         Using Port                    : /dev/tty.usbmodemfd141
         Using Programmer              : arduino
         Overriding Baud Rate          : 19200
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 
         AVR Part                      : ATMEGA328P

So you're not running westfw's optiloader then?

See the video here
http://arduino.cc/forum/index.php/topic,68183.0.html
My understanding was that after connecting the 2 systems, pressing reset on the master started the process, and when done it just waited for another reset to start the next bootloading operation.

CrossRoads:
So you're not running westfw's optiloader then?

No, I was just using the supplied ArduinoISP sketch to turn the first Arduino (in this case the Mega) into an ISP programmer. Then you use avrdude in the usual way .

@CrossRoads,
I have tried this approach. Let me explain it here in detail so that if I may be guided if there is something wrong with what I am doing.

  1. downloaded optiloader .pde and .hex from GitHub - WestfW/OptiLoader: Arduino sketch for burning new bootloaders
  2. Copied both files in a directory named Optiloader and placed that directory in Libraries folder within Arduino folder.
  3. Compiled the sketch and uploaded it to Mega.
  4. Connected Duemilanove (having new Atmega328p inserted) with mega exactly as the way you described.
  5. press the reset button on mega, the LED connected to D13 of both mega and duemilanove flash multiple times and then turn off.
  6. Now connected the Duemilanove(with new Atmega328) with PC. Removed all cables connecting it with mega and tried to upload the sketch to Atmega328. It says, error uploading.
  7. When I press reset on Duemilanove, the LED on D13 does not flash meaning, no bootloader was installed.

Currently working on the method shared by Mr. Gammon. Will post the results as soon as I am done with it.
Man, it sucks!! Any ideas?

Regards

It's an ATmega328P-PU and not a ATmega328-PU?
And you have the Board selected = Duemilanove?

Well,
I really really appreciate the help extended by all the friends here. Special thanks to Mr. Nick Gammon. Your solution worked like a charm sir.
It comes out that I was using wrong pins of Mega to connect to duemilanove. This should have been explained by arduino guys while explaining how to use Arduino as ISP. Thanks to Mr. Gammon, I used the mega pins as he explained and followed the instructions given at Installing an Arduino Bootloader - SparkFun Learn religiously.
I was able to successfully program both the new Atmega328p and they are taking up sketches as they are supposed to.

A word of caution though, for newbies like me: Avrdude does not work properly with IDE 0018. while worked perfectly with IDE 1.0. Also, the bootloader for 328p that comes with "Arduino Optiboot Bootloader" ( which can be downloaded at the sparkfun page linked above) did not work for me. It burned the bootloader but then I was unable to upload sketches to 328p. Contrary to that, the bootloader that comes arduino IDE, works perfectly fine.

@CrossRoads: do we need to set fuses first for the new part for using optiloader to program directly through mega?

Regards

@drwaseem,
I don't know. westfw could probably answer that.