Atmega328p: only able to upload one sketch

Hi, I'm new to the forum, but I've been playing with Arduino for about a year and often found terrific answers simply by searching old threads. This time, I had to sign up to address a unique (?) issue. I can bootload my 328p, I can upload and run a blink LED sketch, but I can't upload anything after that without re-bootloading first.

A little bit about my test setup:
I am programming custom boards that use the Atmega328p (32-pin, surface mount version). Sometimes, I can't get Tools > Burn Bootloader to work from the Arduino IDE. It throws up "invalid device signature" errors. So, I tried out the Atmega_Board_Programmer sketch from Nick Gammon (Gammon Forum : Electronics : Microprocessors : Atmega bootloader programmer). I uploaded it to an 8MHz Arduino-compatible board from RocketScream called the Mini Ultra (yes, quite an imaginative name). When I run the sketch, I choose the lilypad option for my 8MHz programmer and the bootloader writes to the chip. Everything seems good.

I, then, take that 328p (which is in a socket adapter, btw) to another breadboard with an 8MHz oscillator, connect the FTDI cable, and upload a simple blink LED sketch w/ no issues. When I try to upload again (even re-uploading the blink LED sketch), my SparkFun FTDI cable never gets a response from the MCU (green LED doesn't blink).

Here's the rub. I bit the bullet, got an AVRISP mkII, and ended up with the same results using the Tools > Burn Bootloader option from the Arduino IDE. For some reason, it won't take a second sketch without burning the bootloader again.

I thought this might be a fuse settings problem, but I can't figure it out. My fuses and lock byte are set after bootloading as follows:
LFuse = 0xFF
HFuse = 0xDA
EFuse = 0xFD
Lock byte = 0xCF

Any thoughts on this? I've looked at boards.txt and tried setting the fuses and lock bytes to be the same within Nick's sketch. Maybe the bootloader is being overwritten? The sketch I am writing is very small, so I doubt this is the problem.

I'm mystified because my boards that successfully bootloaded from the Arduino IDE never had this issue. Advice is appreciated.

Are you sure the ".upload.maximum_size=" entry in the boards.txt file for the board you are specifying at Upload is correct - taking into account the size of the bootloader you are using?

Are you uploading via serial, or using 'Upload Using Programmer'? The latter of the two erases the chip before programming, so removes the bootloader.

cparka23:
Hi, I'm new to the forum, but I've been playing with Arduino for about a year and often found terrific answers simply by searching old threads. This time, I had to sign up to address a unique (?) issue. I can bootload my 328p, I can upload and run a blink LED sketch, but I can't upload anything after that without re-bootloading first.

I am programming custom boards that use the Atmega328p

If you're uploading sketches with an ISP programmer then it will erase the bootloader, yes.

pegwatcher:
Are you sure the ".upload.maximum_size=" entry in the boards.txt file for the board you are specifying at Upload is correct - taking into account the size of the bootloader you are using?

Yes. The sketch size is set to ~30kB, which accounts for the 2kB bootloader I'm using. The test sketch I wrote is about 6kB, so I don't think this is the issue.

fungus:
If you're uploading sketches with an ISP programmer then it will erase the bootloader, yes.

Ah, that would explain the behavior of the AVRISP mkii. I will stop using the AVRISP for uploading sketches. Unfortunately, it doesn't quite explain why my serial upload doesn't work more than once. I first found this behavior using the bootloader sketch, and then I uploaded sketches using an FTDI cable.

Thank you all for the insight so far, though.

Thought I would update this thread, since I found the culprit this afternoon.

It seems that I have a bit of noise on the serial line. This isn't too surprising since I'm using a socket adapter with 5" wires that run to the breadboard. I soldered a bootloaded chip onto our PCB, and uploading multiple sketches with an FTDI cable works flawlessly.

If I were really interested in uploading sketches onto the breadboard, I suppose lowering the baud rate could work, and adding a dedicated voltage regulator might help too (I was powering things at 3.3V from a second FTDI board, fwiw.). It's not necessary for my current application as long as I know the chips bootload correctly, which they do. Just putting this out there for anyone who experiences this problem in the future.

Again, thanks for the input everyone.

Are you trying to use the internal oscillator? Or have you got an external 8MHz crystal/oscillator?

The internal osc is not tuned, so it wont be exactly 8MHz, instead it will be +/-10%. It is also temperature and voltage dependent. Basically it is unreliable for achieving accurate baud rates making programming hit and miss with a bootloader.