K4ICY's Arduino ProjectsArduino Projects  By Mike, K4ICY  

Weekend Radio 
Click Here for More Electronics Projects and Tutorials By Mike Maynard, K4ICY

_

HOW TO PROGRAM A STANDALONE ATmega328p

.     [This ALSO applies to many other microcontroller IC's including the ATtiny85]

   ATmega328P-PU
       Let's assume you're already familiar with building circuits and programming 'sketches' around an Arduino IDE development platform such as the Uno Rev3, Nano, Mega and other more compatible boards.  In due time, you'll want to miniaturize or self-contain your Arduino project in a smaller enclosure or wearable, and you may also desire to make a permanent version of your project or many copies of it and using actual Arduino UNO's or NANO's isn't feasible. (Arduino Pro-Mini's are also $3+ ea.)  In these cases you'll want to use just the heart of the Arduino, the actual AVR microcontroller (MCU) IC such as the ATmega328p.

       When working with Arduino (AVR) microcontrollers, the ability to upload sketches to a blank ATmega328p microcontroller IC opens up a world of possibilities for custom projects.  By utilizing the ISP (In-System Programming) a.k.a. ICSP (In-Circuit Serial Programming) protocol, you can program a bare ATmega328p chip without the need for a preloaded bootloader.  Below, I'll describe several options for uploading your sketches, including using an Arduino development board like the Uno R3 or Nano, an off-the-shelf dedicated programmer like the USBasp, or an FTDI interface.

       Blank vigin MCU chips weren't easy to find during the pandemic and subsequent chip shortage, but are now plentiful again.  The prices aren't going to be as friendly as before due to inflation, and Microchip, who now makes the ATmega328p and other older AVR's is trying to sunset these 'obsolete' MCU families. But ultimately, bare chips are way cheaper than whole Arduino boards and there are still good stocks.  Expect to pay around $3 USB a piece for the 328p on DigiKey.  If you choose to build a custom PCB through a company like JLCPCB, PCBWay or OSHPark you can get your boards with a factory fresh SMD ATmega328p-AU pre-soldered on board for at least $7 a piece.  If you go the proto-board route, a homebrew option with the ATmega328p-PU DIP package will be the easiest.

       You'll most likely be starting off with 
a new chip which will be a blank slate as it will NOT come with a bootloader and you just can't wire it directly to a USB cable (don't even...)  The job of a bootloader is to provide a Serial-to-USB interface link between the MCU and the PC and the initial installation of a bootloader also sets some of the 'fuses' that sets crucial working parameters of the MCU. You'll have to use a programmer!


Please familiarize yourself with the .PDF manual for this family of Microchip/Atmel microcontrollers. [HERE]


       [Bear with me here as I build this page.  My goal is to make this an easy-to-understand and follow process.]


       Should I burn a Bootloader to my Atmega328p?


       Understanding the Bootloader and Why You Might Not Need It for a Standalone ATmega328p

       When setting up an ATmega328p microcontroller IC, one of the first steps is often to burn a bootloader onto the chip.  But what exactly is a bootloader, and do you always need it?

       A bootloader is a small program that resides in a dedicated section of the microcontroller's memory.  Its primary job is to manage the uploading of sketches from the Arduino IDE to the microcontroller via a USB interface, and it also sets certain configuration fuses and internal registers that define critical parameters like clock speed and memory allocation.

       Why Burn the Bootloader?

       For many standard Arduino boards, such as the Uno or Nano, the bootloader is essential because it enables communication between the microcontroller and your PC through a USB cable.  This makes it easy to upload new programs directly from the Arduino IDE.  However, when using an ATmega328p chip as a standalone device without a USB interface, the bootloader's role becomes less crucial.  The primary reason to burn the bootloader initially is to correctly configure the fuses and set up the microcontroller for the desired clock speed, such as 16 MHz with an external crystal.  Once this is done, the bootloader itself isn’t necessary for future sketches.


       Advantages of Skipping the Bootloader in Standalone Applications

       By programming your microcontroller directly through ISP, you can skip the bootloader, gain additional memory, and enjoy faster startup times — all without sacrificing functionality.

       •  Memory Savings:  The bootloader occupies a small portion of the microcontroller's memory, and by omitting it, you free up this space for your program, allowing for slightly larger sketches.
       •  Faster Startup:  When a bootloader is present, it introduces a delay during startup as it waits for potential incoming communication from the IDE over USB.
               Without the bootloader, your program starts running immediately upon power-up.

       Programming Without the Bootloader

       If you plan to use the ATmega328p as a standalone device without a USB interface, you’ll typically program it using an ISP (In-System Programming) method, such as an Arduino configured as an ISP (Arduino as ISP) or a dedicated USBasp programmer.  Once the bootloader is burned initially to set up the fuses, you can load subsequent sketches directly through the ICSP (In-Circuit Serial Programming) port, bypassing the bootloader entirely.  This approach is ideal for projects where you need to maximize memory usage and reduce startup time.  It also simplifies the microcontroller’s operation, as it will no longer wait for USB communication before executing your code.

       While the bootloader is essential for USB-enabled Arduino development, it’s not always necessary for standalone ATmega328p applications.  Keep these two things in mind:

       •  You are required to burn the bootloader just once at first on any blank AVR microcontroller chip you plan to use a programmer on so that crucial fuses can be set.
       •  Anytime you upload a sketch via an ISP programmer - your bootloader will be deleted (unless you can specify for it not to).
             If you try to place your ATmega328p IC into the blank socket of an Arduino Uno without a bootloader, it won't work!  Just burn a bootloader before doing so.

.

       Please check out these YouTube videos to learn more about running the ATmega328p on its own
       outside of an Arduino board as well as how to burn an Arduino bootloader. :

_
  
_
 
  


         The method in this video relies on the ATmega328p IC already being initially loaded with a bootloader and
         simply acting in a remote fashion as to when it is programmed.  This is not the direct ISP programming
         method we'll be discussing below but may be a more desirable method for ease of use.


.
 
  

         Sine Lab shows you how to Remove the ATmega from your Arduino!

.
         And also read the tutorial provided by Arduino: https://www.arduino.cc/en/Tutorial/BuiltInExamples/ArduinoISP

.

A WARNING:

       It is possible to lock up (or "brick") your target Arduino board or AVR microcontroller chip in the process of programming,
       especially if the Reset line is interrupted, an incorrect setting is sent or any particular anomaly.  Fixing your device
       would then require a High-Voltage Programmer.  Do not start off following this on more important projects
       but first try a few practice runs. - The builder following this tutorial does so at their own risk.

       See BELOW on building and using an AVR High Voltage Parallel Programmer to un-brick your AVR micro...

.

Option 1: Uploading with Arduino Uno R3 (as ISP):

  Follow these steps to use your Arduino as a Programmer:

   BUT FIRST:

       Make sure to read FIRST read Arduino's tutorial for specific details that may vary from the below simplified step-by-step.


       With this option, there's NO need to purchase an ISP Programming Shield or the like.


   Step 1: Prepare the Arduino Uno R3:
       a. Connect the Arduino Uno R3 (Nano, etc.) to your computer via USB..
       b. In Arduino IDE, select "Arduino Uno" from the Board menu.
       c. Select the appropriate serial port in the Tools menu.
       d. Build the Programmer Circuit (As Shown Below) on your Arduino.

   Step 2: Connections Wiring:
           Connect the Arduino Uno R3 (Nano, etc.) to the ATmega328p IC as follows:
             - You can ALSO program another actual Arduino Uno (Nano, etc.)!

           Arduino Uno 5V  to  ATmega328p VCC [IC pin 7 / Arduino 5v pin]
           Arduino Uno GND  to  ATmega328p GND [IC pin 8 / Arduino Gnd pin]
           Arduino Uno 10 (SS)  to  ATmega328p RESET [IC pin 1 / Arduino Reset pin]
           Arduino Uno 11 (MOSI)  to  ATmega328p MOSI MOSI [IC pin 17 / Arduino pin 11]
           Arduino Uno 12 (MISO)  to  ATmega328p MISO [IC pin 18 / Arduino pin 12]
           Arduino Uno 13 (SCK)  to  ATmega328p SCK [IC pin 19 / Arduino pin 13]


   Step 3: Burn the Bootloader (optional - but do so once before uploading sketches):
       a. Select "Arduino as ISP" from the Tools menu.
       b. Select the appropriate board and processor from the Tools menu.
       c. 
Open the "Arduino as ISP" Example sketch, Upload this to your Arduino.
       d
. Choose "Burn Bootloader" from the Tools menu.

   Step 4: Upload Sketch to Blank ATmega328p IC (or target Arduino device):
       a. Prepare your desired sketch in the Arduino IDE.
       b. Select "Arduino as ISP" from the Tools menu.
       c. Select the appropriate board and processor from the Tools menu.
       d. Click "Upload Using Programmer" to program the target device with the sketch.

More descriptively:

>

       1)   Build indicators as shown here:

  ISP Programmer Indicators

