arduino一个按键控制两个灯的电路图
时间: 2024-11-03 17:20:06 浏览: 28
在一个基本的Arduino项目中,你可以使用一个按键(通常是数字输入类型的按钮)来控制两个LED灯泡(也作为数字输出)。以下是一个简单的电路图和步骤:
**电路图说明:**
1. Arduino UNO板(或类似型号):连接到电脑,提供微控制器处理数据。
2. 按键(如数字pin 2或3):连接到Arduino的数字输入端口,通常将其接地(连接到GND)并连接到5V电源(连接到VCC),同时连接到一个中间的中断(INT)引脚,例如数字pin 4或5。
3. LED灯1:将正极接到Arduino的数字输出pin 9(或其他可用的数字输出端口)的5V引脚,负极接到GND。
4. LED灯2:同理,将正极接到数字输出pin 10(或其他可用端口)的5V,负极接到GND。
**示例代码(基于Arduino IDE):**
```cpp
int buttonPin = 4; // 按钮引脚
int led1Pin = 9; // 灯1引脚
int led2Pin = 10; // 灯2引脚
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // 设置按钮为上拉模式
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == LOW) { // 如果按钮按下
digitalWrite(led1Pin, HIGH); // 开启灯1
digitalWrite(led2Pin, LOW); // 关闭灯2
} else { // 如果按钮松开
digitalWrite(led1Pin, LOW); // 关闭灯1
digitalWrite(led2Pin, HIGH); // 开启灯2
}
}
```
**注意事项:**
1. 使用上面的代码前,请确保你的Arduino版本支持中断功能。
2. 根据实际硬件连接调整电路和引脚。
3.
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)