Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.
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.
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.
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:
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.
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:
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.
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 } }
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.
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.
As an Amazon Associate I earn from qualifying purchases: