Arudino Uno R3开发板简介

Arudino Uno R3开发板引脚说明

电路图

目标:使用四角按键开关控制LED灯,当按钮按下时灯亮,松开时灯灭。

实现思路:将引脚2改为input mode,反映按键的状态。引脚12为output mode,控制led灯的亮灭。当按键按下时,引脚2为高电平,此时led灯亮;当按钮松开时,引脚2为低电平,此时led灯灭。(对应仿真图连线)

如果引脚2的状态为input pullup mode(如果外部组件未启用,上拉电阻将输入端口处的电压拉到高电平),当按键按下时,引脚2为低电平,此时led灯亮;当按钮松开时,引脚2为高电平,此时led灯灭。(对应实物连线)

注意事项:四角按键同侧不相连,相连不同侧。

仿真
实物

Arduino程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int buttonPin = 2;
int ledPin = 12;

void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(buttonPin) == LOW){
digitalWrite(ledPin, HIGH);
} else{
digitalWrite(ledPin, LOW);
}
}

参考文章