Disclaimer: This is not a full tutorial, it's a list of notes about "what" needs to be done, often without saying "how" and "why". Also, many things are left unexplained, e. g. pin mapping between an Arduino and an ATmega328P, what are RX/TX/DTR pins, etc.
Here are the steps needed when replacing an Arduino Nano board with a standalone ATmega328P microcontroller.
We will use the following sample project to illustrate the migration:
The LED is connected to D7, the push button is connected to D8. When the button is pressed, the LED lights up.
The code for this functionality is very simple:
void setup() {
pinMode(7, OUTPUT);
pinMode(8, INPUT);
}
void loop() {
digitalWrite(7, digitalRead(8));
}
Let's now replace the Arduino Nano on the breadboard with an ATmega328P microcontroller and keep the same functionality of the circuit.
This process involves two steps:
- Burning a bootloader onto the ATmega328P (if not already there) - there are two options, see below
- Programming the ATmega328P using a USB-to-Serial converter
Step 1: Burning a bootloader onto the ATmega328P
We have two options. The first one is to use the ATmega328P together with a few external components: a 16 MHz oscillator and a couple of capacitors. In this setup, we would basically use these additional components to replicate a regular Arduino board.
The second option is more minimalistic - we can rebuild the circuit without the need for the external components. In this setup, we can use the 8 MHz oscillator that is built directly into the ATmega328P.
We will choose this second minimalistic option here (we will still use an Arduino UNO board to help us with burning the bootloader though).
What we need:
- an Arduino UNO board (or an equivalent Arduino board) - to use it as a bootloader programmer for the ATmega328P
- a breadboard and a couple of wires
- the target ATmega328P
First, we need to add new boards into the Board Manager in the Arduino IDE: https://github.com/MCUdude/MiniCore?tab=readme-ov-file#how-to-install.
The goal is to have these items available in the menu:
Once we have these items available, we can open the ArduinoISP example project and upload it to the Arduino UNO as usual:
Now we need to place the ATmega328P on the breadboard and connect it to the Arduino UNO:
Once the wiring is complete, we need to select the "ATmega328" item in the Board -> MiniCore menu:
We also need to make sure that Clock is set to "Internal 8 MHz" and that Programmer is set to "Arduino as ISP":
The final step is to click on "Burn Bootloader" in the Tools menu:
If no error is returned, the bootloader should be successfully burnt onto the ATmega328P.
We can now proceed to the next section and migrate the sample LED project from the Arduino Nano to the standalone ATmega328P chip.
Step 2: Programming the ATmega328P using a USB-to-Serial converter
Now we need to replace the Arduino Nano from the sample project with the ATmega328P. We will also connect the USB-to-Serial converter that will take care of uploading the project code to the ATmega328P. The wiring needs to look like this:
The USB-to-Serial converter will provide 5V/GND to the circuit using the VCC/GND pins, so we don't need an external power source for the ATmega328P for now.
And finally, we can take the project code from the top of this article,
void setup() {
pinMode(7, OUTPUT);
pinMode(8, INPUT);
}
void loop() {
digitalWrite(7, digitalRead(8));
}
make sure that the board settings in the Arduino IDE are correct,
and hit the Upload button to upload the project to the ATmega328P.
If all went well, the project is now uploaded to the ATmega328P. We can now safely remove the USB-to-Serial converter, connect an external power source and enjoy the result!