Arduino 简明教程
Arduino - Connecting Switch
按钮或开关将电路中的两个开口端子连接起来。此示例在按连接到引脚8的按钮开关时打开引脚2上的LED。
Pushbuttons or switches connect two open terminals in a circuit. This example turns on the LED on pin 2 when you press the pushbutton switch connected to pin 8.

Pull-down Resistor
下拉电阻用于电子逻辑电路中,以确保在外部设备断开连接或处于高阻抗时,输入到Arduino会稳定在预期的逻辑电平。因为没有任何东西连接到输入引脚,这并不意味着它是一个逻辑零。下拉电阻连接在接地和设备上相应的引脚之间。
Pull-down resistors are used in electronic logic circuits to ensure that inputs to Arduino settle at expected logic levels if external devices are disconnected or are at high-impedance. As nothing is connected to an input pin, it does not mean that it is a logical zero. Pull down resistors are connected between the ground and the appropriate pin on the device.
数字电路中的下拉电阻示例如下图所示。按钮开关连接在电源电压和微控制器引脚之间。在这样的电路中,当开关闭合时,微控制器输入处于逻辑高值,但当开关打开时,下拉电阻将输入电压下拉至接地(逻辑零值),防止输入处于未定义状态。
An example of a pull-down resistor in a digital circuit is shown in the following figure. A pushbutton switch is connected between the supply voltage and a microcontroller pin. In such a circuit, when the switch is closed, the micro-controller input is at a logical high value, but when the switch is open, the pull-down resistor pulls the input voltage down to the ground (logical zero value), preventing an undefined state at the input.
下拉电阻的阻值必须大于逻辑电路的阻抗,否则它可能会将电压下拉得太低,并且无论开关位置如何,引脚上的输入电压仍将保持在恒定的逻辑低值。
The pull-down resistor must have a larger resistance than the impedance of the logic circuit, or else it might pull the voltage down too much and the input voltage at the pin would remain at a constant logical low value, regardless of the switch position.

Components Required
您将需要以下组件:
You will need the following components −
-
1 × Arduino UNO board
-
1 × 330 ohm resistor
-
1 × 4.7K ohm resistor (pull down)
-
1 × LED
Procedure
按照电路图进行操作,并按照下面给出的图片所示进行连接。
Follow the circuit diagram and make the connections as shown in the image given below.

Sketch
在电脑上打开 Arduino IDE 软件。使用 Arduino 语言编写代码将控制你的电路。单击新建打开一个新草图文件。
Open the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit. Open a new sketch File by clicking on New.

Arduino Code
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 8; // the number of the pushbutton pin
const int ledPin = 2; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Code to Note
当开关打开时(未按下按钮),按钮的两个端子之间没有连接,所以引脚通过(下拉电阻)连接到地,我们读到一个LOW。当开关闭合时(按下按钮),它会在其两个端子之间建立连接,将引脚连接到5伏,这样我们就能读到一个HIGH。
When the switch is open, (pushbutton is not pressed), there is no connection between the two terminals of the pushbutton, so the pin is connected to the ground (through the pull-down resistor) and we read a LOW. When the switch is closed (pushbutton is pressed), it makes a connection between its two terminals, connecting the pin to 5 volts, so that we read a HIGH.