Standalone Atmega328P guide
2022-09-12electronics, Arduino
This guide describes how to burn a program into an Atmega328P microcontroller, using an Arduino board as programmer and an external 16MHz oscillator. It's the result of the frustration that every single time I wanted to to this, it was a struggle because I couldn't find any complete tutorial. At least, the following works for me.
#Materials needed
- Atmega328P (the P matters)
- Arduino board (I used an Arduino Micro)
- 16MHz oscillator
- 2× 22pF capacitors
- 10kΩ resistor
- 100µF capacitor
#Notes
If you don't have any 22pF capacitor, you can salvage some capacitors from an old device (e.g. there are plenty in videotape readers). Unfortunately through-hole capacitors with this value look pretty rare, so you may need to salvage a bunch of unmarked surface-mounted ones and build a simple Arduino-based capacitance meter to find the wanted ones.
#Circuit
Check Atmega328P's pinout in the datasheet.
- Connect the MOSI, MISO, SCK(=SCLK) pins on the Arduino to the corresponding ones on the Atmega. (on Arduino Uno, they are respectively on pins 11, 12, 13)
- Connect Arduino pin 10 to Atmega's Reset pin.
- Connect each pin on the oscillator to the XTAL/TOSC pins on the Atmega, and place a 22pF capacitor between each of these two pins and the GND.
- Connect the GNDs together. Connect Atmega's VCC, Atmega's AVCC and Arduino's +5V together.
Triple-check the wiring. MOSI goes on MOSI and MISO goes on MISO, they are not swapped.
#For normal operation
The following connections are only needed when the microcontroller is disconnected from the Arduino.
- Place a pull-up resistor on the Reset pin (10kΩ resistor between Reset and VCC).
- Place a 100µF capacitor between GND and VCC.
- The oscillator and ceramic capacitors are still needed.
- VCC and AVCC still have to be connected.
#Program
I'm using the Arduino IDE.
First, we program the Arduino board to make it operating as a relay between the computer and the microcontroller:
- Open the example sketch
ArduinoISP
. - If using an Arduino Uno, uncomment the line
#define USE_OLD_STYLE_WIRING
. - Choose your programmer board as board type (Arduino Micro in my case).
- Choose a regular programmer (typically AVR ISP).
- Upload the sketch normally.
Now we can program the microcontroller:
- Open the wanted sketch. (or start with the
Blink
example) - Choose Arduino Uno as board type. (even if you're using another board as a programmer; it's because a standalone Atmega328P with a 16MHz oscillator behaves like an Arduino Uno)
- Choose Arduino as ISP as programmer.
- If your microcontroller doesn't contain a bootloader yet, upload the bootloader using Tools -> Burn bootloader.
- Upload the sketch using Sketch -> Upload using programmer.