[Solved] Led in series with push button?

Hi

I just wanted to know if adding an led in series with the push button will work or is it a bad idea?
by series I meant the led will light up when button is pressed so that I know there is continuity in the circuit since the button will be placed at least 3 to 4 meters away from the mcu(Attiny13a) and power supply. INPUT_PULLUP will be used instead of a regular resistor.

Power supply will be 3V and led is red 2.4V 20mA and needs to be turned on only momentarily with the button press. The only function of the led is to provide a visual indicator that the long wire is not broken in between.

Yes that will work. Remember to include a resistor in series with the LED. The input pull up resistor is only about 40K and so will not be bright enough.

So I will add a 100 ohm resistor in series with button and led and no input pullup. Will that do or is it too low? I don't want the led to shine at it's brightest.

Use the switch / LED arrangement shown here:-

Noobian:
Hi

I just wanted to know if adding an led in series with the push button will work or is it a bad idea?
by series I meant the led will light up when button is pressed so that I know there is continuity in the circuit since the button will be placed at least 3 to 4 meters away from the mcu(Attiny13a) and power supply. INPUT_PULLUP will be used instead of a regular resistor.

Power supply will be 3V and led is red 2.4V 20mA and needs to be turned on only momentarily with the button press. The only function of the led is to provide a visual indicator that the long wire is not broken in between.

LEDs when off present a very high impedance, so you should enable the internal pullup to pull the pin
properly HIGH and not let it float, as well as use an external pullup that passes enough current to light the LED
fully.

@Grumpy_Mike

I believe that setup requires 3 wires. 5V, GND and IO , but I have only 2 wires, which is why I resorted to using an led in series.

I have a 3-4 meter long 2 wire telephone cord coming from the mcu and my push button is like a remote controller switch. The purpose of the led is to confirm that there is continuity in the wire since the mcu is placed in another room where I cannot see it so cannot add led there.

I will add a diagram soon so that things will be more clear.

Edit :

No, the common point of the push button and LED needs to go to the analog input, the end of the resistor to
Vcc ( pin 8 ). You'll need 3 wires.

Sorry, everything else can be changed except the wire. It's just 2 wire. If the led is not possible in series then I'll simply have to scrap the led plan and just stick with the button alone. There's no way I can change the wire, it goes through a tricky conduit and it's very difficult to replace it.

Sure it can be done with 2 wires. For example add a large (10k) resistor in parallel with both the LED AND small resistor on picture in post #5. Activate input pullup and when you notice button is pressed (input reads LOW) make the pin OUTPUT and write it HIGH. From time to time (every 100ms for example) revert it back to input pullup for a short while (1ms?) to check if the pin is still pulled LOW by the button or if it has been released.

@

Smajdalf:
Sure it can be done with 2 wires. For example add a large (10k) resistor in parallel with both the LED AND small resistor on picture in post #5.

You mean like this?

Smajdalf:
Activate input pullup and when you notice button is pressed (input reads LOW) make the pin OUTPUT and write it HIGH. From time to time (every 100ms for example) revert it back to input pullup for a short while (1ms?) to check if the pin is still pulled LOW by the button or if it has been released.

Make the same input pin as output? Won't the button be open(unpressed) by the time the pin has become output and sends a HIGH?

Hmm, multiplexing like that could be the answer - a little more complex.

You could also sense the ambient light on the LED if you use an analog pin I guess(!) LED Sensing

Noobian:
You mean like this?
...
Make the same input pin as output? Won't the button be open(unpressed) by the time the pin has become output and sends a HIGH?

Yes, exactly this.
...
Humans are soooo sloooow compared to electronics. Even without interrupts the ATTiny should be able to react well under 1ms with proper code (=not using blocking code). So from user's point of view it turns on the light as soon as the button is pressed - noone can notice 1ms "lag". Hidden advantage: when LED turns on you know the buton press was noticed by ATTiny.

Well, I'll give it a try and keep you posted. I just hope it won't cause any problems. :slight_smile:

The problem with that circuit is it will not put a low enough voltage on the digital pin to register as a zero. If by chance ( due to component tolerances ) it does it will not be very reliable. However using an analogue input in place of a digital one would be better.

If I use analog input then what values or range should I be reading?

FYI
You can put a LED and switch on the same pin as shown here:
https://forum.arduino.cc/index.php?topic=445951.msg3336467#msg3336467

Sketch to control a LED and read a switch on the same pin.

/*
  Demonstration how to use a pin as an output to a LED
  and at the same time, as an input from a switch.
 
  Note:
  The LED(s) 'must' be connected to +5V (CA) through a series resistor.
  The switch is connected to GND through a 240 ohm resistor.
 
  Typical setup for SMD LEDs:
  +5V White  LED - 4.7K - I/O PIN - 240R - Switch GND
  +5V Red    LED - 2.2K - I/O PIN - 240R - Switch GND
  +5V Green  LED - 1.2K - I/O PIN - 240R - Switch GND
  +5V Blue   LED - 4.7K - I/O PIN - 240R - Switch GND
  +5V Yellow LED - 2.2K - I/O PIN - 240R - Switch GND
 
  Version YY/MM/DD
  1.00    17/06/01   Running code
*/
 
