If you’ve accidentally uploaded some code to the Arduino Pro Micro board as “Arduino Micro”, “Arduino Leonardo” or any other board, there is a big chance to brick your board by crashing its bootloader so it won’t be recognized by the PC at all anymore.
In my case, I've uploaded a code to the Arduino MICRO using the default Arduino MICRO board from the IDE. Once uploaded, my PC never recognized the microcontroller anymore. We need to burn another bootloader to it. I've tried the default bootloader but didn't work. That's why we burn the one from SPARKFUN. By default, “Arduino Pro Micro” is not listed in the selection of boards in the Arduino IDE as it is the Arduino-compatible board developed by SparkFun. In this note i will show how to add the SparkFun boards, including the “Arduino Pro Micro”, to the board selection menu in the Arduino IDE.
Arduino Pro Micro Board Selection
In the Arduino IDE open “Preferences"
Top Menu -> File -> Preferences
Towards the bottom of the window, paste this URL into the “Additional Boards Manager URLs” text box:
Open “Boards Manager”:
Top Menu -> Tools -> Board: "Arduino Uno" -> Boards Manager...
Search for the SparkFun boards and install the “SparkFun AVR Boards” that include the desired “Arduino Pro Micro” board:
Once the SparkFun boards are installed you will be able to select the “SparkFun Pro Micro”:
Top Menu -> Tools -> Board: "Arduino Uno" -> SparkFun AVR Boards -> SparkFun Pro Micro
As well as to select the ATmega32U4 processor’s voltage and frequency:
Top Menu -> Tools -> Processor: "ATmega32U4 (5V, 16MHz)"
The behaviour described above happens thanks to a special piece of code that is executed at every reset of the microcontroller and that looks for a sketch to be uploaded from the serial/USB port using a specific protocol and speed. If no connection is detected, the execution is passed to the code of your sketch.
This little (usually 512 bytes) piece of code is called the "Bootloader" and it is in an area of the memory of the microcontroller - at the end of the address space - that can't be reprogrammed as a regular sketch and had been designed for such purpose.
The Memory Map of an ATmega328P
To program the bootloader and provide to the microcontroller the compatibility with the Arduino Software (IDE) you need to use an In-circuit Serial Programmer (ISP) that is the device that connects to a specific set of pins of the microcontroller to perform the programming of the whole flash memory of the microcontroller, bootloader included. The ISP programming procedure also includes the writing of fuses: a special set of bits that define how the microcontroller works under specific circumstances.
Use Arduino as ISP
The whole process of loading the bootloader code and burning properly the fuses to make an ATmega microcontroller an "Arduino" is managed by the Arduino Software (IDE): it provides a specific menu item and allows you to use a variety of programming devices. Among the programmers, the "Arduino as ISP" is the cheapest and most practical solution to burn a bootloader on another Arduino board with ATmega, 32U4 or ATtiny.
The programming process uses VCC, GND and four data pins. Three pins connect MISO, MOSI and SCK between the programming micro and the target micro, the fourth pin from the programming micro goes to the reset pin of the target.
How to wire your boards
The following table display on which pins the MOSI, MISO and SCK are broken out on the different Arduino boards:
|UNO or Duemilanove||11 or ICSP-4||12 or ICSP-1||13 or ICSP-3||5V|
|Mega1280 or Mega2560||51 or ICSP-4||50 or ICSP-1||52 or ICSP-3||5V|
|101||11 or ICSP-4||12 or ICSP-1||13 or ICSP-3||3.3V|
The SPI interface - and therefore these pins - is the interface used to program the AVR microcontrollers. Note that MISO, MOSI, and SCK are available in a consistent physical location on the ICSP header; this connector is used also by shields that rely on the SPI interface allowing the design of shields that work on every board.
On the Arduino UNO in the following image, we have highlighted in red the connections on the female strips; in yellow the ICSP connector that connects to the ATmega328P. Please note that the Rev.3 board has an ATMega 16U2 chip that manages the USB connection and also that chip can be reprogrammed via a dedicated connector labeled ICSP2, just above the ATMega 16U2 itself.
On some Arduino boards (see table above), pins MOSI, MISO and SCK are the same pins as digital pin 11, 12 and 13, respectively. That is why many tutorials instruct you to hook up the target to these pins. If you find this wiring more practical, have a define USE_OLD_STYLE_WIRING. This will work even when not using an UNO. (With the UNO board this is not needed).
Load the sketch
The Arduino that you will use as programmer needs a specific sketch. You find it under Examples > 11. ArduinoISP > ArduinoISP .
Going through the lines of the sketch you find a number of parameters that need to be set according to the target board. These parameters are, however, set by a specific file available for each bootloader/board supported by the Arduino Software (IDE). Other parameters are clearly explained by the comments and should be changed only if you know what you are doing. The sketch also supports three LEDs that give you a visual feedback about the programming process.
To upload the sketch to your Arduino board - the one that you will use as the programmer - you need to select board type and port, then you can proceed as usual.
Program the bootloader
If all the wires are set, you need to switch to the board type you want to program with the bootloader. This step is mandatory to select the proper bootloader code and the fuses configurations. The programming procedure checks the signature of the microcontroller before any writing action is taken, but many boards share the same microcontroller and each board has its own bootloader. The port remains the one of the ArduinoISP.
Choose "Burn bootloader" under tools and wait for the confirmation message in the lower part of the Arduino Software (IDE) interface. If you connected the LEDs you may follow the process visually.
The Serial Programming Mode
The programming process manages the three SPI lines (MISO, MOSI and SCK) according to the standard SPI programming protocol, the same used to read and write SD memory cards. The only difference with memory cards is the lack of a CS (Chip select) pin. On our AVR microcontrollers we use the RESET pin that halts the execution of any sketch or bootloader and puts the microcontroller in a specific state where it listens to the commands arriving from the SPI interface. The very first command that the protocol requires is the one that enters the microcontroller in the Serial Programming Mode.
Once this specific mode is active, we can write and read all the microcontroller programmable areas: Flash, EEPROM and Fuses. At the end of the Flash memory, we have the bootloader code area, as highlighted in the image at the beginning of this article. The "Burn Bootloader" procedure also sets properly the fuses of the microcontroller according to the design of the board. This is one of the reasons why you have to burn the bootloader selecting your exact board model in the list.
Technical aspects of programming
The open source software tool used to program the microcontroller is avrdude. The process goes through four steps: unlocking the bootloader section of the chip, setting the fuses on the chip, uploading the bootloader code to the chip, locking back the bootloader section of the chip.
The fuses are managed according to the preferences stored into each parameter file associated with the board, avoiding potential mistakes.
The management of fuses, usually a set of three bytes - low, high and extended -, is the most delicate aspect of the bootloader programming: a wrong fuse setting could brick the microcontroller and the board. Fuses define many aspects of the microcontroller's functions like: selecting different clock sources and change how fast the chip runs, setting the minimum voltage required before the chip works (brownout), setting whether or not a bootloader is used, setting how much memory is allocated to the boot loader (from 256 to 2048 words - 512 to 4096 bytes), disabling reset or serial programming and stop EEPROM data being erased when uploading a new sketch.
Detailed description of the fuses can be found on the datasheet of each microcontroller.
Every setting has its own usage and it is logical to allow the developer to lock the chip and protect it from ISP programming, but it might happen to mistakenly set a fuse in the wrong way, locking you out of the programming process through the ISP interface. To recover the microcontroller, you have to rely on a High Voltage Serial Programmer that uses 12V to reset the fuses.