ATMega16U2 Arduino Mega

Hi all, I am working on a project based around the Arduino Mega2560. I have just finished designing and assembling the first custom PCB.

The PCB uses an ATMega16U2 and ATMega2560 just like the proper Arduino Mega does. All supported components (clock crystals, capacitors, etc) are the same values used on the Arduino Mega.

However, I am having an absolute nightmare trying to configure the ATMega16U2! When I connect the USB cable I get nothing at all in device manager, as if it's dead.

However I have managed to use my Arduino UNO as an ISP to load the bootloader onto the ATMega2560, and it appears to work fine. The "L" LED binks, and when you hold the reset button it stops blinking until released.

I just cannot for the life of me get the USB functionality with the ATMega16U2! I am able to communicate via ICSP and AVRDude. AVRDude sees the IC fine, communicates fine and can adjust fuses, etc over ICSP with no issues, but I am obviously doing something wrong.

The commands I am sending are as follows.....

First send: avrdude -c arduino -P COM1 -b 19200 -p m16u2 -t -e, this should remove any lock bits

Next send: avrdude -c arduino -P COM1 -b 19200 -p m16u2 -F -U efuse:w:0xF4:m -U hfuse:w0xD9:m -U lfuse:w:0xEF:m -U lock:w:0xCF:m, this should set the fuses, values taken from this thread here... What next? Mega2560r3 ATmega16U2 DFU Win8 USB - Installation & Troubleshooting - Arduino Forum

Finally send: avrdude.exe -c arduino -P COM1 -b 19200 -p m16u2 -F -U flash:w:Arduino-usbserial-atmega16u2-Mega2560-Rev3:i, this will program the firmware with the one which comes with Arduino IDE.

All steps work fine, no errors at all, but when I connect the USB cable I get nothing at all, PC just does not recognise it.

Any ideas?

UPDATE: I got it to work!! Turns out the IC was not properly soldered...oops!!

I removed it, cleaned up the pads, resoldered and applied some gentle pressure as it cooled down...boom it appears!

However I now have a new issue...

When I try to upload a basic "blink" sketch from Arduino IDE I can see the RX light on the board flash periodically, but no Tx, so the board is not responding, then I get the following errors...

"An error occurred while uploading the sketch
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer"

So I am making progress, albeit slowly. If anyone has experienced this new issue any further advice would be greatly appreciated!

So.... this is embarrasing.... the Tx pin was not soldered on correctly, so while the IC was receiving data it could not communicate back out. Going over the pins with the iron has sorted it, made sure to go over all other pins as well.

This was my first project using solder paste & stencils, so a bit of a learning curve. Might look into investing into a microscope as I had a short on that same IC yesterday that could not be seen, was only evident when I removed the IC and the short disappeared....

However, not totally out of the woods it seems!

While I can upload the example "Blink" sketch which just blinks the "L" LED on the board, when I go to upload any more substantial piece of code, it times out and fails every time. I also noticed it only allows you to upload once then you need to power cycle to upload again. Could this be a fuse setting issue on the ATMEGA16U2?

Current fuses are set as follows.... LFuse: 0xFF, Hfuse: 0xD9, Efuse: 0xF4 & Lock: 0x0F

Not it is not about fuses. Once you are able upload it should work for any program. According you previous post, I am suspecting your HW. Anyway, try to decrease the speed so you have to upload recompiled bootloader for slower UART. Maybe you could post the log.

I have to appreciate your effort and willingness for presentation. Thank you for that. Enjoy your first karma.

Thanks for the info, but I figured it out, just needed to ensure the jumper for the reset enable was connected so that avrdude was able to send the reset command and start the bootloader prior to programming.

Without it, it was only possible to program shortly after powering on and while the bootloader was running. I noticed I could upload larger code if I powered the board on part way through the compiling so it was still in the bootloader once it tried to upload.

Seems that once the IC is in full application mode (for lack of a better term) it's not possible to program the IC, so either a power cycle is needed or an ability for avrdude to perform a reset and get into the bootloader for programming (which the jumper provides provision for).

Full board is working exactly as expected now.

For reference the fuses on a stock Arduino Mega ATMEGA16U2 is LFuse: 0xEF, Hfuse: 0xD9, Efuse: 0xF4

Had a look and the only difference between LFuse being 0xFF vs 0xEF is that when set to 0xFF the bootloader runs for 61ms longer than when set to the stock 0xEF, so neither here nor there for my application. :slight_smile:

Hello again!!

Back with another ATMEGA16U2 issue....basically the IC appears to not be working again for no obvious reason. I uploaded some code from Arduino IDE, and while checking stuff on serial monitor and attempting to make changes to the code the Arduino IDE locked up and wouldn't respond. I pulled the USB cable (only partly for some reason) as that sometimes unfreezes the IDE environment (not this time).

Anyway, after I sorted the IDE crash I re-opened the software and reconnected the board....nothing! No "Windows detected hardware" sound! I have checked and I can still communicate and program the ATMEGA16U2 via the ICSP headers, everything works except for USB. I even uploaded some code that transmits data to the ATMEGA16U2 constantly, when this occurs the ATMEGA16U2 should light up the Tx LED which is connected to it, but nothing happened.

I have tested the code on an Arduino UNO and can see that when the 16U2 receives comms on pin 8, then the LED connected to pin 11 should light up.

My gut feeling is something internal to the ATMEGA16U2 has failed, but what is the concensus on here? Anything else I should try before replacing it (need to order one)?

You can try loopback test - self Rx to Tx communication (search for it, here is a guide). ATmega16U2 is just USB/serial transceiver. It just passing bytes from one side to another and back. Plus, reset signal must be.

Budvar10:
You can try loopback test - self Rx to Tx communication (search for it, here is a guide). ATmega16U2 is just USB/serial transceiver. It just passing bytes from one side to another and back. Plus, reset signal must be.

It's impossible to do the loopback test if the ATMEGA16U2 cannot be seen by Windows. For the loopback to work Windows need to assign a COM port so that I can send some data to somewhere and see if it loops back on itself.

As of right now, Windows is acting as if nothing is connected to the USB port, however the cable and port work perfectly fine with the UNO, so the fault is with the board, and if I cannot get a COM port assigned, then I can't do any external test.

What I have done is an "internal blink test". I uploaded a sketch via the ICSP to the ATMEGA2560 and have told it to pulse the Tx pin at 500ms on, 500ms off. With the UNO this causes the Tx LED to blink, with my board it does nothing at all.

Using the oscilloscope I can see the signal getting from the ATMEGA2560 to the correct pin on the ATMEGA16U2 (same one as on the UNO), however there is NO output from the ATMEGA16U2 to the LED, so something internal to the 16U2 is not right, even though I can communicate and program the 16U2 over the ICSP header fine, it is not fully functional.

Any other suggestions appreciated.