_

        2)   Make connections between the programmer Arduino and the target device as shown:

  Arduino as Programmer - ICSP Connections
       
       Programmer Pins:
       
       Pin 13 - SCK
     
        Pin 12 - MISO
       
       Pin 11 - MOSI
       
       Pin 10 - Reset (on Target device)
.
              Alternatively, for a standalone setup for the ATmega328p make the protoboard connections as shown:

                  This setup will allow for burning a bootloader as well as any subsequently loaded sketches.
                 
The same setup will apply for the ATmega328p-AU SMD package and many other ISP capable AVR MCU's.

  Arduino Uno - Program ATmega328p on Protoboard

                  A few notes on operation speed:  A fresh blank and default ATmega328p IC out of the box will have certain base-level fuses
                  set from the fab.  Most crucially, is the operation speed where, instead of the chip being set at 16 MHZ, it will be set to use
                  the internal oscillator which runs at ~8 MHz with a pre-scale speed divider fuse set at /8 which means the chip will be set to
                  run at only 1 MHz and when a programmer tries to communicate with it, there may be a mismatch.
                  Arduino IDE with the "Arduino as ISP" setup, as pictured in the wiring diagram below, will automatically set these fuses which will
                  tell to the chip to run at 16 MHz with an external crystal from then on out.  If attempting to burn the bootloader fails using the
                  Arduino as ISP method, then first make sure that Port under the Tools menu is set to the detected Com Port that the
                  Arduino UNO (as programmer) is connected to.
                 
                  If using a USBasp programmer [Go HERE to learn more] you will NOT be able to burn the bootloader on a fresh chip
                  unless you bridge the jumper pins on the USBasp marked "JP3" or "Slow SCK" as the USBasp will then know to work with the
                  Atmega328p which will be running at 1 MHz.  Once the bootloader is burned through Arduino IDE, which will take at least 20 seconds,
                  Remove the jumper after successful bootloader uploading.  The chip will now be set to run at 16 MHz with an external crystal and
                  burning bootloader updates and uploading sketches will be possible at standard programming speeds.

                  USBasp Program Speed Jumper - JP3

                  For parts you will need:  One 10k pullup resistor from V+ to pin 1 on the IC.  For 16 Mhz operation, either use an external 16 MHz
                  oscillator package, or use a 16 MHz crystal, and two ceramic capacitors valued at the specified "load capacitance" of the crystal,
                  generally 20 or 22 picofarads. Also add a 1 Meg-ohm resistor in parallel to the crystal which is for stability but not crucial to operation.

                  Without those components, the ATmega328p would need to run on its 8 MHz internal oscillator and appropriate measures have
                  to be made, including using a specific bootloader along with running some setup features from the AVRDUDE command line.
                  Certain 'fuse' bits would also need to be set.  The 10 microfarad electrolytic capacitor is for power supply smoothing.

                  Once programmed, all wires from the programmer can be removed.

