Starting Electronics Needs Your Help!
It is that time of the year when we need to pay for web hosting and buy new components and equipment for new tutorials. You can help by making a donation. Contribute to this website by clicking the Donate button. The total will be updated once daily. (You may need to clear your browser cache to see the updates.)

Target Amount: $2000
Amount Raised: $895

Donations Received

Top Donor: C.C. $100

X

Push Button Module Arduino Tutorial

Created on: 17 January 2018

How to use the push button switch module from the 37 in 1 sensor kit for Arduino from Geekcreit, Elegoo, Elektor and others.

This tutorial shows the basic use and testing of the three pin push button module with Arduino. An Arduino sketch shows how to read the push button module to determine if its switch contacts are open or closed.

Note that there are two different pinouts for the push button module, depending on which kit they are from. See the push button module pinout for more details.

The image below shows the push button switch module used in this tutorial.

Geekcreit Push Button Module

Geekcreit Push Button Module

Push Button Module Circuit

Important:

Before continuing, refer to the push button module pinout to determine the configuration of your push button sensor module.

There are two different push button modules and two different ways that each can be configured.

The push button module can be wired to an Arduino using the 10k resistor on the module as either a pull-down or pull-up resistor. A different Arduino sketch or program is required for each different wiring configuration.

Geekcreit Push Button Module Circuit

Below are two circuits that show how to wire the Geekcreit push button module to Arduino. It is recommended to check your module with a multimeter to make sure which pins the 10k resistor (R1) on the module is connected to.

Circuit Connections to Arduino:

  • +5V connects to the Arduino 5V pin.
  • GND connects to the Arduino GND pin.
  • SENSE connects to an Arduino digital input pin. Arduino pin 2 is used in the sketches below, but can be changed.

The circuit on the left is used with the pull-down resistor Arduino sketch below. The circuit on the right is used with the pull-up resistor Arduino sketch below.

Geekcreit Push Button Module Arduino Circuit

Geekcreit Push Button Module Arduino Circuit

Alternate Push Button Module Circuit

If your push button module is configured with the 10k resistor connected between pins 1 and 2 of the module, then choose one of the following circuits. This configuration is used on Keyes modules, but it is still recommended to check your module with a multimeter to make sure that this is indeed the correct configuration.

Circuit Connections to Arduino:

  • +5V connects to the Arduino 5V pin.
  • GND connects to the Arduino GND pin.
  • SENSE connects to an Arduino digital input pin. Arduino pin 2 is used in the sketches below, but can be changed.
Alternate Push Button Module Arduino Circuit

Alternate Push Button Module Arduino Circuit

Push Button Module Arduino Sketch

Choose one of the following sketches, depending on if you wired the push button module to use the resistor as a pull-down or pull-up.

Both sketches monitor the pin that the push button module is connected to. After the push button switched is closed and then opened, the Arduino on-board LED is switched on for two seconds. Note that nothing happens while the switch is closed. The LED only switches on when the push button is released after closing it.

Pull-down Resistor Push Button Module Arduino Sketch

Use this sketch with the pull-down resistor circuit. SENSE in the circuit diagram connects to BUTTON_PIN defined in the sketch (defaulted to pin 2 in the sketch).

// Sketch for push button sensor in pull-down resistor configuration
// Pinout:   https://startingelectronics.org/pinout/push-button/
// Tutorial: https://startingelectronics.org/tutorials/arduino/modules/push-button/
// Change pin number that the push button module is connected to here
#define BUTTON_PIN 2

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);       // on-board LED, usually pin 13
  pinMode(BUTTON_PIN, INPUT);         // push button pin set to input
}

void loop() {
  if (digitalRead(BUTTON_PIN)) {       // switch closed?
    // switch closed with pull-down resistor
    delay(40);                        // switch debounce delay
    while (digitalRead(BUTTON_PIN));  // wait for switch to open
    digitalWrite(LED_BUILTIN, HIGH);  // switch LED on
    delay(2000);                      // leave LED on for period
  }
  else {
    // switch open with pull-down resistor
    digitalWrite(LED_BUILTIN, LOW);   // switch LED off
  }
}

Pull-up Resistor Push Button Module Arduino Sketch

Use the sketch below with the pull-up resistor circuit. SENSE in the circuit diagram connects to BUTTON_PIN defined in the sketch (defaulted to pin 2 in the sketch).

// Sketch for push button sensor in pull-up resistor configuration
// Pinout:   https://startingelectronics.org/pinout/push-button/
// Tutorial: https://startingelectronics.org/tutorials/arduino/modules/push-button/
// Change pin number that the push button module is connected to here
#define BUTTON_PIN 2

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);       // on-board LED, usually pin 13
  pinMode(BUTTON_PIN, INPUT);         // push button pin set to input
}

void loop() {
  if (!digitalRead(BUTTON_PIN)) {       // switch closed?
    // switch closed with pull-up resistor
    delay(40);                        // switch debounce delay
    while (!digitalRead(BUTTON_PIN)); // wait for switch to open
    digitalWrite(LED_BUILTIN, HIGH);  // switch LED on
    delay(2000);                      // leave LED on for period
  }
  else {
    // switch open with pull-up resistor
    digitalWrite(LED_BUILTIN, LOW);   // switch LED off
  }
}

The only difference between this sketch and the previous sketch is the NOT operator (!) that is placed in front of the digitalRead() function in the if statement and while loop.

See how the logical NOT operator works in part 12 of the Arduino programming course where an explanation of the NOT operator can be found near the bottom of the page.

Amazon.com

Amazon.co.uk

Arduino IDE Example Sketch

Both the pull-up and pull-down push button module circuits can be used with the Arduino DigitalReadSerial example program from the Arduino IDE built-in example sketches. This example sketch reads the state of the push button switch from pin 2 of the Arduino and sends the value as either 1 or 0 over the USB serial link. The Serial Monitor window found in the Arduino IDE can then be used to display the state of the push button switch.

Find the example program in the Arduino IDE using the top menu under File → Examples → 01.Basics → DigitalReadSerial.

The tutorial for this sketch can be found on the Digital Read Serial tutorial page on the Arduino website.

← Go back to Photo Resistor Sensor Module Tutorial









Donate to Starting Electronics



Payoneer