//Assume 6 LED/Switch combinations are connected to pins as below
const byte HeartBeatLED  = 41; //Heart beat LED, toggles on and off showing if there is blocking code
const byte YellowLED     = 40; //This LED will be toggled when the switch on this pin is pressed
const byte BlueLED       = 37; //This LED will be toggled when the switch on this pin is pressed
const byte GreenLED      = 36; //This LED will be toggled when the switch on this pin is pressed
const byte RedLED        = 35; //This LED will be toggled when the switch on this pin is pressed
const byte WhiteLED      = 34; //This LED will be toggled when the switch on this pin is pressed
 
byte lastYellowSwitchState; //The state this switch was in
byte lastBlueSwitchState;
byte lastGreenSwitchState;
byte lastRedSwitchState;
byte lastWhiteSwitchState;
 
//======================================================================
//An example of Timer 'structure' coding
struct timer
{
  //lastMillis = the time this "timer" was (re)started
  //waitMillis = delay time (mS) we are looking for
  //restart    = do we start "this timer" again and again
  //enableFlag = is "this timer" enabled/allowed to be accessed
  //timeType   = true = millis(), false = micros()
  //**********************
  //For each timer object you need:
  //Example:
  //   timer myTimer = //give the timer a name "myTimer"
  //   {
  //     0, 200UL, true, true, true  //lastMillis, waitMillis, restart, enableFlag, timeType
  //   };
  // You have access to:
  // myTimer.lastMillis, myTimer.waitMillis, myTimer.restart, myTimer.enableFlag, myTimer.timeType, myTimer.CheckTime()
  //**********************
 
  unsigned long lastMillis;
  unsigned long waitMillis;
  bool          restart;
  bool          enableFlag;
  bool          timeType;
 
  unsigned long currentTime;
 
  bool CheckTime() //Delay time expired function "CheckTime()"
  {
    if (timeType == true)
    {
      currentTime = millis();
    }
    else
    {
      currentTime = micros();
    }
 
    //is the time up for this task?
    if (enableFlag == true && currentTime - lastMillis >= waitMillis)
    {
      //should this start again?
      if (restart)
      {
        //get ready for the next iteration
        lastMillis = currentTime;
      }
      //time was reached
      return true;
    }
    //time was not reached
    return false;
 
  } //END of CheckTime()
 
}; //END of structure timer
//======================================================================
 
