Shop OBEX P1 Docs P2 Docs Learn Events
Question about basics of pushbutton — Parallax Forums

Question about basics of pushbutton

Hello. I'm new the electronics and this forum, but having fun learning about the Basic Stamp.

I'm at the pushbutton chapter and can follow along pretty well, but something about its basic setup has got me confused. All the pushbutton schematics show that it must have a connection to a Vss pin by default, so that current flows even when the button is not pushed. Coming from the programming background, I found this odd, as I would expect the IN(IN3 in this case) setting to be 0 by default without needing a connection to Vss. However, with further testing, it seems all the IN settings to be 1 in their default state when not connected to Vss. I suppose this explains why Vss connections are needed, but I'm still wondering why this is the case. Also, if Vss is needed, can a simple jumper wire suffice? Why does it need to be a 10k resistor?

Thank you in advance.

Comments

  • Hello Mason_L,

    In those cases the 10K is acting as Pull-Down resistor since it is pulling the I/O pin down to a Low level or 0.
    This goes back to the days of TTL and CMOS, and ensures that the logic levels are known.
    An unconnected I/O pin will "float" and any stray voltage, such as from a static charge, will easily change it's logic level.

    10K is a very large resistance, so a pressed pushbutton which has a small resistance by comparison, will easily change the I/O pin to a 1 or High level.
    If you use Ohm's Law, you will see that a 10K resistor allows only a tiny current to flow through it.
    i = V / R = 5V / 10,000 Ohms = 0.0005 A or 0.5 mA
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    Which book are you referring to?

    Active-Low switches are pulled to Vdd via a 10K or other weak resistor and when the switch is closed the signal is driven to ground. With an active-low switch the default open state will be 1 while the signal is pulled up via the resistor. When the switch closes and the signal pin is now connected to ground the input will read a 0. This is the more common method of connecting buttons and switches and some of the reasons why it is done in this manner are legacy reasons but there are other reasons, such as having multiple buttons on the same wire. In this "open-collector" type of system and switch can be closed and drive the line to ground without causing any contention on the "bus".

    You could always create an active-high switch where the closing of the switch connects to Vdd and the signal line is pulled low via a resistor to Vss. This would result in a 0 when the switch was open and a 1 when the switch was closed. This can often be more intuitive for beginning programmers. In either case the resistor is require to keep the signal input from floating and potentially randomly switching between states.
  • GenetixGenetix Posts: 1,754
    edited 2016-08-15 16:58
    Chris,

    I assumed, being a newbie, that What's a Microcontroller? (WAM) is being referenced.
  • In cmos & ttl "nothing" is a unknown state, so nothing is not a zero

    So a buttons state connected to a mcu input pin needs to have both a pressed and a not-pressed state, that you set with a resistor.
    A button active-low, e.g a mcu pins reads a zero when it's pressed as you're are connecting it to Vss (ground) when pressed, may look reversed but you get used to it.
  • Thank you, all. This is very helpful. I didn't know about random floating of input signals, and it now makes sense. Looks like I should learn more about pull-down resistors.

    And yes, I'm working off the "What's a Microcontroller?" book. I chose Parallax kit for its good explanations and helpful community, and am happy to learn that it's true.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    tonyp12 wrote: »
    A button active-low, e.g a mcu pins reads a zero when it's pressed as you're are connecting it to Vss (ground) when pressed, may look reversed but you get used to it.

    You don't even have to get used to it if you don't want. On many microcontrollers, such as the BASIC Stamp 2, you can easily change the way the pin reads by inverting the read.

    Here's an example...say the switch is active-low and you really want your code to use a 1 for switch closure and 0 for open even though the switch is wired for active low, you can just use this in our test.
    IF ~IN0 = 1 THEN DEBUG "Switch Pressed", CR
    

    The tilde (~) in front of the IN0 command (or any valid variable or constant) will invert the value. If this seems more confusing, just disregard. But it can change the way your treat inverted logic within your code at the time you read the input.

  • Mason_L,

    Welcome to the Parallax community and please feel free to ask questions since we were all novices at one point.

    I don't know if you already know this you can download a PDF of the text as well as some of the code and a few other items.
    The code can also be copied and pasted from the PDF but beware that the spacing gets obliterated.
    https://www.parallax.com/product/28123
Sign In or Register to comment.