_

       3)   In the Arduino IDE choose "Arduino as ISP" (In-System Programmer) under the Tools menu:
              [Do NOT choose the "ArduinoISP" option as that is a specific Arduino product]

        Arduino As ISP

_

       4)   Open the "ArduinoISP" Example sketch in your IDE and UPLOAD this to your Arduino which will then make it the programmer:
              [Remember to first remove the capacitor from the Reset line if this procedure was done previously - see below]

        isp

             You should see the "Heartbeat" LED fading in and out, indicating that the programmer sketch is running.

            NOTICE:  Make sure the Board: option under the Tools menu reflects the device you're using as a programmer.

_

     5)   AFTER uploading ArduinoISP sketch - add a 10uF capacitor between Reset and GND on the programmer Arduino as shown to disable rebooting.

  Anti-Reset Capacitor on Arduino Programmer
       
       NOTICE:  Please note the polarity of the capacitor.  Remember to remove when uploading future sketches to this board in particular. 

_

       6)   To FIRST burn the bootloader or if you wish to make a fresh AVR IC an "Arduino"  -
             Under Tools - first choose the appropriate TARGET "Board:", then choose Burn Bootloader:

        Burn Bootloader

_

       7)   After uploading the bootloader, if you wish to directly upload a sketch to a stand-alone AVR IC -
             With the target device connected via the ICSP port, open your desired sketch and choose
             "Upload Using Programmer" from the Sketch menu.
             [Do NOT choose "Upload" as the IDE will attempt to overwrite the programmer sketch on the host Arduino.]

         Upload Using Programmer

       POWER SUPPLY NOTICE:  Make sure that your target board's power supply is isolated from the programmer!
        There could be a number of sensors or devices in your project that may not work well with power provided
        by your programmer including 3.3v items. You may try just not connecting the (+5v) line between the two,
        only sharing a common ground connection - as long as the voltage of each device is 5 volts.
        It is advisable to isolate the 5+ supply to the target AVR IC via a disconnect jumpers in your project, but even with that,
        in a complete circuit, the AVR IC may still unintentionally supply power to connected devices through the I/O
        and the current consumption of your target's incorporated circuit may damage your programmer.
        Consider programming the AVR IC by itself on a protoboard with its associated crystal and capacitors
        or put both devices on the same power supply.


        PROGRAMMING:  You should see the three indicator LEDs flashing in various sequences.  The Error LED should not stay lit and
        the Programming LED will now rapidly flash for a bit.  The bootloader install process should take just seconds
        but uploading a full 32k sketch may take more than half a minute.  If installed, you may also see the "built-in"
        (pin 13) LED on your target device flashing as well.  Wait until the Programming LED has gone off and the
        target AVR has rebooted before removing the connections and testing.


