I have an Arduino Leonardo coded to send keystrokes to my PC on a button press. In the code I've used Arduino's internal pullup resistors with INPUT_PULLUP set to the pin.
I also want to light an LED when the button is pressed at the same time using the voltage from the Arduino, so I thought if I placed one in series it would light. It does, but the Arduino now does not trigger the key.
I'm guessing that this is because the forward voltage from the LED is 1.5v(ish) less. I found I could compensate for the voltage drop by putting a 1.5v AA battery in series after the LED (or even before it) which does allow the button press to light the LED and also is read by the Arduino pin and sends a keystroke, but I'm aware this might be not the best thing to do as it seems a bit odd to add in a battery this way.
You are approaching this all wrong - it's not clear what you tried, but whatever it was, it was wrong. Put that battery away before you damage something.
So you have a switch that is normally not connected (normally open), and one end is on ground, the other end on a pin set input_pullup.
Connect negative side of the LED to the side of the switch connected to the Arduino pin.
Connect positive side of LED to a resistor (at least 220 ohm, I usually use 470 or 1k).
Connect other side of the resistor to the 5v pin of the Arduino.
Alternately, you can connect up the LED to a pin set output (like you'd normally do to blink an LED), and have your sketch read from the switch and turn the LED on and off based on that.
got the led to light when the button is pressed with 5v->Resistor->LED->A0 but couldn't then read the state... not sure what code I'd need to read it..?
Think I've also bricked my Leonardo somehow messing around as it no longer registers as a COM port on any machine I own. Doh.
The pins (all of them) on a 5V Arduino are 5V tolerant.
The pins (all of them) on a 3.3V Arduino are likely to get damaged by connecting them to 5V unless you have a sufficiently large current limiting resistor in place.
So in the meantime I unbricked my Leo by opening up the blink sketch in the Arduino IDE, pressing Upload, then pressing Reset on the board twice when the IDE read "Uploading". Amazingly it re-registered as a COM port and I'm back in action... Note to self try not to fry Arduinos with half-baked laziness.
OK, I understand what you'd said DrAzzy. So I'm lighting the LED from the 5v pin which meets the A0 pin and the switch makes and breaks their shared connection to ground. It works! Better than a AA battery for sure. Thanks for that!
Unfortunately it doesn't fix my problem which is I need to read the pin and light the LED from a single wire with a connection to ground (eg either 5v to ground or a pin to ground)... Is it possible for example to set an arduino pin as an OUTPUT, and send 5v that way to light the LED (when a button is pressed), which then connects back to ground? Is it possible to somehow know when that circuit is open and closed?
I don't understand why you must connect to GND for this.
What I think you want to do is light the LED as the button is pressed, and be able to read the button from the Arduino, am I correct? Why does it HAVE to be GND?
When a pin is set out OUTPUT it's not an INPUT of course.
What you can do is use the LED's current limiting resistor as the pull-up resistor for the pin.
Thanks wvmarle, but again that that requires 3 pins (5v, GND and A0) and I need to use just 2.
The device I'm working on only has two wires (it's a reed switch that connects A0 to GND) and I can't add more to send a third back to the Arduino...
So the problem with my current circuit is that using digitalRead the value doesn't ever move from HIGH to LOW (even though when I use an analogRead I can see that the value changes from around 1015 to 350 when the button is pressed). If I was only using analog pins I could work with this to register this in the code, but I'm using both analog and digital pins and I need the digital ones to register too.
So presumably the internal pullup is preventing the drop from HIGH to LOW when I'm using digitalRead.
If I ground the button before the LED the keypress is registered (AO -> GND), but the LED does not light (path of least resistance means nothing goes to the LED right..?).
So is it that the LED and resistor mean that the signal is never pulled LOW enough to register ?
phluxm:
Thanks wvmarle, but again that that requires 3 pins (5v, GND and A0) and I need to use just 2.
I'd say it requires one pin (one of your input pins).
It also needs only two wires to go to the LED and the button as of course the resistor can be connected directly to the Arduino.
If I ground the button before the LED the keypress is registered (AO -> GND), but the LED does not light (path of least resistance means nothing goes to the LED right..?).
So is it that the LED and resistor mean that the signal is never pulled LOW enough to register ?
Huh?
You have to set your pin to INPUT mode or you can't read it in the first place.
You will definitely be able to read the signal with analogRead(), just check on the actual values you read. You will see a good difference between the +5V (switch open - reading should be 1023) and the switch closed (reading will be 300-600 give or take depending on the LED used).
Indeed depending on the forward voltage of your LED it indeed may not go low enough to register LOW on a digital pin (red/green/yellow LEDs have Vf of 1.8-2V so should be fine, blue Vf is around 2.5V so most likely not, white LEDs have a Vf of around 3.5V so certainly not).
Diagram is correct, but why the 100k across the LED.
The pin is already (should be) pulled up internally with pinMode(4, INPUT_PULLUP);
No voltage across the red/grey wires, so no bleed resistor needed.
Leo..
As I understand OP has this requirement that there must be no more than two wires (I still don't get why, really) with which he has to light the LED when the button is pressed, and detect the button state at the same time. This still requires three wires to go away from the Arduino.
Yep wvmarle is right. We have a custom made lead (actually a 3 core stereo mic 1/4 inch jack lead) that supplies power and ground to 2 reed switches which is over 5m long.
I'm trying to add in an LED light to indicate when the reeds are on and off, but I'm struggling to light the LED basically although someone had said it was possible.
Here's a illustration. On the left is what I have now. On the right what I'm trying to solve (adding in an LED).
This will probably work much better with analog pins, as I said before the LED may prevent the voltage from going low enough to record a LOW signal. Resistance of your long long cable may make this even worse. So pin 8 you may have to move to an analog one, and then use analogRead() instead of digitalRead(). By using analogRead you should have no problem distinguishing between the states.
Another thing: 5 meters of wire may have significant resistance of its own. You may have to lower the pull-up resistor value to compensate for that; the value suggested gives 15 mA of current to an LED with 1.8V forward voltage. Amend this resistor value to get the current you want for your LEDs. Do not set your pins to OUTPUT, HIGH in this configuration as you will break stuff.
Also make sure your reed switches have no problem with this current.
phluxm:
Is it possible for example to set an arduino pin as an OUTPUT, and send 5v that way to light the LED (when a button is pressed), which then connects back to ground? Is it possible to somehow know when that circuit is open and closed?
Nice ideas Sounds like I may still hit problems using the digitalPins though..?
wvmarle is right - I can't add any more wires and to make it more tricky there are 8 of these doubled reeded things attached to the Leonardo so I'm having to use the digital pins as well as the analog...
I do have room to add something at the Arduino end, or at the reeds end though, so wondered if I couldn't somehow use the reeds to trigger the LEDs but power them another way..?
I suppose another option is to use the 5v method and read the analogPins on multiple Leonardos or Micros... that could work. That way I'd have enough analog pins to read and be able to power the LEDs from 5v out... might try this later today.