# ATmega32U4 with TXLED and RXLED as output pins and USB functionality

I use ATmel ATmega32U4 in a schematic which copies Arduino Leonardo almost completely. So, Caterina-Leonardo bootloader works fine with it.

Unfortunately, I have run out of all available output pins. So, I wonder whether I can use pin 22 (TXLED, PD5) as additional digital output.

As I understand from many forums and Q&As out there, I have to build custom bootloader. I’ve added my own board to Arduino IDE and built new bootloader from Caterina code (so, the compiler and LUFO libraries work well), but I don’t have a clue how to properly disable TXLED and RXLED signals, in order to use these pins for output correctly (RXLED as the 17-th Leonardo digital pin and TXLED as the 30-th one).

On the other hand, the board uses USB (ATmega32U4 pins of 3 and 4) for communication with the computer (it needs to be a plug-and-play device), sending some data to it, and for uploading new sketch’s versions through Arduino IDE. So I need to use USB functionality and TXLED as an output pin simultaneously. (The output pin doesn’t lead to the computer, just in case).

Can you please tell me, is this issue solvable, and, if yes, how can I get things done? What should I change in the bootloader sources (like caterina.h, caterina.c, descriptors.c etc.)?

Any useful information on that will be highly appreciated.

I spend hours digging bootloader code and different websites but eventually failed. For instance:

Seems like the last guy had the same problem and successfully solved it, but unfortunately he didn’t describe his solution and didn’t attach any source.

Here is my circuit diagram:
I enabled internal pull-ups in ROW1..ROW4 and want to make them LOW when switch is pressed. It works well with all STRs except the 3rd one. The problem is that STR3 is connected to pin 22 (TXLED).

• Ye, it's solvable. So N people do not have to dig it up, please provide cct diagram of system - relevant parts at least. | As you are making uyour own I assume a SMALL extra amount of hardware is OK. Yes? Jan 18 '17 at 9:38
• The thing is I'm trying to fix software for already designed schematic where keyboard matrix is used, and TXLED pin is used for output row from where signal goes through a diode and a switch to some input (column) pin. Jan 19 '17 at 6:55
• In that case you really must give us a circuit diagram. Jan 19 '17 at 14:50
• I have added the diagram. Thanks for your patience. Jan 20 '17 at 20:26
• I'm voting to close this question as off-topic because it has been abandoned for a year and a half during which the asker has ignored pointed requests to explain how the provided responses fail to meet their needs. If the asker refuses to clarify, closure is the only means of putting this to rest. Jun 19 '18 at 1:48

In the Arduino IDE 1.8.1 is the Catarina bootloader at /hardware/arduino/avr/bootloaders/caterina

Open the files Catarina.c and Catarina.h and search for TX_LED and RX_LED, you will see that they are used a lot. When you remove them, you could adapt the LED_SETUP() macro as well, since that macro sets three pins for output (RX, TX, L).

Did you connect AREF to VCC ? That is not okay. When the internal voltage reference is selected it will cause an internal shortcut.

• Thanks for the reply. I cannot remember, maybe I already nulled these macros, like TX_LED() {} and so on. Will try your solution, and write here then. As for AREF, I use analogReference(DEFAULT) in the code, but I’m an ordinary web programmer and am too far away of being an expert of electronics. How should I connect AREF pin in your opinion? Jan 21 '17 at 10:11
• Connect the AREF pin to 100nF capacitor and that capacitor to GND. Nothing else. It reduces the noise for the voltage reference inside the chip. If you don't use analogRead(), you can omit the capacitor and just leave it open. Clearing those macros should work as well of course. Did we miss something ? As far as I know, only the macros are used to set those pins. Do you use LED_BUILTIN_RX and LED_BUILTIN_TX in your sketch ? You can use them in a normal way with digitalWrite and pinMode with a newer Arduino IDE.
– Jot
Jan 21 '17 at 20:20
• Sorry, do you mean I should connect it like this? sishot.ru/i/blJYiduSABY5 Feb 1 '17 at 14:54

You did not show the whole diagram that is relevant.
How is the TX LED driven?

If TX LED is driven by the pin with a series resistor:

LED circuit can be

• pin - resistor - LED - ground or

• pin - LED - resistor - ground

either works equally well in the modification below.

Add any "Jellybean" NPN transistor (BC337 or ....) as Q1

• Disconnect connection from pin to TX LED circuit
• Connect pin to Q1 base.
• Connect Q1 collector to Vcc.
• Connect Q1 emitter to LED circuit.
• Drive switch matrix from pin as before.

The transistor acts as and emitter follower and as a buffer for the pin.
Te LED drive will be slightly reduced as now it is being drivem by Vpin-Vbe_Q1 instead of by the pin directly. The resistor size could be somewhat reduced if more brightness is required.

• This is a whole diagram. There are no LEDs on those pins at all. I wanted to use those pins for my switch matrix (as shown on the diagram). TX pin here is 22 (PD5), and RX pin here is 8 (PB0). Apr 26 '18 at 11:49
• @SashaMaximAL t's now 15 months since I answered. I need a better explanation of what you want than a few extra words. I could spend much time going over the old material trying to work out what you want and have it wasted by misunderstanding you. YOU need to do the work to bring thngs up to date so we can better help you. Apr 27 '18 at 12:55