Breadboard 328P Accepting Bootloader but failing Sketch upload

Hi All,

I have a Atmega328P on a breadboard. I am using MKII to ISP program the chip with a bootloader. This works wonderfully. I can load blink this way also and that works.

I have usb connected using a "Breakout Board for FT232RL USB to Serial" from sparkfun. If I ISP load a sketch onto the Atmega328P I can get data to print out to the Serial Monitor so all is well there.

Here is where the problem is. If I burn a bootloader onto the chip and then try to upload a program via USB using the normal Arduino IDE I get the following error:

avrdude: AVR device initialized and ready to accept instructions

Reading | avrdude: Send: u [75]   [20] 
avrdude: Recv: . [14] . [1e] . [95] . [0f] . [10] 
################################################## | 100% 0.02s

avrdude: Device signature = 0x1e950f
avrdude: Send: V [56] . [a0] . [03] . [fc] . [00]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [00] 
avrdude: Recv: . [10] 
avrdude: Send: V [56] . [a0] . [03] . [fd] . [00]   [20] 
avrdude: Recv: . [00] 
avrdude: stk500_cmd(): programmer is out of sync

The funny thing is it worked ONE time, the first time, and no longer does. I have tried re-burning the bootloader but now it just does not work. It seems communication is fine since the Device Signature comes back ok. I have tried hitting reset right before it starts uploading and I get this message. I am using the optiboot bootloader.

During troubleshooting I did get the new Arduino 1.0.5 version (I was at 1.0.3).

This is driving me crazy, I've spend days on it and thought someone here might have some ideas.

Thanks all,

Steve

What type of clocking are you using for the breadboard m328p? It needs to match the bootloader type and IDE board selection to get the right serial baud rate to match up.

Which optiboot file are you using? Are you burning bootloader from the IDE or from command line with avrdude?

What are you fuse settings?

Stuff about breadboard Arduinos:

I am using an external Crystal at 16 Mhz. I am using the Arduino IDE to upload and selecting "Arduino UNO" as the board. The optiboot file I am using is optiboot_atmega328.hex.

I am using the standard UNO fuse settings (in the output below). I read through the Gammon tutorial and loaded the board detector sketch. It worked just fine and here is the output:

Atmega chip detector.
Entered programming mode OK.
Signature = 1E 95 0F 
Processor = ATmega328P
Flash memory size = 32768
LFuse = FF 
HFuse = DE 
EFuse = FD 
Lock byte = FF 
Clock calibration = A3 
Bootloader in use: Yes
EEPROM preserved through erase: No
Watchdog timer always on: No
Bootloader is 512 bytes starting at 7E00

Bootloader:

