0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

可以用Arduino来制作USB设备吗?尝试通过Arduino Pro Micro(Leonardo)使用HID功能

麦特拉布 来源:麦特拉布 作者:麦特拉布 2023-02-23 09:49 次阅读

这篇文章来源于DevicePlus.com英语网站的翻译稿。

pYYBAGPy0UWARwjsAAC4diR2CL4190.jpg

本文最初发布在deviceplus.jp网站上,而后被翻译成英语。

目录

前言

电子设计步骤

关于Arduino Pro Micro

使之被识别为HID

使用操纵杆创建鼠标设备

结论

相关文章

前言

本文中,我将介绍一种不一样的Arduino使用方式。乍一看,照片中的Arduino看起来像我们之前系列中使用过的Arduino Pro Mini,但其实这是另一种Arduino。它被称为“Arduino Pro Micro”。虽然“Mini”变成了“Micro”,尺寸却并没有发生任何变化,因此,两者有点难以区分。这种Arduino在连接到电脑时会被识别为鼠标或键盘等HID设备。

电子设计步骤

预计完成时间:60分钟
所需元器件

Arduino主机(Arduino Pro Micro)

面包板

双轴操纵杆模块#27800

轻触开关

220Ω 电阻

LED

1. 关于Arduino Pro Micro

Arduino Pro Micro是一种Arduino,配备有名为“ATmega32U4”的芯片(UNO等配有ATmega328P等)。该芯片最大的特点是当通过USB连接时会伪装成键盘和鼠标等人机接口设备(HID)。配备ATmega32U4的Arduino除了“Pro Micro”之外,还被称为“Arduino Leonardo”,是非常有名的开发板。

在编写程序时,您可以选择名为“Arduino Leonardo.”的开发板。

poYBAGPy0UmAB-oCAADU0F1oL-0420.jpg

乍一看,Arduino Pro Mini与Arduino Pro Micro的外观非常相似。

但是,Pro Micro具有可以连接到智能手机等设备的USB连接器,而Pro Mini只有一个串行连接器

2. 使之被识别为HID

现在,我们让外观相似的Arduino Pro Micro读取示例程序并尝试让电脑将其识别为HID。

尝试运行Arduino IDE的“File”-“Sketch Example”-“09.USB”-“Keyboard”-“KeyboardMessage”程序。

在这个程序中,我们创建一个在引脚4上设有开关的简单电路,当引脚4被按下时,应通过键盘输入显示按下的次数。

(这次,我将引脚4改换为引脚7)

pYYBAGPy0U2AWC-YAAHaqVzXCI0715.png

#include "Keyboard.h"
 
const int buttonPin = 7; // input pin for pushbutton
int previousButtonState = HIGH; // for checking the state of a pushButton
int counter = 0; // button push counter
 
void setup() {
 // make the pushButton pin an input:
 pinMode(buttonPin, INPUT);
 // initialize control over the keyboard:
 Keyboard.begin();
}
 
void loop() {
 // read the pushbutton:
 int buttonState = digitalRead(buttonPin);
 // if the button state has changed,
 if ((buttonState != previousButtonState)
 // and it's currently pressed:
 && (buttonState == HIGH)) {
 // increment the button counter
 counter++;
 // type out a message
 Keyboard.print("You pressed the button ");
 Keyboard.print(counter);
 Keyboard.println(" times.");
 }
 // save the current button state for comparison next time:
 previousButtonState = buttonState;
}

pYYBAGPy0VCAc5W3AAA3rOOUYYI513.jpg

编写程序并打开记事本后,无需触碰键盘,每按一次按钮,就会按照上面的描述进行计数。

如果可以如此轻松地制作USB设备,那么就可以实现更多梦想!

3. 使用操纵杆创建鼠标设备

我们已经知道Arduino Pro Micro可以用作HID,下面我想通过将它与其他一些元器件组合来创建鼠标设备。这一次,我将使用曾经在无线电控制设备制作中使用过的操纵杆,并尝试创建一个可以用操纵杆和轻触开关来代替鼠标的设备。

首先,准备一个可用于设置操纵杆方向的程序。

pYYBAGPy0VKATPhwAADfvofeKFU829.jpg

pYYBAGPy0VWAWtoSAACCly1rnS4275.jpg

将电路添加到之前的轻触开关电路中。将操纵杆和后面要使用的LED连接到引脚2。

Code Example

