Joystick Module(KY-023) CN: 두 판 사이의 차이
(새 문서: {{#seo:|title=ArduWiki:Arduino 摇杆模块指南|title_mode=append|keywords=Arduino, 信息科学, 创客教育, 项目式学习, 摇杆模块, Arduino作品, 毕业设计, Arduino示例代码|description=介绍如何使用Arduino控制摇杆模块(基础信息、电路、示例代码)。可用于信息与创客课程。}}center|class=coders100 通过电阻的变化控制X轴和Y轴的数值变化,中间按钮可以作为开...) |
(차이 없음)
|
2025년 3월 27일 (목) 18:12 판

通过电阻的变化控制X轴和Y轴的数值变化,中间按钮可以作为开关使用的模块。
常用于RC车、无人机等需要移动操作的项目中。
规格
- 工作电压:3.3V ~ 5V
- X轴、Y轴:模拟输出
- Z轴:数字输出(无上拉电阻)
示例所需硬件
- Arduino board
- 杜邦线
- 摇杆模块
- LCD 1602
连接方式
| Arduino Nano | LCD | 摇杆模块 |
| 5V | VCC | 5V |
| GND | GND | GND |
| A1 | VRx | |
| A0 | VRy | |
| D2 | SW | |
| A4 | SDA | |
| A5 | SCL |

示例代码
该示例会将摇杆的坐标与按钮状态显示在LCD上。
LCD的使用方法请参考链接页面。
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int xAxis = A1;
const int yAxis = A0;
const int zSwitch = 2;
void setup() {
pinMode(zSwitch, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("X:");
lcd.setCursor(8, 0);
lcd.print("Y:");
lcd.setCursor(0, 1);
lcd.print("Z : ");
}
void loop() {
for (int i = 2; i < 6; i++) {
lcd.setCursor(i, 0);
lcd.print(" ");
}
lcd.setCursor(2, 0);
lcd.print(analogRead(xAxis));
for (int i = 10; i < 14; i++) {
lcd.setCursor(i, 0);
lcd.print(" ");
}
lcd.setCursor(10, 0);
lcd.print(analogRead(yAxis));
lcd.setCursor(4, 1);
lcd.print(digitalRead(zSwitch));
delay(100);
}
执行结果