Go Down

### Topic: How button exampls circuit is wrong? (Read 1 time)previous topic - next topic

#### Venki

##### Dec 06, 2014, 05:50 am
Hi! somebody said's me in examples button program's circuit is wrong, there is not internal pull up resistance. So, How it is working properly? Is it working properly means Why every load need internal pull-up resistor. Is it working properly because it is only LED? Very Low current need? If operational current is higher means is it necessary to add pull up resistor, Else even though is there any voltage drop is there due to un presence of pull up resistor?

Thank you.

http://forum.arduino.cc/index.php?topic=280357.30

Code: [Select]
`/*  Button  Turns on and off a light emitting diode(LED) connected to digital   pin 13, when pressing a pushbutton attached to pin 2.    The circuit: * LED attached from pin 13 to ground  * pushbutton attached to pin 2 from +5V * 10K resistor attached to pin 2 from ground  * Note: on most Arduinos there is already an LED on the board attached to pin 13.   created 2005 by DojoDave <http://www.0j0.org> modified 30 Aug 2011 by Tom Igoe  This example code is in the public domain.  http://www.arduino.cc/en/Tutorial/Button */// constants won't change. They're used here to // set pin numbers:const int buttonPin = 2;     // the number of the pushbutton pinconst int ledPin =  13;      // the number of the LED pin// variables will change:int buttonState = 0;         // variable for reading the pushbutton statusvoid setup() {  // initialize the LED pin as an output:  pinMode(ledPin, OUTPUT);        // initialize the pushbutton pin as an input:  pinMode(buttonPin, INPUT);     }void loop(){  // read the state of the pushbutton value:  buttonState = digitalRead(buttonPin);  // check if the pushbutton is pressed.  // if it is, the buttonState is HIGH:  if (buttonState == HIGH) {         // turn LED on:        digitalWrite(ledPin, HIGH);    }   else {    // turn LED off:    digitalWrite(ledPin, LOW);   }}`

#### JimboZA

#1
##### Dec 06, 2014, 06:00 am
You can use either a pullup or a pulldown. Both work, and that's what Mike said in that thread you linked to, that the button tutorial is "wrong".

Clearly, it's not wrong in an absolute sense, since it works; but pullups are the preferred way of doing things.

Pullups can be handled with an external resistor, but the Arduino has them built in so it's obviously more convenient to use them than to have extra external ones.

You'll notice that the logic is reversed between pullups and pulldowns:
• With pullUP, the pin is normally high and goes low when you press the button; so-called "active low"
• With pullDOWN, the pin is normally low and goes high when you press the button; so-called "active high"
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

#### JimboZA

#2
##### Dec 06, 2014, 06:08 am
By the way, the reason for using either is effectively the same: it's to guarantee that the pin is in a known state when the button isn't pressed.

Let's say the button takes the pin to ground. You might therefore assume that the pin is high the rest of the time. But you don't know that for a fact. So one time when you read the pin without pressing the button, the pin might happen to be low anyway: you would then think the button was pressed and act accordingly. But it wasn't.... So the pullup guarantees that the pin is high when it's not deliberately low.

Similarly a pulldown guarantees it's low, if you prefer it that way.

But pullups and active low, are best practice.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

#### tkbyd

#3
##### Dec 07, 2014, 09:26 am
Previous answers have covered "Why you need EITHER a pull-up or a pull-down resistor". You can't get away without having one or the other.... somewhere.

They have not talked about the fact that the Arduino has some very clever "pull up resistors" BUILT IN to the Arduino, which, through software, you can "connect" to inputs.

So: You will sometimes see perfectly good circuits with pushbutton inputs which don't SEEM to have ANY pull-up or pull-down resistor. But the circuit is only "good" if the INTERNAL pull-ups have been "connected".

More on this at...

http://sheepdogguides.com/arduino/aht0button.htm

#### JimboZA

#4
##### Dec 07, 2014, 10:19 am
Previous answers have ... not talked about the fact that the Arduino has some very clever "pull up resistors" BUILT IN

Pullups can be handled with an external resistor, but the Arduino has them built in so it's obviously more convenient to use them than to have extra external ones.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

#### TomGeorge

#5
##### Dec 07, 2014, 11:19 am
Hi, I think Venki has the idea that the current flowing through  the button, is the current that is flowing through the LED.

Quote
Why every load need internal pull-up resistor. Is it working properly because it is only LED? Very Low current need?
Venki, the current switched by the button is to tell the program what to do.
The current output to the LED is supplied from the 5V supply through an output transistor and is controlled, ON or OFF, by the program.

Tom.....
Everything runs on smoke, let the smoke out, it stops running....

#### Grumpy_Mike

#6
##### Dec 07, 2014, 11:26 am
Quote
You can use either a pullup or a pulldown. Both work, and that's what Mike said in that thread you linked to, that the button tutorial is "wrong".

Clearly, it's not wrong in an absolute sense,
In the original thread Venki said he was using the button tutorial as a basis for attaching a magnetic sensor to an arduino. At that point I thought he meant a "magnetic sensor" because those are the words he used. It turns out he actually meant a reed switch, which while it responds to a magnet is not a magnetic sensor in the terms that everyone in electrons understands it.

Real magnetic sensors have mostly open collector outputs and in order to apply that to the button example, the button example is wrong as it has a pull down resistor and that can not be "extended" to be applied to a sensor.

Functionality is not a measure of correctness. Using a pull down resistor of 10K erodes the noise margin of the digital input, which is why no professional would ever use it. In this respect it is wrong, but it will function.

Go Up