7E00: 11 24 84 B7 14 BE 81 FF F0 D0 85 E0 80 93 81 00 
7E10: 82 E0 80 93 C0 00 88 E1 80 93 C1 00 86 E0 80 93 
7E20: C2 00 80 E1 80 93 C4 00 8E E0 C9 D0 25 9A 86 E0 
7E30: 20 E3 3C EF 91 E0 30 93 85 00 20 93 84 00 96 BB 
7E40: B0 9B FE CF 1D 9A A8 95 81 50 A9 F7 CC 24 DD 24 
7E50: 88 24 83 94 B5 E0 AB 2E A1 E1 9A 2E F3 E0 BF 2E 
7E60: A2 D0 81 34 61 F4 9F D0 08 2F AF D0 02 38 11 F0 
7E70: 01 38 11 F4 84 E0 01 C0 83 E0 8D D0 89 C0 82 34 
7E80: 11 F4 84 E1 03 C0 85 34 19 F4 85 E0 A6 D0 80 C0 
7E90: 85 35 79 F4 88 D0 E8 2E FF 24 85 D0 08 2F 10 E0 
7EA0: 10 2F 00 27 0E 29 1F 29 00 0F 11 1F 8E D0 68 01 
7EB0: 6F C0 86 35 21 F4 84 E0 90 D0 80 E0 DE CF 84 36 
7EC0: 09 F0 40 C0 70 D0 6F D0 08 2F 6D D0 80 E0 C8 16 
7ED0: 80 E7 D8 06 18 F4 F6 01 B7 BE E8 95 C0 E0 D1 E0 
7EE0: 62 D0 89 93 0C 17 E1 F7 F0 E0 CF 16 F0 E7 DF 06 
7EF0: 18 F0 F6 01 B7 BE E8 95 68 D0 07 B6 00 FC FD CF 
7F00: A6 01 A0 E0 B1 E0 2C 91 30 E0 11 96 8C 91 11 97 
7F10: 90 E0 98 2F 88 27 82 2B 93 2B 12 96 FA 01 0C 01 
7F20: 87 BE E8 95 11 24 4E 5F 5F 4F F1 E0 A0 38 BF 07 
7F30: 51 F7 F6 01 A7 BE E8 95 07 B6 00 FC FD CF 97 BE 
7F40: E8 95 26 C0 84 37 B1 F4 2E D0 2D D0 F8 2E 2B D0 
7F50: 3C D0 F6 01 EF 2C 8F 01 0F 5F 1F 4F 84 91 1B D0 
7F60: EA 94 F8 01 C1 F7 08 94 C1 1C D1 1C FA 94 CF 0C 
7F70: D1 1C 0E C0 85 37 39 F4 28 D0 8E E1 0C D0 85 E9 
7F80: 0A D0 8F E0 7A CF 81 35 11 F4 88 E0 18 D0 1D D0 
7F90: 80 E1 01 D0 65 CF 98 2F 80 91 C0 00 85 FF FC CF 
7FA0: 90 93 C6 00 08 95 80 91 C0 00 87 FF FC CF 80 91 
7FB0: C0 00 84 FD 01 C0 A8 95 80 91 C6 00 08 95 E0 E6 
7FC0: F0 E0 98 E1 90 83 80 83 08 95 ED DF 80 32 19 F0 
7FD0: 88 E0 F5 DF FF CF 84 E1 DE CF 1F 93 18 2F E3 DF 
7FE0: 11 50 E9 F7 F2 DF 1F 91 08 95 80 E0 E8 DF EE 27 
7FF0: FF 27 09 94 FF FF FF FF FF FF FF FF FF FF 04 04 

MD5 sum of bootloader = 26 50 2F 77 BC 73 92 BC 84 2D 85 A4 D9 26 89 B0 

First 256 bytes of program memory:

0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
A0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
B0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
C0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
D0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
E0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
F0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Hiduino: I failed to answer your question correctly. I am using the Arduino IDE to upload the sketch, but I am using the Atmel Studio "Device Programming" to burn the bootloader.

Steve

Nick,

You have some great tools out there, thank you for providing them for us wanna be arduino masters!

I decided to use a couple of your tools to track down this issue. Here is the output from your Fuse Calculator:

Atmega fuse calculator.
Written by Nick Gammon.
Entered programming mode OK.
Signature = 0x1E 0x95 0x0F 
Processor = ATmega328P
Flash memory size = 32768
LFuse = 0xFF 
HFuse = 0xDE 
EFuse = 0xFD 
Lock byte = 0xFF 
Clock calibration = 0xA3 
External Reset Disable.................. [ ]
Debug Wire Enable....................... [ ]
Enable Serial (ICSP) Programming........ [X]
Watchdog Timer Always On................ [ ]
Preserve EEPROM through chip erase...... [ ]
Boot into bootloader.................... [X]
Divide clock by 8....................... [ ]
Clock output............................ [ ]
Bootloader size: 512 bytes.
Start-up time: SUT0: [ ]  SUT1: [ ] (see datasheet)
Clock source: low-power crystal.
Brownout detection at: 2.7V.

After answering Hiduino's post and reading Nicks's I realized I should probably try to program the bootloader with an Arduino using Nicks Board Programmer option. Low and behold, it FAILED. I have no idea why. Here is the output in hoping it sparks an idea in someone:

