Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Button Module

../_images/06_button.png

Introduction

The button module is an electronic device that detects the state of a button.They are usually used as switches to connect or break circuits.Buttons are used in many scenarios, such as doorbells, desk lamps, remote controls, elevators, fire alarms, etc.

Principle

The button module works on the principle of a switch. A switch is an electrical component that can be used to open or close a circuit.

The following is the internal structure of a button. The symbol on the right below is usually used to represent a button in circuits.

../_images/06_button_2.png

Since the pin 1 is connected to pin 2, and pin 3 to pin 4, when the button is pressed, the 4 pins are connected, thus closing the circuit.

../_images/06_button_3.png

Module Schematic Diagram

../_images/06_button_module_schematic.png

Usage

Hardware components

  • Arduino Uno R4 or R3 board * 1

  • Button Module * 1

  • Jumper Wires

Circuit Assembly

../_images/06_button_circuit.png

Code



Code explanation

  1. Setting Up: In this part of the code, we first declare the sensorPin as a constant integer and assign it the pin number we will connect our button to on the Arduino board. The setup() function sets the mode of the sensorPin as INPUT, meaning we’ll be receiving data in through this pin from the button. The Serial.begin() function initiates serial communication at a baud rate of 9600.

    const int sensorPin = 7;
    
    void setup() {
      pinMode(sensorPin, INPUT);
      Serial.begin(9600);
    }
    
  2. The Loop: The loop() function contains the main logic of the program. It continuously reads the button state and prints it to the serial monitor every 50 milliseconds. The digitalRead() function reads the state of the button, and the Serial.println() function prints this value to the serial monitor. The delay() function then pauses the execution for 50 milliseconds before the next reading. The button outputs a low level when pressed, and a high level when released.

    void loop() {
      Serial.println(digitalRead(sensorPin));
      delay(50);
    }
    

Additional Ideas

  • Use the button with if statements to control different outcomes in a program.

  • Make the button toggle an LED on and off instead of just printing to serial monitor.

More Projects