Programming sketch to ATMEGA328P-AU

OK...I'm at the end of my tether in trying to program my ATMEGA328P-AU. I went through every thread and google links and tried to program the darn thing. I first tried using the ATMEL studio 6.2 from recovering the hex file. I tried using the UNO as a programmer. I used the gammon.com.au website forum and used his method.

The furthest I got was programming the bootloader to the MCU using Gammons method, but could not get the sketch onto it. I keep getting the (i forgot now because I needed a few weeks to cool off) - "expect error". I done some search on that error and it referenced to not seeing the device. I checked and rechecked and rechecked the pcb and everything is where it is supposed to be connected. Pull up resistors, decoupling caps all in place.

I'm now wondering about fuses and possibly the code. The code is a simple use of the 6 PWM outputs to fade the LEDs randomly. It works on the UNO, but I don't know enough about the code if it uses the internal clock of the ATMEGA on the UNO or the crystal ( I ripped the code and modified it to work on 6 LEDs). The bootloader programmed OK without the use of a crystal or resonator.

Any ideas of where to go next without ripping out what's left of my hair.

Here's the code:

int thr=0; // represents brightness for pin,
int fiv=50; // each integer's name is the firs three letters of the pin
int six=100; // it represents of the pin it represents this is just to keep
int nin=150; // track it represents
int ten=205;
int ele=255;

int c1=5; //c represents change, each pin gets its own change
int c2=5; // so it wont interfere with any other pin
int c3=5;
int c4=5;
int c5=5;
int c6=5;

void setup(){
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
}
void loop(){
analogWrite(3,thr);
thr=thr+c1;
analogWrite(5,fiv);
fiv=fiv-c2;
analogWrite(6,six);
six=six-c3;
analogWrite(9,nin);
nin=nin-c4;
analogWrite(10,ten);
ten=ten-c5;
analogWrite(11,ele);
ele=ele-c6;

if(thr==0 || thr==255){
c1=-c1;
}
if(fiv==20 || fiv==255){
c2=-c2;
}
if(six==20 || six==255){
c3=-c3;
}
if(nin==30 || nin==255){
c4=-c4;
}
if(ten==20 || ten==255){
c5=-c5;
}
if(ele==10 || ele==255){
c6=-c6;
}
delay(30);
}

If you can't remember the exact error message then I can't remember the answer.

Can you get that code to compile and upload to the UNO?

You shouldn't need to use Atmel Studio. The Arduino IDE can upload using an UNO as a programmer.

Did you get the right bootloader for your application? If it's expecting a crystal and you have no crystal then it won't work.

Here's the code:

The code isn't relevant (nor is it in code tags).

If you can upload the bootloader you can upload a sketch. Please report what the fuse settings are (copy and paste).

I have checked every connection again and am only still able to upload the board detector output and the chip programmer output. I have 4 loaded boards and they are all able to get this far.

When I try to upload the ASCIItable example using a USB Serial Converter (with the 0.1uF cap) the bottom of the screen shows it is "Uploading" and gets to about 90% green bar. The bar disappears but the word "Uploading..." is still there. I've waited up to 3 minutes, still there. I open the serial comm window and nothing there (baud rate correct). I try to upload my sketch...same issue...

At this point...may I add...When burning the bootloader, I select "L". Then in the Tools>>Board I select "Lilypad Arduino USB". So I think that this may be the issue. So I then tried to change Tools>>Board to "Lilypad Arduino". I then get an option to choose processor "Atmega328" or "Atmega168". I choose "Atmega328". I then try upload either sketch and get the following: Problem uploading to board: avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x51.

The only difference I see is that in Nicks example it says to use Tools>>Board and choose "Lilypad Arduino w/ Atmega328" which is not on the list.

I feel like I'm so close, but frustratingly so far.

Uploading Issue 1_1.jpg

Uploading Issue 2_1.jpg

Uploading Issue 3_1.png

Hi Nick,

Thanks. At this stage I'm guessing it's the fuses. I don't know how to find, read, nor change this. Help?

Please run the sketch described here.

