1. Overview
The single-way button module consists of one tap switch and one yellow button cap. We have learned how to code the board pin output high or low. Herein, we learn how to control the board pin to read high and low levels.
2. Parameters
3. Principle
When using, we only need to read the digital signal of the GPIO port connected to the module, so as to determine whether the button is pressed. When it is not pressed, S end is pulled up by the VCC through the R1 resistor. So S end is at a high level. When it is pressed, S is directly connected to GND, so S is at low.
4. Wiring Diagram
The button module is a digital module. It needs to be connected to a blue socket of the main board.
Here we connect the module to socket 3. From the port view of the kidsIOT board, socket 3 is digital port io26.
5. Test Code
/*
Project name: 6_Button_Basic
Function: read button state
Author: keyestudio
Hardware connection:
- Connect to port 3
Library:
- none (no need to import additional library)
Cautions:
- Connect to port 3
- Before uploading test code, please correctly connect to the development board and port
- Set the baud rate to 9600 before using serial monitor printing
*/
#define button_Pin 26 //set button pin to IO26
void setup() {
//Set the baud rate
Serial.begin(9600);
//set the pin to input
pinMode(button_Pin, INPUT);
}
void loop() {
//Read TTL level of pin IO26 (high level '1', low level '0')
int button_state = digitalRead(button_Pin);
//Serial port print character "Button State:", no wrap
Serial.print("Button State:");
//Serial port print the value of variable `button_state`, wrap
Serial.println(button_state);
//Delay 300ms to observe the printed values
delay(300);
}
6. Test Result
After uploading the test code, Click on the Arduino IDE and set the baud rate to
9600
. The serial monitor prints the button state. When the button is pressed, the state value is 0. When it is released, the value is 1.
7. Extension
Now that we have learned how to read the state of the button, how will we screen these state? if
comes in handy. It is used to determine whether the condition is satisfied. If yes, execute the code in “{ }”; if not, exit the execution.
Test Code:
/*
Project name: 6_Button_Basic_2
Function: read button state
Author: keyestudio
Hardware connection:
- Connect to port 3
Library:
- none (no need to import additional library)
Cautions:
- Connect to port 3
- Before uploading test code, please correctly connect to the development board and port
- Set the baud rate to 9600 before using serial monitor printing
*/
#define button_Pin 26 //set button pin to IO26
void setup() {
//Set the baud rate
Serial.begin(9600);
//set the pin to input
pinMode(button_Pin, INPUT);
}
void loop() {
//Read TTL level of pin IO26 (high level '1', low level '0')
int button_state = digitalRead(button_Pin);
//determine whether the button is pressed
if (button_state == 0) {
//if ues, "Button Down!"
Serial.println("Button Down!");
} else {
//if not, "Release the button!"
Serial.println("Release the button!");
}
//Delay 300ms to limit the printing speed
delay(300);
}
After uploading the test code, open the serial monitor and set the baud rate to 9600, and the serial monitor prints results. When the button is pressed, “Button Down!” is shown. If the button is released, serial monitor prints “Release the button!”.
8. Code Explanation
8.1 Serial.begin(9600);
Serial.begin(9600); set the serial baud rate. Serial port printing can be performed only when the serial baud rate is consistent. 9600 and 115200 are commonly used.
8.2 digitalRead(button_Pin);
digitalRead(button_Pin); read TTL level of the digital pin: high (1), low (0). Syntax: digitalRead(Pin);
Pin: digital pin that needs to be read
Please refer to the official website for details: digitalRead() | Arduino Documentation
8.3 Serial.print("Button State:");
Serial.print(); Serial print without line wrap. When printing, enter variables or characters to be printed in parentheses (printing characters need to be placed in double quotes).
8.4 Serial.println(button_state);
Serial.print(); Serial print with line wrap. When printing, enter variables or characters to be printed in parentheses (printing characters need to be placed in double quotes).
8.5 if() else
if() is used to determine whether the condition is satisfied. If yes, execute the code in “{ }”; if not, execute the code in else “{ }”.
Please refer to the official website for details: if | Arduino Documentation