const int _UDPIN = A0; // UD Input
const int _LRPIN = A1; // LR Input
const int _SWPIN = 7; // Digital Pin
int _UD = 0; // Value for Up/Down
int _LR = 0; // Value for Left/Right
 
void setup() {
 Serial.begin(9600);
 pinMode(_SWPIN,INPUT) ;
}
 
void loop() {
 _UD = analogRead(_UDPIN);
 _LR = analogRead(_LRPIN);
 Serial.print("UP-DOWN:");
 Serial.print(_UD, DEC);
 Serial.print(" - Left-Rright:");
 Serial.println(_LR, DEC);
 if (digitalRead(_SWPIN) == HIGH) {
 Serial.println("switch on");
 }
 delay(100);
}

poYBAGPy0ViAcrVDAABZt_-UoZQ683.jpg

经过确认,可以知道它读取了程序,转动操纵杆时数字会发生变化。

接下来,让我们将操纵杆数字值转换为鼠标坐标。实际上,这个程序也是已经备好的示例程序,所以让我们来用用看。请选择“File”-“Sketch Example”-“09.USB”-“Mouse”-“JoystickMouseControl”。

执行此程序时,会将上下(模拟引脚A2)和左右(模拟引脚A1)的值反映在鼠标坐标上。此外,由于引脚2通过接入5V电源来实现开关功能的,因此可以通过将引脚2与VCC相连或将开关夹在中间的方式来打开/关闭设备。

Code Example

#include "Mouse.h"
 
// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 5; // switch to turn on and off mouse control
const int mouseButton = 7; // input pin for the mouse pushButton
const int xAxis = A1; // joystick X axis
const int yAxis = A2; // joystick Y axis
const int ledPin = 2; // Mouse control LED
 
// parameters for reading the joystick:
int range = 12; // output range of X or Y movement
int responseDelay = 5; // response delay of the mouse, in ms
int threshold = range / 4; // resting threshold
int center = range / 2; // resting position value
 
boolean mouseIsActive = false; // whether or not to control the mouse
int lastSwitchState = LOW; // previous switch state
 
void setup() {
pinMode(switchPin, INPUT); // the switch pin
pinMode(ledPin, OUTPUT); // the LED pin
// take control of the mouse:
Mouse.begin();
}
 
