Button Module¶
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.
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.
Usage¶
Hardware components
Arduino Uno R4 or R3 board * 1
Button Module * 1
Jumper Wires
Circuit Assembly
Code¶
Code explanation¶
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. Thesetup()
function sets the mode of thesensorPin
asINPUT
, meaning we’ll be receiving data in through this pin from the button. TheSerial.begin()
function initiates serial communication at a baud rate of 9600.const int sensorPin = 7; void setup() { pinMode(sensorPin, INPUT); Serial.begin(9600); }
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. ThedigitalRead()
function reads the state of the button, and theSerial.println()
function prints this value to the serial monitor. Thedelay()
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.