How do I detect if a LED is connected to digital pin?

Hi,

I’m developing an Arduino Mini Pro sketch that will take a number presented through a BCD switch and display that number on EITHER a 7-segment display OR a series of 6 discrete LEDs. The design is such that the 7-segment and the 6 LEDs will be connected to the same digital pins of the arduino – but NOT at the same time. That is to say, only ONE of the display types can be connected at a time and I’d like the sketch to automatically detect and configure the run-state of the software according to the type of display attached. If no display is connected, I’d like the sketch to disable the display code at run time.

The 7-segment display will be connected like this:

I am connecting the display’s common cathode to digital pin 6 of the arduino to allow for PWM intensity control. In a similar manner, the 6 discrete LED display will share 6 of the 8 pins, as well as the common cathode return.

I have determined a method to detect whether one or the other displays are connected. I’ll measure the voltage present at the junction of the current limiting resistor R1 and Segment-A on the 7-segment display. This digital pin is not in common between the displays. If I measure 5V, then the 7-segment display is not connected. If I measure 1.6-2.4 volts, then the display is connected. I can do the same for resistor R2 and Segment-B. If I measure voltage drop on this junction, the 6 discrete LED display is attached.

I could assume that no voltage drop on either junction means no display is attached, but I’d like some way to detect current returning through digital pin 6. This would give the sketch a means to determine that no display is connected. Since there is no current limiting resistor, I can’t use the voltage drop method. Are there other ways to measure current on a digital pin?

Hi Robert,

I'll measure the voltage present at the junction of the current limiting resistor R1 and Segment-A on the 7-segment display.

How? With an analog pin? If so, you could simply build in a different value resistor into each display to form a voltage divider with a fixed resistor. The value read by the analog pin would then tell you which display is connected, or neither. No need to measure currents on digital pins.

Paul

In that configuration, you could try setting the internal pullup on and setting the pin to an input. If there is no LED you will see high. Otherwise you should see low give that the pullup is very weak and the current through the LED will be small and so the voltage drop should be.

robmack: I could assume that no voltage drop on either junction means no display is attached, but I'd like some way to detect current returning through digital pin 6. This would give the sketch a means to determine that no display is connected. Since there is no current limiting resistor, I can't use the voltage drop method. Are there other ways to measure current on a digital pin?

That is very easy!

On the module that has no LED connected to the monitoring pin, put a dummy resistor instead.

Now an implicit aspect to this is that you want to minimise the number of connector pins (to match a DE-9 connector). OK, you put R1 on the Arduino side of the connector. In fact, you might put all the resistors on that side. No display connected, you measure 5V (approximately) when you poll this pin; with the 7-segment display connected you measure less than 2V (for a red LED) and on your dummy module with its 1k "dummy" resistor, you measure about 4.4V.

Now having come to this conclusion, you can distinguish quite a few different displays - using different resistors. You can even have an extra resistor on a LED being sensed, or determine what colour display is connected.

Wow, great ideas!! I will breadboard the circuit and test the ideas. I am particular to Paul’s idea, since it is expandable for different future display types.

The input is very appreciated. Happy New Year.