void loop() {
// read the switch:
int switchState = digitalRead(switchPin);
// if it's changed and it's high, toggle the mouse state:
if (switchState != lastSwitchState) {
if (switchState == HIGH) {
mouseIsActive = !mouseIsActive;
// turn on LED to indicate mouse state:
digitalWrite(ledPin, mouseIsActive);
}
}
// save switch state for next comparison:
lastSwitchState = switchState;
 
// read and scale the two axes:
int xReading = readAxis(A0);
int yReading = readAxis(A1);
 
// if the mouse control state is active, move the mouse:
if (mouseIsActive) {
Mouse.move(xReading, yReading, 0);
}
 
// read the mouse button and click or not click:
// if the mouse button is pressed:
if (digitalRead(mouseButton) == HIGH) {
// if the mouse is not pressed, press it:
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
// else the mouse button is not pressed:
else {
// if the mouse is pressed, release it:
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}
 
delay(responseDelay);
}
 
/*
reads an axis (0 or 1 for x or y) and scales the
analog input range to a range from 0 to 
*/
 
int readAxis(int thisAxis) {
// read the analog input:
int reading = analogRead(thisAxis);
 
// map the reading from the analog input range to the output range:
reading = map(reading, 0, 1023, 0, range);
 
// if the output reading is outside from the
// rest position threshold, use it:
int distance = reading - center;
 
if (abs(distance) < threshold) {
distance = 0;
}
 
// return the distance for this axis:
return distance;
}

完成编程后,我们来尝试让它动起来。

哦,它真的动起来了!

结论

这次,我们学习了使用Arduino Pro Micro创建基于Arduino的USB设备时的基本流程。在下一篇文章中,我们将进一步深化应用Arduino Pro Micro,尝试创建更具“Device Plus”风格的USB设备,让项目更具挑战性!

审核编辑:汤梓红

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • usb
    usb
    +关注

    关注

    60

    文章

    7768

    浏览量

    262330
  • HID
    HID
    +关注

    关注

    2

    文章

    125

    浏览量

    46445
  • Arduino
    +关注

    关注

    186

    文章

    6453

    浏览量

    185900
收藏 人收藏

    评论

    相关推荐

    怎样制作一个基于Arduino Pro Micro与ADXL345的陀螺仪体感鼠标呢

    为什么要制作一个基于Arduino Pro Micro与ADXL345的陀螺仪体感鼠标呢?怎样制作一个基于
    发表于 03-01 06:42

    制作基于Arduino的多功能电能表

    描述在本教程中,我将向您展示如何制作基于 Arduino 的多功能电能表。这个小仪表是一个非常有用的设备可以显示有关电气参数的重要信息。该
    发表于 08-10 06:33

    使用Arduino pro microUSB

    描述USB垫 使用Arduino pro micro(或单个 USB的超级简单宏板)的垫子。这个使用 16 键帽的 Cherry MX 风格
    发表于 08-31 07:44

    使用arduino pro micro制作一个游戏控制器

    pro micro制作一个游戏控制器所以这里的一个建议是:请不要用 Arduino Uno 尝试这个项目,因为
    发表于 09-01 06:38

    分享一个不错的使用Arduino Leonardo PC音量控制的项目

    描述Arduino Leonardo PC 音量控制这是一个使用 Arduino Leonardo、电位计和 10 个 LED 控制计算机
    发表于 09-01 06:18

    Arduino_1.5.5_软件下载

    。主体构造和Arduino Leonardo的相同,但内嵌了一块ATmega32U4晶片,可以通过USB端口
    发表于 04-03 17:27 247次下载

    Arduino_1.5.6_软件下载

    。主体构造和Arduino Leonardo的相同,但内嵌了一块ATmega32U4晶片,可以通过USB端口
    发表于 04-03 18:43 224次下载

    怎样Arduinopromicro将电脑触控板转换为USB设备

    microLeonardo,因为它们可以被编程为显示为HID(人机界面设备),这意味着它们可以
    的头像 发表于 08-07 11:29 1.2w次阅读

    怎样将USB游戏控制器添加到Arduino Leonardo / Micro

    Arduino MicroArduino Leonardo应该出现在已安装游戏控制器的列表中。选择Arduino
    的头像 发表于 12-11 11:15 4384次阅读
    怎样将<b class='flag-5'>USB</b>游戏控制器添加到<b class='flag-5'>Arduino</b> <b class='flag-5'>Leonardo</b> / <b class='flag-5'>Micro</b>

    如何对便宜又紧凑的Arduino Pro Mini进行编程

      如果您确实需要 USB 接口,另一个非常好的选择是“ArduinoPro Micro。它的大小与 Arduino
    的头像 发表于 06-10 07:48 4094次阅读
    如何对便宜又紧凑的<b class='flag-5'>Arduino</b> <b class='flag-5'>Pro</b> Mini进行编程

    Arduino Leonardo的电路原理图

    Arduino Leonardo的电路原理图
    发表于 06-17 14:51 0次下载

    使用Arduino pro micro板的USB宏垫

    电子发烧友网站提供《使用Arduino pro micro板的USB宏垫.zip》资料免费下载
    发表于 08-11 10:42 0次下载
    使用<b class='flag-5'>Arduino</b> <b class='flag-5'>pro</b> <b class='flag-5'>micro</b>板的<b class='flag-5'>USB</b>宏垫

    使用Arduino Leonardo和红外传感器制作手势控制设备

    电子发烧友网站提供《使用Arduino Leonardo和红外传感器制作手势控制设备.zip》资料免费下载
    发表于 10-26 15:57 0次下载
    使用<b class='flag-5'>Arduino</b> <b class='flag-5'>Leonardo</b>和红外传感器<b class='flag-5'>制作</b>手势控制<b class='flag-5'>设备</b>

    Raspberry Pi和Arduino Micro制作的虚拟窥视孔

    电子发烧友网站提供《Raspberry Pi和Arduino Micro制作的虚拟窥视孔.zip》资料免费下载
    发表于 11-11 11:23 0次下载
    <b class='flag-5'>用</b>Raspberry Pi和<b class='flag-5'>Arduino</b> <b class='flag-5'>Micro</b><b class='flag-5'>制作</b>的虚拟窥视孔

    基于Arduino Micro的简单USB MIDI适配器

    方案介绍这是个基于 Arduino MicroLeonardo 的简单 USB 到 MIDI 适配器,可选择过滤 MIDI 数据。我在网上搜索了如何使用
    发表于 12-13 16:08 2次下载