Button module experiment
Introduction to button module
The meaning of the digital I/O port is the INPUT and OUTPUT interface. In the previous LED
lamp experiment, we only used the OUTPUT function of GPIO. Now let's try to use the I/O
INPUT function in Arduino, which reads the output from an external device in this experiment.
We used buttons and LED lights to complete the experiment using INPUT and OUTPUT as
combinations.
The experiment purpose
We connect the button to interface D7 and the red light to interface D11. (all d0-d13 digital
I/O interfaces in Arduino controller can be used to connect the keys and indicator lights, but
try not to choose digital D0 and D1 interfaces, because D0 and D1 functions are reused. In
addition to I/O port functions, they are also used as serial communication [Link]
device was communicating with the PC when the program was [Link] we should
keep the D0 and D1 interfaces to avoid the trouble of inserting lines, we do not choose the D0
and D1 interfaces.
Component list
Keywish Arduino Uno mainboard
Breadboard
USB cable
Button * 1
1
LED light module * 1
Jumper wires
Schematic diagram
Wiring Circuit
Arduino Uno Key switch module
3 S
GND -
+5V Pin2
Arduino Uno LED
11 +
GND -
2
The experimental principle
By analyzing the circuit, we can know that when the button is pressed, the D7 interface is in
high level. It sets the D11 output pin into high level, which can make the light on. When the
D7 interface is read low, the D11 output remains low and the indicator goes off. The
principle is the same as above.
3
Code
int led_out = 11 ; //GPIO 11 LED pin
int keypad_pin = 7 ; // GPIO 7 key pin
int value;
void setup()
{
pinMode(led_out,OUTPUT); // init led pin output
pinMode(keypad_pin,INPUT); // init key pin input
}
void loop()
{
val = digitalRead(keypad_pin); // read key pad pin vaule
if( value == LOW )
{
digitalWrite(led_out,HIGH); // if key value is down turn on LED
}
else
{
digitalWrite(led_out,LOW); // if key value is down turn off LED
}
}
Download the program and complete the button experiment. Since the LED lamp module we
use is a tricolor lamp, we only need to connect the pin of one of the lamps. The experimental
principle is very simple, widely used in a variety of circuits and electrical appliances. In our
real life, it is not difficult to find it in various devices, such as everyone's mobile phone, press
the random button, the backlight will turn on, click the elevator button, the indicator light on
the elevator will light up and so on.
4
Experiment Result
Mblock grapgical programming program
mBlock writes the button module experiment program is shown in the following figure:
5
You can also open the program file directly with mblock, which is a. Sb2 file. Here are the
steps to open it:
Mixly graphical programming program
Mixly writes the button module experiment program is shown in the figure below:
6
7
Magicblock grapgical programming program