website/content/blog/atmega328p-standalone/index.md

63 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

+++
title = "Standalone Atmega328P guide"
date = 2022-09-12
description = "Simple guide to program a standalone Atmega328P using an Arduino."
insert_anchor_links = "left"
[taxonomies]
tags = ["electronics", "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](https://www.instructables.com/Capacitance-Meter/) to find the wanted ones.
## Circuit
{{ float_img(alt="Circuit for programming the Atmega using an Arduino.", src="circuit.png", style="max-height:100vh;max-width:100%;") }}
Check Atmega328P's pinout [in the datasheet](atmega328p.pdf).
* 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:
1. Open the example sketch `ArduinoISP`.
1. If using an Arduino Uno, uncomment the line `#define USE_OLD_STYLE_WIRING`.
1. Choose your programmer board as board type (_Arduino Micro_ in my case).
1. Choose a regular programmer (typically _AVR ISP_).
1. Upload the sketch normally.
Now we can program the microcontroller:
1. Open the wanted sketch. (or start with the `Blink` example)
1. 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)
1. Choose _Arduino as ISP_ as programmer.
1. If your microcontroller doesn't contain a bootloader yet, upload the bootloader using _Tools -> Burn bootloader_.
1. Upload the sketch using _Sketch -> Upload using programmer_.