how can i program a new ATmega328p containing the Optiboot bootloader?

hi guys, some days ago i bought an arduino uno board because i really liked it's very easy and performing, so i downloaded on my pc the ARDUINO program and drivers, connected the board to the PC, i setted correctly the programmer and the serial port and i loaded the on-off led program example into its memory and everything worked fine ( i was obviously using the microcontroller i received with my board). Then, i decided i wanted to use another ATmega328p microcontroller (bought on amazon), and using the same settings i used for the previous microcontroller, i couldnt make it work, the led doesn't even turn on! i thought i broke the board so i tried to re-program the first microcontroller and it worked again so i feel quite confused :confused: why can't i make it work?

Are you sure the ATmega328P you bought on Amazon has a bootloader on it?

You are supposed to use a current limiting resistor on LEDs unless that one has a built in resistor.

Exactly what pert says.

Unfortunately, some sellers advertise their ATMega328P chips as having a bootloader installed when in reality they do not. I got caught by this once, so now only buy completely blank chips and load a bootloader myself. Besides other considerations, it's a good way to ensure that you have the ~0.5KB 'Optiboot' bootloader, rather than the earlier ~2KB bootloader.

Regarding the LED, I read a tutorial yesterday, (linked from a thread in the "Education and Teaching" section of these forums), which said it was OK to connect an LED to pin 13 without a current-limiting resistor, but that was an error in the tutorial that has since been corrected.
Apparently on early UNOs, according to the tutorial author, there was an onboard current-limiting resistor attached to pin 13, but that's not the case with the R3 board.

In case you're interested in checking it out, I've attached the UNO R3 schematic to this reply.

Arduino_Uno_Rev3-schematic.pdf (81.4 KB)

thanks for helping :slight_smile: today i noticed that on the back of the microcontrollers there are 2 different codes, even if they are both ATmega328p microcontrollers:

  • ATmega328 i received with arduino:
    A82JXB 35473D 1-P1549 e3

  • ATmega328 bought on Amazon:
    A82GFA 35473D 1-P1549 e3

i hope this is not rilevant for the microcontroller's working..

Anyway, the microcontrollers's amazon shop in its ad says the Optiboot bootloader has been inserted in, but i'm not really sure it's true because everything should work! IS THERE A WAY TO CHECK IT OUT? CAN I LOOK ON THE MICROCONTROLLER'S MEMORY LOOKING FOR THE BOOTLOADER SOMEHOW? :slight_smile:

stefanoyoyo:
IS THERE A WAY TO CHECK IT OUT? CAN I LOOK ON THE MICROCONTROLLER'S MEMORY LOOKING FOR THE BOOTLOADER SOMEHOW? :slight_smile:

Yes, you can read the chip to find out if a bootloader is installed.

I'm not sure (Edit:if you can do it) how to go about it using avrdude and a USB connection, but it can definitely be done via ISP. You need a working UNO board and the new chip set up on a breadboard, complete with 16MHz crystal or resonator. Then you can run Nick Gammon's "Atmega_Board_Detector.ino" sketch.

It's pretty late here, so I can't dig up the exact details, but this page will help:-

Edit: Ignore the codes on the chips - they have nothing to do with your problem.

if i tried to load a new bootloader even if there is alrady a bootloader in the microcontroller, would it get damaged somehow? :slight_smile: if it doesn't get damaged i could try directly to load a new one..

If you are working with Arduino I recommend you invest in an ISP device so you can refresh/update the bootloader if you ever feel like it. You can get a USBasp clone from the far east for well under $5 via eBay. Search for "USBasp", Buy It Now, and sort by "Price + Shipping: lowest first":

http://www.ebay.com/sch/i.html?_sop=15&_nkw=USBasp&LH_BIN=1

