I just spent some time searching around for all this information on how to take blank Atmega16u2 chips from Digikey, Mouser, and the like and using them just like they are on the normal Arduino products. This information may be elsewhere, but I thought a nice consolidated source would help out.
There are two main parts to getting a blank Atmega16u2 working:
-Firstly, the fuses need to be set. For me, the stock fuses were dividing down the clock by 8 internally, and this was causing my serial terminal window on my computer to see random garbage instead of real data. It took me a while to figure out that the problem was the timing of the USB chip and not some other serial to usb conversion issue.
-Secondly, the firmware hex file needs to be flashed.
Warning: Changing fuses incorrectly can make it very difficult to "fix" your chip and make it work again. It is not without risk.
Required materials:
-avr programmer (I used usbtiny, TinyProgrammer by SparkFun)
-An atmega16u2 with all support circuitry (clock crystal, capacitors,....)
-USB jack that you can hook the chip up to
Procedures:
1. Getting fuses set correctly
The atmega16u2 is most likely locked by the factory, so we will need to remove the lock bit. Use the following command:
C:\ avrdude -p at90usb16u2 -c usbtiny -F -t
Explanation:
-at90usb16u2 is the chip we are programming. However, the device signatures will most likely not match, so the -F command tells avrdude to ignore them not matching
-usbtiny is the programmer I am using
-t allows us to enter the erase command in the next step
Next, you will see this prompt:
avrdude>
Enter "erase" to get the following:
avrdude> erase
Then enter "quit"
Now that the lock bit has been erased, we can set the fuses how we need them. For my setup, I have a 16MHz crystal clock hooked up, so that determines my clock settings.
See here for more settings: AVR® Fuse Calculator – The Engbedded Blog
Enter the following command:
c:\ avrdude -p at90usb16u2 -c usbtiny -F -U efuse:w:0xF4:m -U hfuse:w:D9:m -U lfuse:w:0xFF
Explanation:
-U issues the command, efuse specifies the fuse being written to, "w" tells that we are writing and not reading which would be "r", and F4 is the value being written to the efuse
This completes the fuse portion!
For more information on fuses see this page: AVR Tutorial - AVRDUDE
2. Loading the firmware
This can be done at least two ways. Firstly, you can use Flip which is a windows program made by atmel where you can write the hex file to the atmega16u2 over USB. Secondly, you can use the AVR programmer to again program using avrdude.
The easiest is using avrdude just like we did for the fuse steps because we are already setup to do it.
Navigate to the folder holding the hex files. For a stock windows installation, it should be here:
C:\Program Files (x86)\Arduino\hardware\arduino\firmwares\atmegaxxu2\arduino-usbserial
And its called:
Arduino-usbserial-atmega16u2-Uno-Rev3.hex
Use this command:
C:\avrdude -p at90usb16u2 -c usbtiny -F -U flash:w:Arduino-usbserial-atmega16u2-Uno-Rev3.hex
That is it!