This is a part of MP Electronic Devices site.
The AVR family of microcontrollers, with ATmega and ATtiny series, are arguably the most popular microcontrollers, widely used for small hobby projects. Their popularity was established by the use of the ATmega328 microcontroller in the iconic Arduino Uno microcontroller board. They may not be the latest and best technology, but the large and widespread support for their applications outweighs any drawbacks.
While using the ATmega328 on an official Arduino board is standardised and therefore easy, using it in other environments, or using other chips can be tricky. The most difficult problem I faced was uploading machine code to the AVR's flash memory.
Please note that this web page gives only a general overview for the most common chips and the most common situations that arise when programming AVR microcontrollers, and that some issues can be greatly simplified.
Basically, the code is compiled and uploaded by what is called a device programmer. The code is uploaded using the Serial Peripheral Interface (SPI) protocol with three dedicated lines, SCK, MOSI and MISO. The upload is triggered by the pulse on the pin RESET, which means that the upload requires four chip pins.
Fortunately, Arduino Uno (or some of its relatives like Arduino Nano, Arduino Mini etc.) can be used as the hardware part and Arduino IDE as the software part of the programmer instead of using expensive vendor solutions. First, load the special in-system programming (ISP) sketch ArduinoISP onto the programmer. The sketch can be found under Examples in the Files menu of Arduino IDE. Select the appropriate board and the Arduino as ISP (not to be confused with ArduinoISP) as the programmer in the Tools menu. Then connect the MOSI (PB3), MISO (PB4) and SCK (PB5) pins of the programming device and the target device. In addition, connect the PB2 pin of the programming device to the RESET pin of the target device with a 10 kΩ pull up resistor to VCC. Finally, upload the code by selecting Upload Using Programmer in the Sketch menu.
At this moment, I am sure you are wondering: When we uploaded code to another device, Arduino Uno was a programmer. However, when we uploaded code - ArduinoIDE - Arduino uno was a target. So who is programming the programmer? This is the key question to understand if you want to understand AVR microcontroller programming.
The process described in the previous chapter is hardware built into AVR microcontrollers. However, there is a software process that uses a bootloader and allows computers, whether personal computers or Raspberry Pi, to act as programmers. A bootloader is a section of program memory that is executed at power-up or reset before the main code is executed. Bootloaders, typically loaded on AVR chips use the Universal Asynchronous Receiver-Transmitter (UART) protocol with two dedicated lines, RX and TX. The upload is again triggered by the pulse on the pin RESET, which means that the upload requires four chip pins. This protocol allows the AVR chip to be connected to the USB port of the personal computer or Raspberry Pi via a variety of USB-UART interface chips. Arduino Uno usually uses an ATmega16u2 chip, but there are many alternatives, such as CH340G, FT232RL, PL2303 and CP2102 chips.
Select the appropriate board in the Tools menu. Then connect the TX and RX pins of the interface chip to the RX (PD0) and TX (PD1) of the target device, respectively. In addition, connect the DTR pin of the interface chip to the RESET pin of the target device with a 10 kΩ pull up resistor to VCC. The DTR pin is connected through 100 nF capacitor. At the beginning of the transmission, DTR changes its state and the capacitor converts this change into a pulse. Finally, upload the code by selecting Upload in the Sketch menu or simply clicking the Upload button. The selection of the programmer in the Tools menu is neglected.
Before the bootloader can be used, it must of course be uploaded to the chip. If a chip is used on an Arduino board, the bootloader is usually already uploaded with a default bootloader named Optiboot. Otherwise, you will need to use the hardware upload method. The only difference is that instead of Upload Using Programmer in the Sketch menu, you use the Burn Bootloader in the Tools menu is used. Note that uploading the sketch with the hardware method overwrites the bootloader.
Note that uploading the sketch with a hardware upload method overwrites the bootloader!
The ATmega series microcontrollers used on Arduino boards (ATmega328, ATmega32U4) are natively supported by Arduino IDE. However, for the ATtiny series, board managers must be included in the program. In the past there were two board managers, Attiny by David A. Mellis for hardware upload and DigistumpArduino for bootloader upload, which have now been superseded by the ATTinyCore manager that supports both.
Adding the board manager is a rather tedious procedure. First, add URL http://drazzy.com/package_drazzy.com_index.json to Additional Boards Manager URLs in Preferences in the File menu. Then go to Boards Manager in the board selection in the Tools menu and install ATTinyCore.
The problem is that the ATtiny series microcontrollers do not support the hardware UART protocol. Therefore, instead of using the Optiboot bootloader, it is much more common to use another bootloader named Micronucleus, provided by both Digitump and ATTinyCore board managers. Instead of using the UART protocol through a USB-UART interface, Micronucleus directly uses the USB protocol with two dedicated lines, D+ and D-.
Select the appropriate board in the Tools menu. Then connect the D- and D+ USB lines to the PB3 and PB4 pins of the target device, respectively. Both connections should be made using 68 Ω resistors and 3.6 V zener diodes to clamp the ATtiny's 5 V signals to the USB standard. Finally, upload the code by selecting Upload in the Sketch menu or by simply clicking the Upload button. The selection of the programmer in the Tools menu is neglected.
The bootloader upload procedure is described in the previous section. Note that uploading the sketch with a hardware upload method overwrites the bootloader!
When you buy an AVR microcontroller as a chip, its fuses (default settings) must be adjusted. This is done by the bootloader upload procedure. If no bootloader is used, this procedure only burns fuses.
The USB bootloader upload method is apparently starting to be non-operational with a lot of newer computers. The Micronucleus bootloader barely mimics the correct USB protocol, which may not be properly recognised by new chipsets and drivers.
Below is the pinout of the AVR microcontroller for reference: † internal oscillator, ‡ reset disabled.
|pin name||pin number|
Please use the feedback form to tell me what else you would like to see here, suggestions, new tricks, etc.
Created by Marko Pinteric: feedback form.
Updated . Web page has been read by visitors since September 2021.