connecting push button with this code. high or low

Hi im confused on how a push button should be wired in. should is connect the pin to 5v or gnd. Ive read a few forum messages and have seen both so not sure what is the correct way. The code below is a snip from a friend for the button. Am i correct in reading that the button is low and a press takes it high. If so would it want connecting to send 5v to the pin? Like most things seems 101 ways to do anything

BUTTONSTATEMACHINE.h

#ifndef _BUTTONSTATEMACHINE_h
#define _BUTTONSTATEMACHINE_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif

class ButtonStateMachine
{
private:
    uint8_t pin_to_watch;

    bool read_state_jump();
    uint8_t current_pin_state = LOW;
    uint8_t last_button_state = LOW;
    unsigned long button_down_time = 0;
    unsigned long last_debounce_time = 0;
    unsigned long debounce_delay = 50;

public:
    ButtonStateMachine(uint8_t pin);
    bool has_state_changed();
};


#endif

BUTTONSTATEMACHINE.cpp

#include "ButtonStateMachine.h"
#include "DebugDefines.h"

bool ButtonStateMachine::has_state_changed()
{
    if (read_state_jump() == false)
    {
        return false;
    }

    DEBUG_COMMS_PRINTF("State change on pin %d\n", pin_to_watch );
    return true;
}

bool ButtonStateMachine::read_state_jump()
{
    const uint8_t unfiltered_pin_state = digitalRead(pin_to_watch);

    if (unfiltered_pin_state != last_button_state)
    {
        // reset the debouncing timer
        last_debounce_time = millis();
    }

    if ((millis() - last_debounce_time) > debounce_delay
        && unfiltered_pin_state != current_pin_state)
    {
        current_pin_state = unfiltered_pin_state;

        // We only want to return true if the state change was to high (button up)
        // This shows the press and release was performed.
        if (current_pin_state == HIGH)
        {
            return true;
        }
    }

    return false;
}

ButtonStateMachine::ButtonStateMachine(uint8_t pin)
{
    pin_to_watch = pin;
    pinMode(pin, INPUT);
}

Thanks

use pinMode(pin,INPUT_PULLUP);

that way it gets 5vdc from the internal pull up resistor and you can write

if (digitalRead(pin) == LOW)

so you would just attach one side of the switch to the pin and the other side to ground.

that could be one way to do it maybe.

you can do both, connect to 5V or to GND but you adjust the code accordingly.

if you connect : 5V – BTN – Arduino pin input → if(btn = HIGH) then btn is pressed.

if you connect : GND – BTN – Arduino pin input → if(btn = LOW) then btn is pressed.

when you connect to GND you should add a resistor in series as well to limit the current to safe levels.
or, you can use the internal pull-up resistors on the Arduino, you can activite it by setting pinMode(btnPin, INPUT_PULLUP); instead of just pinMode(btnPin, INPUT);

BabyGeezer:
you can do both, connect to 5V or to GND but you adjust the code accordingly.

if you connect : 5V – BTN – Arduino pin input → if(btn = HIGH) then btn is pressed.

if you connect : GND – BTN – Arduino pin input → if(btn = LOW) then btn is pressed.

In the top one you forgot the pull-down resistor. There’s not a built in one to use if you do the reverse logic like that.

BabyGeezer:
when you connect to GND you should add a resistor in series as well to limit the current to safe levels.

What? No, they’re inputs. They’re high impedance. There’s no dangerous currents here. It’s LEDs that need current limiting resistors.

BabyGeezer:
or, you can use the internal pull-up resistors on the Arduino, you can activite it by setting pinMode(btnPin, INPUT_PULLUP); instead of just pinMode(btnPin, INPUT);

Those aren’t for limiting current. Those resistors are for holding the pin in a given state while the button isn’t pressed.

Delta_G: Those aren't for limiting current. Those resistors are for holding the pin in a given state while the button isn't pressed.

Well you could hold the pin one side or the other with a plain wire, and that would be ok until you pressed the button. But when you activate the button you have a dead short 5V to Gnd and a typical 10k resistor there will indeed limit the current.

Delta_G: In the top one you forgot the pull-down resistor. There's not a built in one to use if you do the reverse logic like that.

ah yes, that's why i always prefer the INPUT_PULLUP way.

Delta_G: Those aren't for limiting current. Those resistors are for holding the pin in a given state while the button isn't pressed.

Thanks for the corrections that refine my understanding.

Thanks guys for the info. Will go have a play. So no pro or con to doing it either way apart from an inbuilt pullup resistor if going that way

Lee

ltodd2: So no pro or con to doing it either way apart from an inbuilt pullup resistor if going that way

I would rather run pin and ground wires between an Arduino and e.g. a front panel than pin and 5volt. If you short ground (or pin) to front panel ground, nothing bad will happen.

Just use INPUT_PULLUP in pinMode, and wire the button between pin and ground. if(btn = LOW)... from post#2 should ofcourse be if(btn == LOW)... or if(!btn)... Leo..

Wawa: if(btn = LOW)... from post#2 should ofcourse be if(btn == LOW)... or if(!btn)...

good catch - thanks.

if i use pseudo code i should just write if(btn is LOW)