Atmega chip programmer.
Written by Nick Gammon.
Entered programming mode OK.
Signature = 0x1E 0x95 0x0F 
Processor = ATmega328P
Flash memory size = 32768 bytes.
LFuse = 0xFF 
HFuse = 0xDE 
EFuse = 0xFD 
Lock byte = 0xFF 
Clock calibration = 0xA3 
Bootloader address = 0x7E00
Bootloader length = 512 bytes.
Type 'L' to use Lilypad (8 MHz) loader, or 'U' for Uno (16 MHz) loader ...
Using Uno Optiboot 16 MHz loader.
Type 'V' to verify, or 'G' to program the chip with the bootloader ...
Erasing chip ...
Writing bootloader ...
Committing page starting at 0x7E00
Committing page starting at 0x7E80
Committing page starting at 0x7F00
Committing page starting at 0x7F80
Written.
Verifying ...
Verification error at address 7E00. Got: 0x00  Expected: 0x11 
Verification error at address 7E01. Got: 0x00  Expected: 0x24 
Verification error at address 7E02. Got: 0x00  Expected: 0x84 
Verification error at address 7E03. Got: 0x00  Expected: 0xB7 
Verification error at address 7E04. Got: 0x00  Expected: 0x14 
Verification error at address 7E05. Got: 0x00  Expected: 0xBE 
Verification error at address 7E06. Got: 0x00  Expected: 0x81 
Verification error at address 7E07. Got: 0x00  Expected: 0xFF 
Verification error at address 7E08. Got: 0x00  Expected: 0xF0 
Verification error at address 7E09. Got: 0x00  Expected: 0xD0 
Verification error at address 7E0A. Got: 0x00  Expected: 0x85 
Verification error at address 7E0B. Got: 0x00  Expected: 0xE0 
Verification error at address 7E0C. Got: 0x00  Expected: 0x80 
Verification error at address 7E0D. Got: 0x00  Expected: 0x93 
Verification error at address 7E0E. Got: 0x80  Expected: 0x81 
Verification error at address 7E0F. Got: 0xC0  Expected: 0x00 
Verification error at address 7E10. Got: 0x00  Expected: 0x82 
Verification error at address 7E11. Got: 0x00  Expected: 0xE0 
Verification error at address 7E12. Got: 0x00  Expected: 0x80 
Verification error at address 7E13. Got: 0x00  Expected: 0x93 
Verification error at address 7E14. Got: 0x00  Expected: 0xC0 
Verification error at address 7E16. Got: 0x00  Expected: 0x88 
Verification error at address 7E17. Got: 0xC0  Expected: 0xE1 
Verification error at address 7E18. Got: 0x00  Expected: 0x80 
Verification error at address 7E19. Got: 0x00  Expected: 0x93 
Verification error at address 7E1A. Got: 0x80  Expected: 0xC1 
Verification error at address 7E1B. Got: 0xC0  Expected: 0x00 
Verification error at address 7E1C. Got: 0x80  Expected: 0x86 
Verification error at address 7E1D. Got: 0xC0  Expected: 0xE0 
Verification error at address 7E1E. Got: 0xE0  Expected: 0x80 
Verification error at address 7E1F. Got: 0xE0  Expected: 0x93 
Verification error at address 7E20. Got: 0x00  Expected: 0xC2 
Verification error at address 7E22. Got: 0x00  Expected: 0x80 
Verification error at address 7E23. Got: 0x00  Expected: 0xE1 
Verification error at address 7E24. Got: 0x00  Expected: 0x80 
Verification error at address 7E25. Got: 0x00  Expected: 0x93 
Verification error at address 7E26. Got: 0x80  Expected: 0xC4 
Verification error at address 7E27. Got: 0xC0  Expected: 0x00 
Verification error at address 7E28. Got: 0x00  Expected: 0x8E 
Verification error at address 7E29. Got: 0x00  Expected: 0xE0 
Verification error at address 7E2A. Got: 0x80  Expected: 0xC9 
Verification error at address 7E2B. Got: 0xC0  Expected: 0xD0 
Verification error at address 7E2C. Got: 0x80  Expected: 0x25 
Verification error at address 7E2D. Got: 0x80  Expected: 0x9A 
Verification error at address 7E2E. Got: 0xC0  Expected: 0x86 
Verification error at address 7E30. Got: 0x00  Expected: 0x20 
Verification error at address 7E31. Got: 0x00  Expected: 0xE3 
Verification error at address 7E32. Got: 0x80  Expected: 0x3C 
Verification error at address 7E33. Got: 0x80  Expected: 0xEF 
Verification error at address 7E34. Got: 0x80  Expected: 0x91 
Verification error at address 7E35. Got: 0x80  Expected: 0xE0 
Verification error at address 7E36. Got: 0xC0  Expected: 0x30 
Verification error at address 7E37. Got: 0xE0  Expected: 0x93 
Verification error at address 7E38. Got: 0x80  Expected: 0x85 
Verification error at address 7E39. Got: 0x80  Expected: 0x00 
Verification error at address 7E3A. Got: 0xC0  Expected: 0x20 
Verification error at address 7E3B. Got: 0xE0  Expected: 0x93 
Verification error at address 7E3C. Got: 0xC0  Expected: 0x84 
Verification error at address 7E3D. Got: 0xE0  Expected: 0x00 
Verification error at address 7E3E. Got: 0xF0  Expected: 0x96 
Verification error at address 7E3F. Got: 0xF0  Expected: 0xBB 
Verification error at address 7E40. Got: 0x00  Expected: 0xB0 
Verification error at address 7E41. Got: 0x00  Expected: 0x9B 
Verification error at address 7E42. Got: 0x00  Expected: 0xFE 
Verification error at address 7E43. Got: 0x00  Expected: 0xCF 
Verification error at address 7E44. Got: 0x00  Expected: 0x1D 
Verification error at address 7E45. Got: 0x00  Expected: 0x9A 
Verification error at address 7E46. Got: 0x80  Expected: 0xA8 
Verification error at address 7E47. Got: 0xC0  Expected: 0x95 
Verification error at address 7E48. Got: 0x00  Expected: 0x81 
Verification error at address 7E49. Got: 0x00  Expected: 0x50 
Verification error at address 7E4A. Got: 0x80  Expected: 0xA9 
Verification error at address 7E4B. Got: 0xC0  Expected: 0xF7 
Verification error at address 7E4C. Got: 0x80  Expected: 0xCC 
Verification error at address 7E4D. Got: 0x80  Expected: 0x24 
Verification error at address 7E4E. Got: 0xC0  Expected: 0xDD 
Verification error at address 7E4F. Got: 0xE0  Expected: 0x24 
Verification error at address 7E50. Got: 0x00  Expected: 0x88 
Verification error at address 7E51. Got: 0x00  Expected: 0x24 
Verification error at address 7E52. Got: 0x80  Expected: 0x83 
Verification error at address 7E53. Got: 0x80  Expected: 0x94 
Verification error at address 7E54. Got: 0x80  Expected: 0xB5 
Verification error at address 7E55. Got: 0x80  Expected: 0xE0 
Verification error at address 7E56. Got: 0xC0  Expected: 0xAB 
Verification error at address 7E57. Got: 0xE0  Expected: 0x2E 
Verification error at address 7E58. Got: 0x80  Expected: 0xA1 
Verification error at address 7E59. Got: 0x80  Expected: 0xE1 
Verification error at address 7E5A. Got: 0xC0  Expected: 0x9A 
Verification error at address 7E5B. Got: 0xE0  Expected: 0x2E 
Verification error at address 7E5C. Got: 0xC0  Expected: 0xF3 
Verification error at address 7E5E. Got: 0x00  Expected: 0xBF 
Verification error at address 7E5F. Got: 0xF0  Expected: 0x2E 
Verification error at address 7E60. Got: 0x00  Expected: 0xA2 
Verification error at address 7E61. Got: 0x00  Expected: 0xD0 
Verification error at address 7E62. Got: 0x80  Expected: 0x81 
Verification error at address 7E63. Got: 0x80  Expected: 0x34 
Verification error at address 7E64. Got: 0x80  Expected: 0x61 
Verification error at address 7E65. Got: 0x80  Expected: 0xF4 
Verification error at address 7E66. Got: 0xC0  Expected: 0x9F 
Verification error at address 7E67. Got: 0xE0  Expected: 0xD0 
Verification error at address 7E68. Got: 0x80  Expected: 0x08 
499 verification error(s).
First 100 shown.
Type 'C' when ready to continue with another chip ...

