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 that you are already familiar with building circuits and programming 'sketches' around an Arduino 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 cost is a concern.  In these cases you'll want to use just the heart of the Arduino, the actual AVR microcontroller IC (MCU) 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, or an FTDI interface.
       Blank vigin MCU IC's are easier to find again recently after the worst of the international chip shortage, whether on eBay or Digi-Key.  The prices aren't going to be as friendly, which is more so because Microchip 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.  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 on board.  Or, 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 'virgin' 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?

       Yes and no.  A bootloader is a small program that will sit at the beginning of your AVR microcontroller's memory and acts as an interface controller that tells your Arduino where to store and run the sketch that is loaded from your IDE.  It also sets certain fuses and internal registers that tell the AVR chip important details such as clock speed and memory allocation.  If you are building a clone Arduino which would also include the interface controller, then the bootloader is always necessary, but if you're using the AVR as a stand-alone device then you will only need to 'burn' the bootloader ONCE to set the initials, subsequently only needing to load the program (even overiding any existing bootloader.)  A bootloader will always add a bit of time upon power-up to start running your program.  Without the bootloader, the program will essentially start immediately.

       Most likely, as you plan to use the microcontroller by itself without a USB interface like the ATmega16U2-MU IC which is integral to the Arduino Uno, Nano, and etc., as you won't be able to plug up a USB cable and go, you'll have a least two options for a programming interface, one being an actual Arduino configured as programmer, or use an FTDI TTL Programmer (pins are provided on the Main Board PCB.)  If you use an FTDI interface (via the ICSP In-Circuit Serial Port) you'll need to first establish the bootloader.  Please NOTE: your AVR will NOT require a bootloader to be present after the initial burn if it will be used as a stand-alone dedicated device for any subsequent sketches.  That will give you a bit more space as well as speed up your microcontroller's boot time, but you do have to burn the bootloader first so that the required fuses and registers are set - and - you'll have to load your sketches through the ICSP port.  But whether you wish to establish a bootloader for dev-board use or wish to upload the Columns game sketch to a blank IC (sans bootloader,)  it's an easy process!  We'll just have to use an existing Arduino board as the middle-man, connecting your target device via its ICSP port.  And conveniently, both the initial bootloader and stand-alone sketch can be uploaded via this port.

.

       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 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.


.
 
  

         Sin 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 MCU device 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 (Nanp, 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.
                  For 16 Mhz operation, use a 16 MHz crystal, two 22 picofarad capacitors and a parallel 1 Meg-ohm
                  resistor (for stability) as shown.  Without these components the ATmega328p would run at 8 MHz
                  using its internal oscillator, however, appropriate measures have to be made including a specific bootloader
                  and certain fuse settings.  The 10 microfarad electrolytic capacitor is for power supply smoothing.
                  Once programmed, all wires from the programmer Arduino can be removed.

  Arduino Uno - Program ATmega328p on Protoboard

_

       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 DATE:  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 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.

.
Option 3: Utilizing an FTDI Interface:

An FTDI interface can also be used to program a blank ATmega328p IC and are an extremely cheap alternative to a dedicated AVR programmer.

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 Blank 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.

       This article could really use some help!  If you have any error corrections or suggests - make sure they are EASY for me to figure out or even cut-and-past - and please contact me at mikek4icy@gmail.com.

.


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 06/14/23

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