# Driving LED with PIC 5V

I have always been taught to driver an LED using an MCP using the following method (Driving the pin high) as shown in the diagram below:

However I have recently seen LED's being driven using the following method as shown in the diagram below. I assume in this method the LED is driven when the digital pin is set to low. Which method is best practice and why?

• What does the datasheet say about drive strength? Commented Aug 17, 2015 at 8:00
• This depends on the specifications of the microcontroller, the configuration in the second image is more common Commented Aug 17, 2015 at 10:53

The source currents and sink currents can be different for a device, also the internal voltage drop for both cases can be different.

For the used PIC16F1847, you can see on the datasheet page 355 that the sink current is 8mA at 5V and the source current is only 3.5mA at 5V, these values also change in respect to the supply voltage, so you should be aware of that in your design too.

The next thing you might want to consider is, that the voltage drop can be different and thus the heat generated in the MCU. In your case the output high voltage is guaranteed to be more than $V_{DD} - 0.7V$ and the low voltage is guaranteed to be maximal $0.6V$. So for sourcing current the voltage drop might be higher and thus your MCU will get hotter - it might be a limiting factor or not, depends on the currents you are looking at. Some devices also have a total limit for sinking and sourcing currents which might differ also because the number of pins used to supply voltage and ground to the device can be different.

Microchip is actually providing a more detailed graph on the behaviour of the pin source and sink capability beyond those numbers mentioned before on page 400. There you can also see what influence the temperature has, as well as how much of a voltage drop you get at different currents.

For this device I'd say it copes much better with sinking currents than with sourcing them, so the second configuration might work better overall.

I don't think there is a best practice in general, but it has to be decided based on the particular device and design constraints you face.

• The physics dictates that the N-channel output MOSFET has better drive unless the P-channel is made significantly larger. Some micros have roughly symmetric drive, many (most?) sink better than source. I've never seen one that sourced better than sinked on a normal GPIO. Commented Aug 17, 2015 at 12:08
• In support of what Arsenal and Spehro wrote above, here's a similar answer (except it's about MSP430). Commented Aug 18, 2015 at 19:48

It is possible that the first circuit does not work depending on the internals of the MCU.

Usually, a pin can source or sink a current. In this case, it does not matter, which circuit you choose, only the logic will be inverted.

This sketch may give you an idea how this works inside the MCU:

simulate this circuit – Schematic created using CircuitLab

Depending on the state of the output, one of the LEDs will always be on.

But a pin could also be an open-collector output, which can be sketched like this:

simulate this circuit

An open-collector can only sink current, so the LED circuit works only in the direction shown in my sketch.

I'm familiar with Microchips PIC18F2550/2455/4550/44555 family, which have the RA4-"feature". While all outputs are CMOS outputs, RA4 is an open drain output and I guess everybody using this MCUs once forgot that.

However, an open collector/drain output also has the advantage of switching voltages higher than the MCU supply voltage. The mentioned MCU can switch up to 8V on RA4, even if itself is only powered by 3.3V.

For your PIC16F1847, check the tables on pages 13-16 of the datasheet. All I/Os are of type "CMOS", but you'll find pins which will be "OD" in I²C mode. But this is by general I²C design.

So, it does not matter which direction you choose for an LED at your MCU.

Sink or Source?

Which one should you use? In most cases it is a matter of personal preference. To turn on LED 1 you need to set the PO register bit to a zero. To turn on LED2, you set P1 to a logical 1. To many developers, setting a pin to a 1 or high seems more natural for turning something on, so you might want to use the microcontroller pin as a source.

One case you might need to use one driving method over another is when the maximum source and sink currents are not the same, and only one method will be able to handle the current needed.

If the pin you selected is an open drain type, you will need to sink the current. Open drain outputs do not have an internal transistor to Vdd, so the pin can’t supply current. Some microcontrollers have a few I/O pins that are open drain.

http://www.w9xt.com/page_microdesign_pt4_drive_led.html

Hope this helps