//**********************************************************************
//                Create and initialize timer objects
//**********************************************************************
timer heartBeatLED =          //create timer for the heartBeatLED
{
  0, 100UL, true, true, true            //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};
 
timer checkSwitches =         //create a timer to check the switches
{
  0, 50UL, true, true, true   //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};
 
 
 
//                            s e t u p ( )
//**********************************************************************
void setup()
{
  //**************************************
  digitalWrite(HeartBeatLED, HIGH);  //LED  LOW = ON
  pinMode(HeartBeatLED, OUTPUT);
 
  digitalWrite(YellowLED, HIGH); //LED  LOW = ON
  pinMode(YellowLED, OUTPUT);
 
  digitalWrite(BlueLED, HIGH);   //LED  LOW = ON
  pinMode(BlueLED, OUTPUT);
 
  digitalWrite(GreenLED, HIGH);  //LED  LOW = ON
  pinMode(GreenLED, OUTPUT);
 
  digitalWrite(RedLED, HIGH);    //LED  LOW = ON
  pinMode(RedLED, OUTPUT);
 
  digitalWrite(WhiteLED, HIGH);  //LED  LOW = ON
  pinMode(WhiteLED, OUTPUT);
 
} //                    E N D  O F  s e t u p ( )
 
 
//                             l o o p ( )
//**********************************************************************
void loop()
{
  //***************************
  //HeartBeat LED
  if (heartBeatLED.CheckTime())
  {
    //Toggle HeartBeatLED
    digitalWrite(HeartBeatLED, !digitalRead(HeartBeatLED));
  }
 
  //***************************
  // Non blocking code goes here
  //***************************
 
  //Is it time to check the Switch(s)?
  if (checkSwitches.CheckTime())
  {
    handleSwitchPresses();
  }
 
} //                     E N D  O F  l o o p ( )
 
 
//======================================================================
//                         F U N C T I O N S
//======================================================================
 
//**********************************************************************
//               h a n d l e S w i t c h P r e s s e s ( )
//**********************************************************************
void handleSwitchPresses()
{
  //Read our five switches
 
  byte CurrentSwitchState;
 
  //***************************
  //Has the switch changed position for this pin?
  CurrentSwitchState = ReadSwitchLED(YellowLED);
  if (lastYellowSwitchState != CurrentSwitchState)
  {
    //update to the new switch state
    lastYellowSwitchState = CurrentSwitchState;
   
    //when the switch goes from not pushed (HIGH) to pushed (LOW) then do something
    if (CurrentSwitchState == LOW)
    {
      //example: toggle the LED on this pin
      digitalWrite(YellowLED, !digitalRead(YellowLED));
    }
  }
 
  //***************************
  CurrentSwitchState = ReadSwitchLED(BlueLED);
  if (lastBlueSwitchState != CurrentSwitchState)
  {
    lastBlueSwitchState = CurrentSwitchState;
    if (CurrentSwitchState == LOW)
    {
      digitalWrite(BlueLED, !digitalRead(BlueLED));
    }
  }
 
  //***************************
  CurrentSwitchState = ReadSwitchLED(GreenLED);
  if (lastGreenSwitchState != CurrentSwitchState)
  {
    lastGreenSwitchState = CurrentSwitchState;
    if (CurrentSwitchState == LOW)
    {
      digitalWrite(GreenLED, !digitalRead(GreenLED));
    }
  }
 
  //***************************
  CurrentSwitchState = ReadSwitchLED(RedLED);
  if (lastRedSwitchState != CurrentSwitchState)
  {
    lastRedSwitchState = CurrentSwitchState;
    if (CurrentSwitchState == LOW)
    {
      digitalWrite(RedLED, !digitalRead(RedLED));
    }
  }
 
  //***************************
  CurrentSwitchState = ReadSwitchLED(WhiteLED);
  if (lastWhiteSwitchState != CurrentSwitchState)
  {
    lastWhiteSwitchState = CurrentSwitchState;
    if (CurrentSwitchState == LOW)
    {
      digitalWrite(WhiteLED, !digitalRead(WhiteLED));
    }
  }
 
} //      E N D   O F   h a n d l e S w i t c h P r e s s e s ( )
 
 
//**********************************************************************
//                    R e a d S w i t c h L E D ( )
//**********************************************************************
byte ReadSwitchLED(byte thisPin)
{
  //Save the current state of this output pin
  byte PinState = digitalRead(thisPin);
  //read the switch connected to this pin
  pinMode(thisPin, INPUT_PULLUP);
  byte SwitchState = digitalRead(thisPin);
  //return the pin to OUTPUT
  pinMode(thisPin, OUTPUT);
  //restore the pin state
  digitalWrite(thisPin, PinState);
 
  return SwitchState;
 
} //            E N D   O F  R e a d S w i t c h L E D ( )
 
 
//======================================================================
//                         E N D  O F  C O D E
//======================================================================

Grumpy_Mike:
The problem with that circuit is it will not put a low enough voltage on the digital pin to register as a zero. If by chance ( due to component tolerances ) it does it will not be very reliable. However using an analogue input in place of a digital one would be better.

Yes, that is why I suggested adding the resistor as shown in post #9. Considering worst case conditions 10k pull down may be too weak. But using 3k3 (or even stronger) should work for any parts variations.
Using analog read is an option but not so good IMHO - you need "constantly" read the pin consuming power and processor time preventing the ATTiny to go to sleep. Also interrupts cannot be used to respond to key press.

Who mentioned sleep? You constantly have to read a digital pin as well. If you use interrupts then you are pushing this project in a direction of your own making. Don't get carried away.

larryd:
You can put a LED and switch on the same pin as shown here:

Sketch to control a LED and read a switch on the same pin.

Thanks for the image and link but that setup requires 3 wires, I have only two. My requirement is not to do it with just 1 pin, but to do it with just two wires which the button is using. If I had more than 2 wires then I could have just wired them in parallel.

Smajdalf:
Using analog read is an option but not so good IMHO - you need "constantly" read the pin consuming power and processor time preventing the ATTiny to go to sleep. Also interrupts cannot be used to respond to key press.

My MCU won't go to sleep and also I won't be using interrupts. If there are any other disadvantages to analog read then do let me know.

Edit:

@Grumpy_Mike - I have modified my project to use analog input like you suggested and I guess the problem is solved now. I just used the 'voltage read' function and it works like I wanted, so no more monitoring button state. I'm uploading the new diagram. Do let me know if there are any flaws or mistakes.

From your schematic I guessed the application is wait for button press -> send IR command -> do nothing until next button press. Also you show batteries so I guessed you are running from batteries. If it is true power consumption of the MCU is probably much much more than rest of the application. When you don't use sleep MCU will consume like 99% of batteries. On the other hand using sleep will reduce average consumption to single uAs - less than self discharge of "normal" batteries.

If my assumptions are correct you will need to replace batteries every few months when using AAs and no sleep. With sleep you can run from 2 AAs "forever" - limited more by self discharge. Even if you don't plan to do this now you may want to implement sleep in the future when you are more experienced/bored replacing batteries. If so analog method will limit you - you will have to change hardware too which may be inconvenient. But for now if you are not interested in current consumption the analog method is probably easier to implement.

Another thoughts about your curcuit: you have no decoupling cap on ATTiny's power lines. Also you are driving the IR LED directly by a pin. Don't expect current anywhere close to 40mA when powered from 3V. I expect something around 10mA in setup shown. If you need more, use a transistor.