5. Button
5.1 Basic Knowledge of Independent Buttons
An independent button is a simple input device widely used in electronic devices for basic user interaction. Its operating principle is usually based on a simple mechanical switch. When the button is pressed, certain operations are triggered. Independent buttons can have various sizes, shapes, and colors, making them easy for users to identify and use.
5.2 Independent Button Structure
The main structure of an independent button includes a button cap, housing, spring, contacts, conductive plate, and pins. It consists of an elastic body, such as a spring or metal sheet, and a keycap. When the user presses the button, the elastic body compresses and pushes the keycap downward, causing the top of the button to approach or touch the base. When the user releases the button, the elastic body returns to its original state and the key returns to its initial position. Therefore, when the button is not pressed, the contacts are usually separated and the circuit is open. When the button is pressed, the conductive plate touches the contacts, forming a closed circuit.
5.3 Independent Button Driving Principle
An independent button driver allows a microcontroller to recognize the button state. A microcontroller can recognize high and low levels, so most buttons connect one side to a high level and the other side to a GPIO, or one side to a low level and the other side to a GPIO. By detecting whether the level on the button-connected pin changes, the program can determine whether the button is pressed.
5.4 Debouncing Measures
When a mechanical button closes or opens, mechanical vibration, similar to a spring effect, may cause the switch state to change several times in a short period. This is called button bounce. Debouncing methods are mainly divided into software debouncing and hardware debouncing:
- Software debouncing: uses programming to set a delay or timer so that the button state is read only once within a certain time, avoiding the effect of bouncing on the program.
- Hardware debouncing: adds components such as resistors and capacitors to the button circuit to form an RC low-pass filter, smoothing the button signal and reducing the effect of bounce.
5.5 Independent Button Schematic
In the development board schematic, one side of the button is connected through pull-up resistor R14 to the 3.3 V high level and GPIO0, while the other side is connected to GND, a low level. Simplifying the button circuit on the development board gives the diagram below.
When the development board is powered on, GPIO0 becomes high because of pull-up resistor R14. Therefore, when the button is not pressed, GPIO0 is high by default. When the button is pressed, the button closes and GPIO0 is connected to GND through the button, so GPIO0 becomes low.
5.6 Independent Button Driving Flow
The following is a simplified independent button driving flow:
- Initialize the microcontroller GPIO as input.
- Detect the button state, either by timed polling or interrupt triggering.
- Apply debouncing measures if bounce occurs.
- Execute the corresponding program code according to the button state.
- Wait for the next button trigger after the operation is complete.
❓What are timed polling and interrupt triggering?
- Timed polling, also called sequential detection, is a scanning-style button driving method. It continuously checks the button state inside a loop. When a button press is detected, the corresponding operation is performed.
- Interrupt triggering mainly uses the microcontroller interrupt function. When the button state changes, the microcontroller responds immediately, stops the current task, and executes the button response program. This method has higher real-time requirements.
❓We know that code can read the level on the button pin to determine whether the button is pressed. But how do we write the reading code?
digitalRead() is an Arduino function used to read the level state from a digital pin. When a pin is set to input mode, INPUT or INPUT_PULLUP, you can use this function to read the current level state of the pin. digitalRead() has only one parameter:
Pin number: the number of the digital pin you want to read.
The return value of
digitalRead()isHIGHorLOW.Example using
digitalRead():cint buttonState = digitalRead(2);1In this example, the value is read from pin 2, GPIO2. The returned value is stored in the variable
buttonState, and it will be eitherHIGHorLOW, indicating whether the level read from the pin is high or low.
5.7 Button-Controlled LED Verification
Set the LED pin to output mode and the button pin to input mode. When the button is pressed, debounce it first, then check again whether the button is still pressed. After confirming the button press, change the LED state.
// Define the LED pin
int led_pin = 48;
// Define the button pin
int button_pin = 0;
// Define the LED logic value
int led_logic = 0;
// Define the LED state variable. The default is false, or low level.
// It is used to determine whether the LED state has changed.
bool status = false;
// Run only once
void setup()
{
// Set the LED pin (48) to output mode
pinMode(led_pin, OUTPUT);
// Set the button pin (0) to pull-up input mode
pinMode(button_pin, INPUT_PULLUP);
}
// Loop continuously
void loop()
{
// If the button pin level read is 0, or low level,
// the button is pressed
if ( digitalRead(button_pin) == 0 )
{
// Delay 100 ms to eliminate button bounce
delay(100);
// If the button state is still low after debouncing,
// the press was not caused by button bounce
if (digitalRead(button_pin) == 0 )
{
// Invert the LED state variable, switching between low and high level
status = !status;
// Change the LED state
digitalWrite(led_pin, status);
}
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
5.8 Button-Controlled LED Effect
Press the button to turn the LED on, press it again to turn the LED off, and repeat.