I'm not sure the subject line is quite right, but what I'm trying to do is light up an LED with 12v using a ground that also goes to a 2560 digital pin.
The application is a limit switch for a small CNC machine. When the switch is closed I would like the LED to light up as an indicator. The only power available at the switch is 12vdc, which has a common ground with the 2560.
I've consulted professor Google but can't find this exact application, so I've had a go at it myself. As this is my first attempt at designing a circuit I'd really appreciate some guidance.
The diagram below is what I've come up with. I'm using an internal pull-up resistor for the input.
Thanks in advance for any feedback you may wish to provide,
Think about what happens when your switch is off or open. What voltage is on that input pin D2? It will be 5V, which is enough to draw power through the LED and light it. This current goes through the protection diodes inside the Atmel chip, which can never be disabled or disconnected. They are there for your protection.
Use a dual-pole switch and have the second pole switch the LED or have a spare output from the Arduino switch the LED on 5V or use a transistor to switch the LED on 12V.
Use a 47k series resistor between the switch/ LED and the digital pin, and put a diode (1N914 or similar) "pointing" from the digital pin to VCC to bolster the internal protection diode. The current through the diode is limited to 250 µA which is acceptable.
Alternatively, the same arrangement with a 33k resistor from pin to ground. This forms a voltage divider with the 47k so that at 12V, you would have only 4.95 V on the pin which is a clear logic HIGH..
Thanks for the feedback MorganS and Paul__B, I'm learning stuff here.
If R1 in my diagram drops the voltage from 12 to 2.5 for the LD, does this mean there is only 2.5V going to D2 ? If yes, is that enough for a logic high or would using a higher voltage LD work ?
When the switch is open, you want the LED off, so current is zero. What is the voltage across the resistor? It's zero too. So the left-hand end of the LED is as 12V. What is the voltage across the LED? Well, at zero current, it's also zero. So the right-hand end of the LED and your digital pin is at 12V.
In practice, this won't happen because of the protection diode inside the chip. The pin will be at 5V or perhaps slightly above.
MAOMS:
If R1 in my diagram drops the voltage from 12 to 2.5 for the LD, does this mean there is only 2.5V going to D2 ? If yes, is that enough for a logic high or would using a higher voltage LD work ?
In your original circuit, or my correction?
If you were to use your original circuit which of course, you absolutely must not, then with the switch open the protection diode will hold D2 at 5.5 V, so you will have 6.5 V across the resistor and LED. If the drop across the LED is 2.5 V, then the resistor will have 4 V across it and pass 8.5 mA so the LED will actually light up nicely. But this is highly undesirable as the protective diode is only rated for about 1 mA.
If you use my corrected circuit, you will have the same situation but the total resistance will be 47k (ignoring the 470 Ohms) and 85 µA will pass through the LED - you might see a very slight glow in the dark if the LED is good quality.
Just to ensure I do understand, I've redone the diagram. I've included both the diode and the resistor options, not sure which one is the better ? A dim glow when the switch is open is acceptable for this application.
OK so I've tested the circuit and is seems to work as predicted. The led has a dull glow when the switch is open, and illuminates well when the switch is closed. This is using both the diode and the 33K resistor.
Thanks again for your help guys, greatly appreciated.
I had some "cheapies" some years back with very curious behaviour. They appeared to exhibit "leakage" which is to say, they conducted below the threshold voltage but did not light. If fed with sufficient current to exceed the threshold, they would light but that current was somewhat excessive - more than 20 mA. However this did seem to clear the leakage and after this treatment, they generally behaved quite normally.