Thanks again for any help everyone,

Steve

It looks like either bad wiring connections or possibly bad clocking. You should double check you wiring and make sure they are good connections. Also check your 16MHz crystal and 22pF capacitors that they are wired properly.
Do you have a picture of your breadboard you could attach?

Clearly the wiring is at least partly OK, because you are reading the chip signature alright.

Do you have decoupling capacitors? What value and where did you put them? How long are the cable (wire) runs?

The programmer sketch already programs fairly slowly, assuming this line is in it still around line 593:

  // slow down SPI for benefit of slower processors like the Attiny
  SPI.setClockDivider (SPI_CLOCK_DIV64);

Do you have anything else connected to the ISP pins on the m328P? If so it could be interfering with the programming.

OK, still having problems but have tried many more things. I DID have an SPI memory chip on the same connections so I have removed that. It did not help. I have replaced the clock crystal and capacitors (22pf). These are short runs just on the breadboard with no jumpers, just connected directly to ground. The capacitors are sitting on the far side of the clock crystal from the atmega chip.

The SPI.setClockDivider (SPI_CLOCK_DIV64); line is still in the sketches from Nick, and I have made no alteration to those sketchs.

Here is where I have some new information. If I load a hex file from the mkII onto the board it works perfectly. I can run it for hours and it continues to blink LEDs regularly and write information out to the Serial Monitor with no problems. I am writing a 50 character message every 301 milliseconds and according to the internal millis, it is writing every 302 milliseconds ( so the sketch loop takes about 1 to run ). I also read data and return back out. This continues to work perfectly.