Consider building a dedicated programming 'shield' for you Arduino that uses the [above] wiring.
_
        SAVING EEPROM DATA:  If you wish to upload new sketches to your custom target Arduino/AVR without clearing or erasing any saved EEPROM Flash data on the AVR IC, consider using the FTDI header if you have designed one in.  You will have to have a bootloader resident, but as FTDI programmers are cheap and easy to find on Amazon, eBay and BangGood, otherwise, just upload the sketch through the ICSP port or write a sketch that will transfer your EEPROM data to the Serial Monitor which can be pasted into a sketch that will send it back to a clean AVR IC.  Just remember that a sketch or bootloader upload via ICSP will also wipe all stored data!
_
  Programming the ATMEGA328P-AU

.

Option 2: Using a Dedicated Programmer:

Dedicated programmers, such as the USBasp or AVRISPmkII, provide a direct method of programming ATmega328p ICs.

Follow these steps:

   Step 1: Prepare the Programmer:
       Connect the dedicated programmer to your computer via USB and install the necessary drivers and software required.
       A dedicated AVR programmer may require uploading your sketch through an IDE such as Microchip Studio.

   Step 2: Connections Wiring:
       Connect the programmer to the ATmega328p IC is straight foward:

           Programmer's 5V  to  ATmega328p VCC [IC pin 7 / Arduino 5v pin]
   
       Programmer's GND  to  ATmega328p GND [IC pin 8 / Arduino Gnd pin]
   
       Programmer's MOSI  to  ATmega328p MOSI [IC pin 17 / Arduino pin 11]
   
       Programmer's MISO  to  ATmega328p MISO [IC pin 18 / Arduino pin 12]
   
       Programmer's SCK  to  ATmega328p SCK [IC pin 19 / Arduino pin 13]
   
       Programmer's RESET  to  ATmega328p RESET [IC pin 1 / Arduino Reset pin]

   Step 3: Upload Bootloader to the Target Blank ATmega328p IC - If new from the the factory:
           a. Open the Arduino IDE and select the appropriate board (Arduino Uno or Nano] and/or target processor from the Tools menu.
           b. Choose the dedicated programmer (Arduino as ISP, USBasp, etc..) as the programmer from the Tools menu.

           c. If using a USBasp programmer (or similar), bridge jumper "JP3" for the "Slow SCK" mode as the new ATmega328p IC
               will be preset to run at 1MHz, otherwise the bootloader may fail to burn.
           d. Click "Burn Bootloader" under the Tools menu to initialize and set crucial fuses for further programming.

   Step 4: Upload Sketch to the Target Blank ATmega328p IC:
           a. Open the Arduino IDE and select the appropriate board (Arduino Uno or Nano] and target processor from the Tools menu.
           b. Choose the dedicated programmer as the programmer from the Tools menu.
           c. Prepare your desired sketch in the Arduino IDE.
           d. Click "Upload Using Programmer" under the Sketch menu to program the ATmega328p IC with the sketch.

