3. Control LED with Push Button Switch
In the previous chapter, we have learned to control LED blinking. Now, let us learn how to control the LED through the buttons.
3.1. Project 3.1 Control LED with Button
We will use the Freenove Projects Board to get the status of the push button switch, and show it through LED.
3.1.1. Component List
Control board x1
|
USB cable x1
|
Freenove Projects Board
|
|
3.1.2. Circuit Knowledge
3.1.2.1. Connection of Push Button Switch
In Chapter 1, we connect push button switch directly to power up the circuit to control the LED to turn on or off. In digital circuits, we need to use the push button switch as an input signal. The recommended connection is as follows:
In the above circuit diagram, when the button is not pressed, 5V (high level) will be detected by control board port; and 0V (low level) when the button is pressed. The role of Resistor R2 here is to prevent the port from being set to output high level by accident. Without R2, the port could be connected directly to the cathode and cause a short circuit when the button is pressed.
The following diagram shows another connection, in which the level detected by the control board port is opposite to the above diagram, whenever the button is pressed or not.
3.1.3. Circuit
Use pin 2 of control board to detect the status of push button, and pin 13 to drive LED.
Schematic diagram |
|
Hardware connection Insert the Control Board upside down to the Freenove Projects Board, and then turn the corresponding switch to the right (On) |
For other functions that are not being used, it is recommended to turn their DIP switches to the left. |
3.1.4. Sketch
3.1.4.1. Control_LED_by_Button
Now, write code to detect the state of push button, and show it through LED.
1/*
2 Control_LED_by_Button
3
4 modified 2021/6/30
5 by http://www.freenove.com
6*/
7
8int buttonPin = 2; // the number of the push button pin
9int ledPin = 13; // the number of the LED pin
10
11void setup() {
12 pinMode(buttonPin, INPUT); // set push button pin into input mode
13 pinMode(ledPin, OUTPUT); // set LED pin into output mode
14}
15
16void loop() {
17 if (digitalRead(buttonPin) == HIGH) // if the button is not pressed
18 digitalWrite(ledPin, LOW); // switch off LED
19 else // if the button is pressed
20 digitalWrite(ledPin, HIGH); // switch on LED
21}
After the port is initialized, the LED will be turned on or off in accordance with the state of the pin connected to push button switch.
- digitalRead(pin)
Arduino IDE provides a function digitalRead(pin) to obtain the state of the port pin. The return value is HIGH or LOW, that is, high level or low level.
Verify and upload the code, press the button, LED lights up; release the button, LED lights off.
3.2. Project 3.2 Change LED State by Button
In the previous section, we have finished the experiment that LED lights ON when push button switch is pressed, and lights OFF as soon as it’s released. Now, let’s try something new: each time you press the button down, the state of LED will be changed.
3.2.1. Component List
Same with the previous section.
3.2.2. Circuit Knowledge
3.2.2.1. Debounce a push button switch
When a Momentary Push Button Switch is pressed, it will not change from one state to another state immediately. Due to tiny mechanical vibrations, there will be a short period of continuous buffeting before it stabilizes in a new state too fast for Humans to detect but not for computer microcontrollers. The same is true when the push button switch is released. This unwanted phenomenon is known as “bounce”.
Therefore, if we can directly detect the state of the Push Button Switch, there are multiple pressing and releasing actions in one pressing cycle. This buffeting will mislead the high-speed operation of the microcontroller to cause many false decisions. Therefore, we need to eliminate the impact of buffeting. Our solution: to judge the state of the button multiple times. Only when the button state is stable (consistent) over a period of time, can it indicate that the button is actually in the ON state (being pressed).
3.2.3. Circuit
Same with the previous section.
3.2.4. Sketch
3.2.4.1. Change_LED_State_by_Button
Now, write a code to detect the state of the push button switch. Every time you pressed it, the state of LED will be changed.
1/*
2 Change_LED_State_by_Button
3
4 modified 2021/6/30
5 by http://www.freenove.com
6*/
7
8int buttonPin = 2; // the number of the push button pin
9int ledPin = 13; // the number of the LED pin
10boolean isLighting = false; // define a variable to save the state of LED
11
12void setup() {
13 pinMode(buttonPin, INPUT); // set push button pin into input mode
14 pinMode(ledPin, OUTPUT); // set LED pin into output mode
15}
16
17void loop() {
18 if (digitalRead(buttonPin) == LOW) { // if the button is pressed
19 delay(10); // delay for a certain time to skip the bounce
20 if (digitalRead(buttonPin) == LOW) { // confirm again if the button is pressed
21 reverseLED(); // reverse LED
22 while (digitalRead(buttonPin) == LOW); // wait for releasing
23 delay(10); // delay for a certain time to skip bounce when the button is released
24 }
25 }
26}
27
28void reverseLED() {
29 if (isLighting) { // if LED is lighting,
30 digitalWrite(ledPin, LOW); // switch off LED
31 isLighting = false; // store the state of LED
32 }
33 else { // if LED is off,
34 digitalWrite(ledPin, HIGH); // switch LED
35 isLighting = true; // store the state of LED
36 }
37}
Verify and upload the code, then each time you press the button, LED changes its state accordingly.
When judging the push button switch state, if it is detected as “pressed down”, wait for a certain time to detect again to eliminate the effect of bounce. When the state is stable, released push button switch, and wait for a certain time to eliminate the effect of bounce after it is released.




