Programming ATMega328P to Use Internal Clock From External Clock With Arduino As ISP

by zactaylor in Circuits > Microcontrollers

479 Views, 1 Favorites, 0 Comments

Programming ATMega328P to Use Internal Clock From External Clock With Arduino As ISP

aruduino_wired_to_atmega_with_16mhz_external.jpg

*CORRECTION TO WIRING IN STEP 1*


I figured out how to program an ATMega328P to use it's internal clock. There are many similar guides out there but none of them quite worked for me. After gathering information from these sources and hours of debugging and trial and error, I figured out what I was doing wrong--and what to do to get it right. I came here to write this guide in the hope of saving you some of the time and pain I endured figuring this out.


Some of the sources I used:

[1] https://medium.com/@srmq/using-arduino-board-as-isp-to-program-atmega328-ic-without-a-crystal-667ef0fccf4

[2] https://docs.arduino.cc/built-in-examples/arduino-isp/ArduinoToBreadboard

[3] https://forum.arduino.cc/t/burning-bootloader-on-standalone-atmega328-internal-clock-8mhz/297586

[4] http://oddwires.blogspot.com/2012/06/burning-bootloader-into-bare-atmega328.html

Supplies

  • 16 MHz Crystal oscillator
  • Jumpers
  • Breadboard
  • ATMega328P ('P' has bootloader pre-flashed) or ATMega328 (no 'P' does not have bootloader)
  • Arduino Uno (possible other Arduinos will work too) with serial to USB
  • Arduino IDE (done with 1.8.x but newer versions will also possibly work)


Many similar guides will recommend that your crystal oscillator has two 18-20pF capacitors connected to it as well as a 10kOhm resistor connected to the reset pin [2] but this is the version with the fewest components that I could come up with that still works.

Wiring

ProgramATMega328_BaseWiring_bb.jpg
ProgramATMega328_ExternalClock_bb.jpg

An ATMega328 configured to run with an external clock will not accept sketches or bootloader burn attempts if there is no external clock properly wired. However, an ATMega328P configured to run without a clock will run regardless of the presence or lack of a clock. Therefore, wire your ATMega328P according to figure 2.

If, by chance, you don't have an external clock you can try wiring your ATMega328P according to figure 1. If you are lucky and it was already configured to run with no external clock (that is, to use it's internal clock), then you will succeed in burning the bootloader or uploading the sketch.

Essentially, this wiring is what enables the bootloader to be burnt onto the board. The bootloader burning phase is when we can change the fuses on the target (standalone) ATMega328P so that we can tell it what clock to use.

This is the generic Arduino as ISP wiring but without the indicators LEDs.


*Wiring now correct*

Prepare IDE

To enabled the Arduino IDE to burn a bootloader or upload a sketch to your ATMega328P, you will need to clone/copy some files from a Github repo called ATmegaBreadboard. This repo will give you menu options for and the capability to do so.

In your sketchbook location, as mentioned in the IDE's preferences, typically in your user's Documents directory, C:\Users\%username%\Documents\Arduino or ~/Document/Arduino, add a directory named hardware and clone the repo or copy the relevant files and directories into it.

Please see reference 2 for more details, specifically the helpful GIF with the caption of "Cloning ATmegaBreadboard repository to have board corresponding to ATMega328P without crystal."


The end goal is to have

%sketchbook_location%\hardware\atmegabreadboard\avr\bootloaders\empty\empty_all.hex

%sketchbook_location%\hardware\atmegabreadboard\avr\boards.txt

%sketchbook_location%\hardware\atmegabreadboard\avr\platform.txt


Use IDE to Write to ATMega328

Connect your Uno to your PC via the serial to USB cord.

Select it in the Arduino IDE.

Open the example sketch ArduinoISP (File>Examples>ArduinoISP>ArduinoISP)

Burn the ArduinoISP onto your Uno as you normally would burn a sketch.


Now to prepare to write to the ATMega328.

Under Tools you should have

Board: ...

Chip: ...

Clock: ...

B.O.D.: ...

To burn the bootloader that tells the ATMega328 to use it's internal clock configure those options as follows:

Board: ATMega328

Chip: ATMega328P

Clock: 8 MHz (internal)

The B.O.D. should be set according to what voltage you will be using to power your ATMega328.

Make sure the Programmer option (still under Tools) is set to Arduino as ISP (not ArduinoISP).


Now burn the bootloader with "Burn Bootloader"


Now you will be able to upload sketches that will use the internal clock! When doing this just make sure that that all the above configuration

Board: ATMega328

Chip: ATMega328P

Clock: 8 MHz (internal)

and

Programmer: Arduino as ISP

remain set.