.

...ATmega328p with USBasp

.
Option 3: Utilizing an FTDI Interface:

An FTDI interface can also be used to Upload Sketches to a prepared standalone ATmega328p IC and are also a cheap alternative to a dedicated AVR programmer.

IMPORTANT:  The FTDI cannot set fuses and burn bootloaders as those require an ISP interface - you must burn the initial bootloader with a programmer (see Option 1 or 2:)

Follow these steps:

   Step 1: Prepare the FTDI Interface:
           Connect the dedicated programmer to your computer via USB and install the necessary drivers
           for the programmer if required such as that for the CH340 IC (SEE HERE).


   Step 2: Connections Wiring:
       Connect the FTDI interface to the ATmega328p IC as follows:

           FTDI's 5V  to  ATmega328p VCC [IC pin 7 / Arduino 5v pin]
           FTDI's GND  to  ATmega328p GND [IC pin 8 / Arduino Gnd pin]
           FTDI's TX  to  ATmega328p RX [IC pin 2 / Arduino 0 pin]      - Remember that TX goes to RX
           FTDI's RX  to  ATmega328p TX [IC pin 3 / Arduino 1 pin]        and vice-versa!
           FTDI's DTR  to  ATmega328p RESET [IC pin 1 / Arduino Reset pin]

   Step 3: 
Upload Sketch to the Target Prepared Standalone ATmega328p IC:
           a. Open the Arduino IDE and select the appropriate board and processor from the Tools menu.
           b. Choose the correct serial port of the FTDI interface from the Tools menu.
           c. Prepare your desired sketch in the Arduino IDE.
           d. Click "Upload Using Programmer" under the Sketch menu to program the ATmega328p IC with the sketch.

.

       Uploading an Arduino IDE sketch to a blank ATmega328p microcontroller IC using the ISP (ICSP) protocol provides flexibility and customization options for your projects.
       It should be easy to use your trusty Arduino Uno R3 or Nano and a solderless prototype board, and using an FTDI adapter should only set you back a few bucks.

       More updates are planned for this section.
       Please feel free to correspond with me on relevant corrections or suggestions.

.


Nanos And Clones Waste Sketch Space!

       You may be using the wrong bootloader!

       Yes, if you run an Arduino Nano (whether genuine or clone) or any clone "Arduino",  you may not be using the new, improved and much smaller bootloader.  You may also have a much slower upload speed.  This should concern you because both the Uno R3 and Nano use the SAME die chip!

       Don't worry!  The YouTube channel, Design Build Destroy will get you in the know and guide you step-by-step to getting every
       ounce from your Arduino:

_

  
__
  
_

A Parallel Programmer to Un-Brick your AVR Microcontroller:

       So you've somehow killed your ATmega328p or other AVR micro in the process of programming via ISP!   Learn how to recover the full potential of your AVR by manipulating the fuse bits and salvaging 'bricked' chips.  Sine Lab gives you a step-by-step demonstration to guide you through the setup of your own parallel programming system.  Learn how to interrogate your Arduino's brain by hand on a protoboard and build a permanent rescue board.  Also works on many other 28-pin AVR chip varieties.

Download the sketch HERE:  https://sinelab.net/code/parallel_programmer.c
Download the schematic HERE:  https://sinelab.net/pdf/parallel_programmer.pdf


VISIT Sine Lab's Site: [HERE]

.

  
__

Updated 09/02/24

(c)2024 Copyright - Michael A. Maynard, a.k.a. K4ICY