5.4.3 Project 2.1 Read the Button
1. Description
The common table lamp uses LED lights and buttons, which can control the light on and off pressing the button.
We will work to read the status value of the button and display it on the serial monitor, so as to see it intuitively.
2. Button Principle
The button module is a digital sensor, which can only read 0 or 1. When the module is not pressed, it is in a high level state, that is, 1, when pressed, it is a low level 0.

3. Pins of the Button
Button 1 |
16 |
|---|---|
Button 2 |
27 |
4. Test Code
#define btn1 16
#define btn2 27
void setup() {
Serial.begin(9600);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
}
void loop() {
boolean btn1_val = digitalRead(btn1);
boolean btn2_val = digitalRead(btn2);
Serial.print("button1 = ");
Serial.print(btn1_val);
Serial.print(" ");
Serial.print("button2 = ");
Serial.println(btn2_val);
delay(100);
}
5. Test Result
Open the serial monitor of the arduino IDE

Press the button again to see the change of the button state value, as shown below:
