# Attaching a LED to PIC's Rx pin

I need some way to know whether the microcontroller (PIC) is receiving (or transmitting) any data. So I thought of keeping a separate LED so that it'll blink when any data transfer occur.

But I need to know how to attach this LED. Is it OK to directly attach an LED to PICs Tx (and Rx) pins? I mean will it affect the transferring data in some way (like by dropping out the voltage etc...).

## 3 Answers

(At least some) PICs can't drive much current(*), but also for the RxD pin you better use a transistor to drive the LED, since you'll avoid loading the transmitter at the other end (probably a MAX3232 or similar?).

Connect the input "Q" to the TxD/RxD line. A typical general purpose transistor will have a gain of about 100, then 1 mA base current is enough to get 20 mA collector current.

For a 5 V bus and power supply:
choose $R_B$ = 3.9 kΩ, then the base current will be (5 V - 0.7 V)/ 3.9 kΩ = 1.1 mA. To limit the collector current to 20 mA (typical indicator LED) $R$ should be (5 V - 2 V)/ 20 mA = 150 Ω.

For a 3.3 V bus and power supply, use the same equations, replacing 5 V by 3.3 V, then your resistor values will be 2.2 kΩ and 47 Ω resp.

A MOSFET like AndrejaKo suggests is a good alternative, but make sure you have a logic level gate type, with a maximum gate threshold voltage somewhat below the bus voltage. (There are logic level gate FETs where that can be as high as 4 V and then you won't get enough drain current with a 3.3 V bus voltage.) The real advantage of the FET is that it needs hardly any drive current, but since we only need a mA for the BJT we won't have any problems with that either.

(*) This random PIC controller specifies a 700 mV drop at only 3 mA output current, that's a 230 Ω output resistance. A 2 V LED directly driven from a 3.3 V output will drop the output by 1 V at only 4 mA. Most indicator LEDs are specified for 20 mA.

• Good answer stevenvh, +1 for adding resistor size suggestions, many others just refers to Ohm's law – chwi Oct 2 '12 at 9:32
• Even though this may be the "proper" way of doing it.. if you can't drive <5mA or so from the MCU pin, I'd question the value of that MCU in real-world applications. RS232 tranceivers are also very sturdy. I have numerous designs where the series resistor and LED sit directly on the rx/tx lines. It works fine even if you don't use MAX, but cheap 2nd source tranceivers. As long as you don't attempt to drive 20mA or something crazy like that, there should be no problems. And if you pick a bright, modern LED you can easily use a 10k resistor. – Lundin Oct 2 '12 at 9:47
• @Lundin - Well, Microchip doesn't do bad at all in real-world applications, even when this datasheet specifies a 700 mV drop at only 3 mA and Vcc of 3.3 V. That's an internal resistance of more than 200 $\Omega$, not very good. – stevenvh Oct 2 '12 at 9:57
• @stevenvh Maybe I'm spoiled with Freescale, where you can drive some +-20mA through a single pin (at Vdd=3V) without going out of spec. Though of course, one must design with margins. As for the MAX tranceiver, the standard ones have a threshold volatage spec of high >2.0V, low <0.8V, so that one should never be a problem. – Lundin Oct 2 '12 at 11:05
• Btw the LED spec of 20mA is for the optimal brightness. If you put on a modern super bright one with some >300mcd, you can lower the current to below a 10th of what you would use with classic LEDs. We are then talking uA rather than mA currents. – Lundin Oct 2 '12 at 11:11

No, you don't want to connect the LED via a low side switch transistor as others have shown. In the normal case, the idle level of both lines is high, which would then result in the LED lit most of the time. It will be very difficult to notice it occasionally getting a little dimmer. What you want is the LED to be on only when the line is in the active state, which is low. Here is a simple circuit:

The transistor is used in emitter follower configuration, which eliminates the need for a base resistor and also uses the minimum possible base current for the resulting LED current. When the digital line goes low, the emitter will be at about 700 mV. Considering a normal green LED that drops about 2.1 V, that leaves 2.2 V accross R1. 2.2V / 120Ω = 18 mA, which is just under the maximum of 20 mA the typical T1-3/4 and many other common LEDs are rated for.

This is a case where you want to maximize the LED light output, meaning to run it at its maximum current. The line will go low for short periods, so you want to make that short time as visible as possible. If that doesn't work, you will need some kind of pulse stretching, but try this first.

If you are using a 3.3 V supply, adjust R1 accordingly. 3.3V - 2.1V - 700mV = 500mV accross R1. 500mV / 20mA = 25Ω. You want to leave some margin, so the standard value of 27 Ω should work fine. 3.3 V supply is about the minimum you want to use the emitter follower configuration in.

You shouldn't attempt to connect the diode directly to the pin because it will definitely affect the operation of the pin. Instead, try using a logic level mosfet to drive the LED. Connect the MOSFET's gate pin to the Rx pin and the drain to the LED and a resistor.

Ignore the part number shown on the schematic. BS170 would be much cheaper and work fine for this purpose.