Switch with an LED | Arduino Basics

Here is implementation of today’s discussion,

Used a switch to make an LED on and off with an external pull-up resistor.


int buttonPin = 3;
int led = 13;

void setup() {
  pinMode(buttonPin,INPUT);  
  pinMode(led,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int buttonState = digitalRead(buttonPin); 
  if (buttonState == LOW) { 
    digitalWrite(Led,HIGH); /
  } else {
    digitalWrite(Led,LOW);
  }
  Serial.println(buttonState);
 
}

With slight modification (adding a line), one can directly make use of the internal pull-up resistor on arduino digital pins and unplug the external resistor if needed,


int buttonPin = 3;
int led = 13;

void setup() {
	pinMode(buttonPin,INPUT);
	pinMode(led,OUTPUT);
	Serial.begin(9600);
	
	digitalWrite(buttonPin,HIGH); // internal pull-up

}

void loop() {
  int buttonState = digitalRead(buttonPin); 
  if (buttonState == LOW) { 
    digitalWrite(Led,HIGH); /
  } else {
    digitalWrite(Led,LOW);
  }
  Serial.println(buttonState);
 
}

How do one implement pull-down in the above example? What changes one needs to make in the external connection as well as in the code?

Is there a limit to the pull-up value of resistor one can use with push button? Why?

with @jtd @Ashish_Pardeshi @sandysat @AsmitaRe @Farhan @saida786110 @karnamdpdurga @nagarjunag

3 Likes

To use internal pull-up resistor, you have to use:

pinMode(buttonPin, INPUT_PULLUP);

In your code, you are just setting it HIGH using digitalWrite(). You are not activating internal pullup resistor.

2 Likes

Thanks! This is interesting. So is there any danger in using digitalWrite? The code seems make the system behave as expected

3 Likes

I see, so digitalWrite is just changing the state once, its actually not activating the internal pull-up.

1 Like

No danger per se, but it does not do what you think it’s doing. It’s just a switch attached to the pin without any resistor.

What you are unknowingly doing is akin to initializing a variable to some value.

Also, you are setting it HIGH in setup(), so, as soon as the flow enters loop(), digitalRead() would return HIGH. Thus, buttonState will be HIGH. So now the ‘else’ part sets the LED to LOW.

4 Likes
pinMode(led,OUTPUT);

[quote="ravi312, post:1, topic:4463"]
digitalWrite(Led,HIGH); /

} else {
digitalWrite(Led,LOW);

[/quote]

either change all instances of Led to led or vice versa.
Also a orphan /

1 Like

Yes. There is a limit. The lowest value will depend on the current carrying capacity of the switch. However before you hit that limit you are likely to hit the current capacity of the power supply.

The highest value will be open circuit ie no pullup. When there is no pull up the pin will be read as a high. However the input leakage current is specified as maximum 1uA at 5.5V. Therefore the minimum input impedance will be R=5.5/1^{-6} or 5.5Mohms. This is a very high impedance and is most likely to pick up random electrical interference from any nearby object - our hand, a mobile etc. Hence good engineering practice demands that a pullup or pulldown - if our switch ground point was wired to Vcc instead - resistor be used. For pull down the maximum value is determined by V_{iL} ie input low threshold. This is the input pin voltage value (at a specified supply - in this case 5V ) when the atmega328 will sense the pin as a low.
$R_pd=V_{iL}/I_{iL} R=2.2/1^{-6} or 2.2Mohms.

2 Likes