So connected to USB sure appears to work just fine.

However it still cannot load a program with USB. I tried using the Atmega_Board_Detector, and did find some interesting behavior. when I reset the Arduino running the sketch, it pauses until I hit the reset button (as expected). However if I hold down the reset button for a long time it reads out the entire bootloader correctly. If on the other hand I press it very quickly, the faster I press it the LESS data it reads. If I do a rapid press, it only reads a few lines of code. If it reads the entire thing, I can compare it to the optiboot hex and it matches.

This combined with the fact that I get PARTWAY through the usb sketch loading (still getting to the same place as my previous post) sure make it seem like a clocking difference, but for the life of me and can't figure out what the problem should be. In Atmel Studio, when loading the bootloader with the Device Programming window, I can run it at 4 Mhz which should indicate it is indeed running at 16 Mhz (since Device Programming only works at 1/4 speed).

Any other ideas anyone?

stephenholm:
However it still cannot load a program with USB. I tried using the Atmega_Board_Detector, and did find some interesting behavior. when I reset the Arduino running the sketch, it pauses until I hit the reset button (as expected). However if I hold down the reset button for a long time it reads out the entire bootloader correctly. If on the other hand I press it very quickly, the faster I press it the LESS data it reads. If I do a rapid press, it only reads a few lines of code. If it reads the entire thing, I can compare it to the optiboot hex and it matches.

Sound like the reset pin on the m328p is not connecting properly. You might have a bad contact position on your breadboard.

How are you connecting up to reset on the m328p? The programmers (AVRISPMKII and Atmega_Board_Detector) should go direct to the reset pin and have a 10K ohm pull-up resistor.

For the USB serial, the DTR should be going to a 0.1uF capacitor then to the reset pin on the m328p.

