Wiretuts Professional unprofessional tutorials.

How to use a push button on Arduino

Last update
Use a push button on Arduino

In this tutorial we use only one schematic and we make button work in 4 different ways with coding; hold, toggle switch, hold specific time and double click. You can use these examples in your projects and combine them if you need to.

Youtube video

Schema

  • Button
  • Red led
  • 220 ohm resistor
Arduino schematic

Hold button

Led is on as long you press button


const int buttonPin = 8; // Button pin 
const int ledPin = 7; // Led pin 

void setup() {
    pinMode(buttonPin, INPUT_PULLUP);
    pinMode(ledPin, OUTPUT);
}

void loop() {
    // If button is pressed.
    if (digitalRead(buttonPin) == LOW) {
        // Light up led
        digitalWrite(ledPin, HIGH);
    } else {
        // If button is up. Turn led off
        digitalWrite(ledPin, LOW);
    }
    delay(100);
}

Toggle button

You press button once and led lights up, you press it again and it goes off


const int buttonPin = 8; // Button pin
const int ledPin = 7; // Led pin
bool status = false; // Current led status

void setup() {
    pinMode(buttonPin, INPUT_PULLUP);
    pinMode(ledPin, OUTPUT);
}

void loop() {
    // If button is pressed
    if (digitalRead(buttonPin) == LOW) {
        // Change led state variable
        status = !status;
        // Change led state
        digitalWrite(ledPin, status);
    }
    while (digitalRead(buttonPin) == false);
    delay(100); // Small delay 
}

1sec hold

You hold button 1sec and leds light up, you hold button again 1sec and led goes off.


const int button = 8;
const int led = 7;
bool button_pressed = false;
bool wait_button_release = false;
bool led_on = false;
bool led_state_change = false;
unsigned long time;
int keypress_duration = 1000; // How long button has to be pressed to light up led. (In milliseconds. 1000ms = 1second)

void setup() {
    pinMode(button, INPUT_PULLUP);
    pinMode(led, OUTPUT);
}

void loop() {
    // If button is pressed
    if (digitalRead(button) == LOW) {
        if (!wait_button_release) {
            if (!button_pressed) {
                button_pressed = true;
                time = millis();
            } else {
                if (millis() - time & gt; = keypress_duration) {
                    // Do something
                    led_on = !led_on;
                    led_state_change = true;
                    wait_button_release = true;
                }
            }
        }
    } else {
        wait_button_release = false;
        button_pressed = false;
        time = 0;
    }
    if (led_state_change) {
        if (led_on) {
            digitalWrite(led, HIGH);
        } else {
            digitalWrite(led, LOW);
        }
        led_state_change = false;
    }
}

Double-click

You double click button and led goes on, and when you double click button again it goes off


const int buttonPin = 8;
const int ledPin = 7;
int buttonState; // The current reading from the input pin
int lastButtonState = HIGH; // The previous reading from the input pin
bool firstPress = false;
bool led_on = false;
bool led_state_change = false;
unsigned long last_press_time;
unsigned long doubleclick_delay = 600; // Two clicks has to be happen inside 600milliseconds, so we can detect is as doubleclick
unsigned long lastDebounceTime = 0; // The last time the output pin was toggled
unsigned long debounceDelay = 50; // The debounce time; increase if the output flickers

void setup() {
    pinMode(buttonPin, INPUT_PULLUP);
    pinMode(ledPin, OUTPUT);
}

void loop() {
    int buttonReading = digitalRead(buttonPin);
    if (buttonReading != lastButtonState) {
        lastDebounceTime = millis();
    }
    if ((millis() - lastDebounceTime) & gt; debounceDelay) {
        if (buttonReading != buttonState) {
            buttonState = buttonReading;
            if (buttonState == LOW) {
                if ((millis() - last_press_time & gt; = doubleclick_delay)) {
                    firstPress = false;
                }
                if (firstPress) {
                    if ((millis() - last_press_time & lt; = doubleclick_delay)) {
                        // Do something
                        led_on = !led_on;
                        led_state_change = true;
                    }
                    firstPress = false;
                } else {
                    last_press_time = millis();
                    firstPress = true;
                }
            }
        }
    }
    if (led_state_change) {
        if (led_on) {
            digitalWrite(ledPin, HIGH);
        } else {
            digitalWrite(ledPin, LOW);
        }
        led_state_change = false;
    }
    lastButtonState = buttonReading;
}

0 Comments

No comments yet :(. Be first to write one.