Most come with just a 10-pin ISP cable and the Arduino uses a 6-pin ISP connector so you will need an adapter. At or near the top of the list you should see adapters, two for $0.99. Add one of those to your cart. Then scroll down until you find the USBasp devices (little circuit board with USB plug at one end and 10-pin cable at the other). Currently the lowest price is $1.66+$0.11 shipping ($1.77 total). (For a couple dollars more you can get a fancy one with a plastic case but some clear packing tape or shrink tubing will protect the bare board well enough.) Add one of those to your cart. Check out and pay and you will have your devices in a few weeks.

If you want it FAST you can look for an eBay vendor in your country but it will cost more. For example if I click on the eBay option for "Item Location: North America" the cheapest price for the USBasp jumps to $3.49 and another $3.53 for the 6-pin adapter. Still, $7 isn't bad and it should arrive in days instead of weeks. If you want it in two days and have Amazon Prime you can get them for $5-6 ($12, including the adapter).

I read on the web it's better using an ISP device to load the ATmega328p bootloader. i didn't really understand how to use it.. :confused: should i connect the arduino board to the ISP device to flash it? Does it only flash the bootloader or can i flash codes too? besides, is it true it's better using aISP device than using two arduino boards to burn the bootloader? thanks for helping :slight_smile:

stefanoyoyo:
i didn't really understand how to use it..

  • Plug the ISP into the 6 pin ICSP header on the Arduino Uno. If your ISP has a 10 pin connected then you need an adapter or connect it with jumper wires.
  • Plug your ISP into the USB port on your computer.
  • If you're using an AVRISP mkII you will also need to power the Uno by plugging it into USB or the power jack
  • Choose the correct programmer from the Tools > Programmer menu.
  • Tools > Board > Arduino/Genuino Uno
  • Tools > Burn Bootloader
  • Wait until the burn bootloader process completes successfully.
  • Unplug the ISP USB.
  • Unplug the ISP from the ICSP header.

stefanoyoyo:
Does it only flash the bootloader or can i flash codes too?

You can also flash code via Tools > Upload Using Programmer(or press shift while clicking the upload button) but you need to to Tools > Burn Bootloader first to set the fuses on your ATmega328P. Note that when you do Upload Using Programmer the bootloader is erased so you will need to do Burn Bootloader again before you can upload using USB as normally.

stefanoyoyo:
is it true it's better using aISP device than using two arduino boards to burn the bootloader?

It's certainly more convenient just to have a dedicated device for the task, no need to set up and Arduino as ISP every time. It's faster and there are some limitations with Arduino as ISP, it won't work for Upload Using Programmer on ATmega2560, though some ISPs have the same problem. You can buy the USBasp for around $2 on aliexpress. I recommend getting the one with the 6 pin cable(unless you want to mess with the UART on the 10 pin one) otherwise you will need a 10 to 6 pin adapter. The USBasp is my favorite programmer, and I own all the most common ones.

stefanoyoyo:
Does it only flash the bootloader or can i flash codes too?

You can use Sketch->Upload Using Programmer to upload a sketch using the ISP device. Be warned that your sketch will override the bootloader so you won't be able to upload via USB-Serial again until you burn a fresh bootloader.

stefanoyoyo:
if i tried to load a new bootloader even if there is alrady a bootloader in the microcontroller, would it get damaged somehow? :slight_smile: if it doesn't get damaged i could try directly to load a new one..

No, you won't do any harm by simply writing a new bootloader even if one already exists.

And I agree with Jon and pert that it's well worth investing in a USBASP or similar ISP programmer. That's how I burn bootloaders too. I only suggested using your UNO because I knew you wouldn't have a programmer yet. :slight_smile:

today i bought the 10-6 pin adapert ( http://www.ebay.it/itm/252027098438?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT )

and the USBASP programmer ( http://www.ebay.it/itm/252027098513?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT )

i only hope i'll be able to make the microcontrollers i bought in which i can't load any sketch work by burning the bootloader using the programmer .. i decided to buy a USBASP, i hope i didn't buy the wrong thing :'D

That one should be fine. I don't see a cable in the photo of the USBasp. I don't know if it's shown in another part of the listing. It's not loading fully for me for some reason.

A nice feature of the version of the USBasp you bought is that jumper allows you to select 5V or 3.3V.