Post its output (copy and paste into a message, don't take a screenshot of it).

daffyzone:

  • Uploading Issue 1_1.jpg 8.33 KB downloaded 1 times
  • Uploading Issue 2_1.jpg 11.07 KB downloaded 1 times
  • Uploading Issue 3_1.png 164.21 KB downloaded 1 times

It's really counter-productive to take screenshots of pure text. For one thing, it is tedious to look at them because of the way the forum works. For another, one cannot copy and paste relevants parts into a reply to refer to them.

Apologies for delay...Aussie long weekend...

I read and ran the sketch you mentioned quickly before heading out and it did not work. I kept getting error messages (I will try again and post these error messages ASAP). I tried uploading the bootloader again unsuccessfully (kept getting error messages). So now I'm completely confused. Why can I load the bootloader only once? Am I doing something wrong? Is there something I need to know to load another bootloader?

Hi,

Here is the top bit of the board detector. I'm not sure where to go from here.

Atmega chip detector.
Written by Nick Gammon.
Version 1.17
Compiled on Oct 9 2015 at 08:47:56 with Arduino IDE 10605.
Attempting to enter ICSP programming mode ...
Entered programming mode OK.
Signature = 0x1E 0x95 0x0F
Processor = ATmega328P
Flash memory size = 32768 bytes.
LFuse = 0xE2
HFuse = 0xDA
EFuse = 0xFD
Lock byte = 0xEF
Clock calibration = 0xB2
Bootloader in use: Yes
EEPROM preserved through erase: No
Watchdog timer always on: No
Bootloader is 2048 bytes starting at 7800

Bootloader:

Hi Nick,

Could you please help me? I have copied the output from the board detector and am completely lost where to go from here.

Thanks

What is the bootloader sumcheck? It looks like you probably have the Lilypad bootloader.

The only difference I see is that in Nicks example it says to use Tools>>Board and choose "Lilypad Arduino w/ Atmega328" which is not on the list.

Try an earlier version of the IDE. I'm not sure what those options think are at the other end.

You could also try posting a clear photo of what you are doing.

Hi Nick,

Thanks. I've reverted to an earlier version 1.0.2

Is this the bootloader sumcheck?

MD5 sum of bootloader = 0x27 0xEB 0x87 0x14 0x5D 0x45 0xD4 0xD8 0x41 0x44 0x52 0xCE 0x0A 0x2B 0x8C 0x5F
Bootloader name: ATmegaBOOT_168_atmega328_pro_8MHz

I'm not sure what you mean by "It looks like you probably have the Lilypad bootloader." I am following your tutorial and want to use the minimal circuit (no crystal) so I type "L" in the serial monitor. I can get the bootloader onto several ICs. But cannot proceed to next stage to put the sketch on it.

There doesn't have to be any guesswork. In the bootloader uploader sketch (on the appropriate tab) you will find this:

// File =  ATmegaBOOT_168_atmega328_pro_8MHz.hex
// Loader start: 7800, length: 2048
// MD5 sum = 27 EB 87 14 5D 45 D4 D8 41 44 52 CE 0A 2B 8C 5F

So yes, it looks like the Lilypad bootloader.

So everything is working ... except actually using the bootloader. Possibly wiring error, possibly wrong board selected in the IDE. Please post a photo.

I didn't realize that there were tabs for programming the bootloader. So my understanding now is that I need to select the correct tab and upload this? I've tried bootloader_atmega328.h and bootloader_lilypad328.h (see picture for choice selection)

Then, Tools/Board/Select which one?

Then. Tools/Programmer?Select which one?

Here is a picture of my board with the serial programmer

I didn't realize that there were tabs for programming the bootloader.

What are you talking about? The IDE has tabs.

So my understanding now is that I need to select the correct tab and upload this?

No.

Here is a picture of my board with the serial programmer

What picture?

I am so sorry. I don't know what went wrong. I didn't want to add it as an attachment, so I used "insert an image"...which obviously did not work. What do you need a picture of?

Obviously I'm missing something in my knowledge. Please forgive me, and thank you so much for trying to help.

At this stage I do not believe the wiring is wrong. I've checked it many times. I follow your tutorial. All works until I get tot he sketch uploading. I think it may be the board selection and programmer type. I've tried several combinations.

Can we go one step back? I upload the board detector output (as per the tutorial). Check it works. Yes. Then I upload the board detector output. It works. In the serial monitor I get asked to enter "L" or "U". I choose "L" as I want the sketch to use the internal clock. Is "L" the right selection? If so, what board selection in the Tools menu do I use?

I am so sorry to add the pictures as an attachment (in fear of inserting the pics not working again)

bootloader_1.jpg

bootloader_3.jpg

Can we go one step back? I upload the board detector output (as per the tutorial). Check it works. Yes. Then I upload the board detector output. It works. In the serial monitor I get asked to enter "L" or "U". I choose "L" as I want the sketch to use the internal clock. Is "L" the right selection?

No, we don't need to go back. You have clearly uploaded the correct bootloader, as it verified with the sumcheck. Either your wiring is wrong, or you have the wrong board selected. The Lilypad board is the one I use (with the "L" option). Don't keep fiddling around uploading the bootloader, who knows what you will do. That part is OK.

What are those images showing me? I asked for a photo of your wiring, not a screenshot of your IDE menus.

(Maybe that wasn't clear).

Forget the bootloader uploader. Put it down and walk away. Don't touch it.

You need to have Lilypad selected in the IDE when you try to upload a sketch. You do NOT need to use the menu item "Programmer". Nor do you select "Upload using programmer". You do a straight "Upload" with the Lilypad as the target board type.

I think you are making some assumption here, that you are not even realizing you are making (eg,. that you need to use "upload using programmer" when you don't).

I just reprogrammed one my boards using the Lilypad bootloader (L option). Then I plugged that in, selected "Lilypad Arduino" in the IDE and successfully uploaded a sketch. The whole thing took a couple of minutes.

This was with IDE 1.6.5.