I didn't see an answer to my question about: do you have decoupling capacitors? If so what sort and where are they?

Hello,

solution is quiete simple but very hard to find. I used it today with success.
You have to add a 10k resistor between pin 2(RX) and VCC, and an other one beetween pin 3 (TX) and VCC too.

I didn't beleive it before I tried it. It works now with an ATmega328P-PN (nearly same as PU) with optiboot on a breadboard.

Of course the 0.1 µF capacitor is also necessary.

I have a 10 Kohm pull up resistor on the reset pin of the atmega. The reset pins for the AVRISPMKII and Atmega_Board_Detector goes directly to the reset pin. Using the AVRISPMKII I do not have to do anything and it works. Using Atmega_Board_Detector I DO have to press the reset button for it to work.

I do NOT have a 0.1uF from the DTR pin to the reset pin. Is this needed? I thought/hoped I could just hit reset before it started programming.

Nick: As far as decoupling capacitors, I am using a lithium polymer battery to power the board and am NOT using a regulator. The battery is fully charged and running between 4 and 4.1 volts. I have one 4.7 uF capacitor between the power leg and ground right next to the input from the battery.

Hello,

The 0.1µF capacitor on pin 1 (reset) is necessary.

If this one is not sufficiant, please try to add the two 10K resistors I talked about. You will have a total of 3 resistors.

Regards

The blue capacitors are the decoupling capacitors. They are required for reliable operation. In fact they should be closer than I showed there. Directly adjacent to the chip legs, not a couple of centimeters away.

I should have cut those blue and red wires shorter and put the capacitors next to the pins. However this particular setup worked.

Using Atmega_Board_Detector I DO have to press the reset button for it to work.

That's odd. Can you post a picture? I never have to press reset. Something is seriously wrong if you do. I should point out that the Atmega_Board_Detector holds /Reset low during programming (that's how you enter programming mode). However it does it programmatically. If you have to do it yourself there is something major wrong.

stephenholm:
I have a 10 Kohm pull up resistor on the reset pin of the atmega. The reset pins for the AVRISPMKII and Atmega_Board_Detector goes directly to the reset pin. Using the AVRISPMKII I do not have to do anything and it works. Using Atmega_Board_Detector I DO have to press the reset button for it to work.

Ahh, For the Atmega_Board_Detector, you need to use pin D10 to the reset on the m328p.(Not reset to reset)

stephenholm:
I do NOT have a 0.1uF from the DTR pin to the reset pin. Is this needed? I thought/hoped I could just hit reset before it started programming.

That is fine, you don't need the reset capacitor. You just need to time when you hit the reset button.

nbenm: It worked ... do you happen to know why? I put the two 10 Kohm pull up resistors on the two communication pins between the USB controller and the atmega chip. Did that and bingo! it works! Is still am not using the .1 uF on the DTR pin, I'm just hitting reset instead.

Nick: I do have a 10 uF capacitor on the analog power circuit and it is right next to the pin. On the power side I have the 4.7 uF I mentioned but it is closer to the power supply. So you believe I should have the .1 uFs right next to the chip also?

hiduino: Understood, I had it wired incorrectly for the Atmega_Board_Detector.

Thanks again for all your help everyone!

stephenholm:
Nick: I do have a 10 uF capacitor on the analog power circuit and it is right next to the pin. On the power side I have the 4.7 uF I mentioned but it is closer to the power supply. So you believe I should have the .1 uFs right next to the chip also?

Yes I do. The 0.1 uF filter a different frequency than the 4.7 uF.

stephenholm:
I put the two 10 Kohm pull up resistors on the two communication pins between the USB controller and the atmega chip. Did that and bingo! it works!

After reviewing the FT232RL breakout board from SparkFun, it turns out that it is running at default of 3.3v i/o levels which explains why the serial was not working correctly. You have a mismatch of i/o voltage level interfacing to the m328P. The 10K pull-up is probably biasing the level enough to work, but not the recommended way to do level conversion.