0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

可以用Arduino來制作USB設(shè)備嗎?嘗試通過Arduino Pro Micro(Leonardo)使用HID功能

麥特拉布 ? 來源:麥特拉布 ? 作者:麥特拉布 ? 2023-02-23 09:49 ? 次閱讀

這篇文章來源于DevicePlus.com英語網(wǎng)站的翻譯稿。

pYYBAGPy0UWARwjsAAC4diR2CL4190.jpg

本文最初發(fā)布在deviceplus.jp網(wǎng)站上,而后被翻譯成英語。

目錄

前言

電子設(shè)計(jì)步驟

關(guān)于Arduino Pro Micro

使之被識別為HID

使用操縱桿創(chuàng)建鼠標(biāo)設(shè)備

結(jié)論

相關(guān)文章

前言

本文中,我將介紹一種不一樣的Arduino使用方式。乍一看,照片中的Arduino看起來像我們之前系列中使用過的Arduino Pro Mini,但其實(shí)這是另一種Arduino。它被稱為“Arduino Pro Micro”。雖然“Mini”變成了“Micro”,尺寸卻并沒有發(fā)生任何變化,因此,兩者有點(diǎn)難以區(qū)分。這種Arduino在連接到電腦時(shí)會被識別為鼠標(biāo)或鍵盤等HID設(shè)備。

電子設(shè)計(jì)步驟

預(yù)計(jì)完成時(shí)間:60分鐘
所需元器件

Arduino主機(jī)(Arduino Pro Micro)

面包板

雙軸操縱桿模塊#27800

輕觸開關(guān)

220Ω 電阻

LED

1. 關(guān)于Arduino Pro Micro

Arduino Pro Micro是一種Arduino,配備有名為“ATmega32U4”的芯片(UNO等配有ATmega328P等)。該芯片最大的特點(diǎn)是當(dāng)通過USB連接時(shí)會偽裝成鍵盤和鼠標(biāo)等人機(jī)接口設(shè)備(HID)。配備ATmega32U4的Arduino除了“Pro Micro”之外,還被稱為“Arduino Leonardo”,是非常有名的開發(fā)板。

在編寫程序時(shí),您可以選擇名為“Arduino Leonardo.”的開發(fā)板。

poYBAGPy0UmAB-oCAADU0F1oL-0420.jpg

乍一看,Arduino Pro Mini與Arduino Pro Micro的外觀非常相似。

但是,Pro Micro具有可以連接到智能手機(jī)等設(shè)備的USB連接器,而Pro Mini只有一個(gè)串行連接器。

2. 使之被識別為HID

現(xiàn)在,我們讓外觀相似的Arduino Pro Micro讀取示例程序并嘗試讓電腦將其識別為HID。

嘗試運(yùn)行Arduino IDE的“File”-“Sketch Example”-“09.USB”-“Keyboard”-“KeyboardMessage”程序。

在這個(gè)程序中,我們創(chuàng)建一個(gè)在引腳4上設(shè)有開關(guān)的簡單電路,當(dāng)引腳4被按下時(shí),應(yīng)通過鍵盤輸入顯示按下的次數(shù)。

(這次,我將引腳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

編寫程序并打開記事本后,無需觸碰鍵盤,每按一次按鈕,就會按照上面的描述進(jìn)行計(jì)數(shù)。

如果可以如此輕松地制作USB設(shè)備,那么就可以實(shí)現(xiàn)更多夢想!

3. 使用操縱桿創(chuàng)建鼠標(biāo)設(shè)備

我們已經(jīng)知道Arduino Pro Micro可以用作HID,下面我想通過將它與其他一些元器件組合來創(chuàng)建鼠標(biāo)設(shè)備。這一次,我將使用曾經(jīng)在無線電控制設(shè)備制作中使用過的操縱桿,并嘗試創(chuàng)建一個(gè)可以用操縱桿和輕觸開關(guān)來代替鼠標(biāo)的設(shè)備。

首先,準(zhǔn)備一個(gè)可用于設(shè)置操縱桿方向的程序。

pYYBAGPy0VKATPhwAADfvofeKFU829.jpg

pYYBAGPy0VWAWtoSAACCly1rnS4275.jpg

將電路添加到之前的輕觸開關(guān)電路中。將操縱桿和后面要使用的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

經(jīng)過確認(rèn),可以知道它讀取了程序,轉(zhuǎn)動操縱桿時(shí)數(shù)字會發(fā)生變化。

接下來,讓我們將操縱桿數(shù)字值轉(zhuǎn)換為鼠標(biāo)坐標(biāo)。實(shí)際上,這個(gè)程序也是已經(jīng)備好的示例程序,所以讓我們來用用看。請選擇“File”-“Sketch Example”-“09.USB”-“Mouse”-“JoystickMouseControl”。

執(zhí)行此程序時(shí),會將上下(模擬引腳A2)和左右(模擬引腳A1)的值反映在鼠標(biāo)坐標(biāo)上。此外,由于引腳2通過接入5V電源來實(shí)現(xiàn)開關(guān)功能的,因此可以通過將引腳2與VCC相連或?qū)㈤_關(guān)夾在中間的方式來打開/關(guān)閉設(shè)備。

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;
}

完成編程后,我們來嘗試讓它動起來。

哦,它真的動起來了!

結(jié)論

這次,我們學(xué)習(xí)了使用Arduino Pro Micro創(chuàng)建基于Arduino的USB設(shè)備時(shí)的基本流程。在下一篇文章中,我們將進(jìn)一步深化應(yīng)用Arduino Pro Micro,嘗試創(chuàng)建更具“Device Plus”風(fēng)格的USB設(shè)備,讓項(xiàng)目更具挑戰(zhàn)性!

審核編輯:湯梓紅

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報(bào)投訴
  • usb
    usb
    +關(guān)注

    關(guān)注

    60

    文章

    7773

    瀏覽量

    262409
  • HID
    HID
    +關(guān)注

    關(guān)注

    2

    文章

    125

    瀏覽量

    46448
  • Arduino
    +關(guān)注

    關(guān)注

    187

    文章

    6453

    瀏覽量

    185933
收藏 人收藏

    評論

    相關(guān)推薦

    怎樣制作一個(gè)基于Arduino Pro Micro與ADXL345的陀螺儀體感鼠標(biāo)呢

    為什么要制作一個(gè)基于Arduino Pro Micro與ADXL345的陀螺儀體感鼠標(biāo)呢?怎樣制作一個(gè)基于
    發(fā)表于 03-01 06:42

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

    描述在本教程中,我將向您展示如何制作基于 Arduino 的多功能電能表。這個(gè)小儀表是一個(gè)非常有用的設(shè)備,可以顯示有關(guān)電氣參數(shù)的重要信息。該
    發(fā)表于 08-10 06:33

    使用Arduino pro microUSB

    描述USB墊 使用Arduino pro micro(或單個(gè) USB的超級簡單宏板)的墊子。這個(gè)使用 16 鍵帽的 Cherry MX 風(fēng)格
    發(fā)表于 08-31 07:44

    使用arduino pro micro制作一個(gè)游戲控制器

    pro micro制作一個(gè)游戲控制器所以這里的一個(gè)建議是:請不要用 Arduino Uno 嘗試這個(gè)項(xiàng)目,因?yàn)?
    發(fā)表于 09-01 06:38

    分享一個(gè)不錯(cuò)的使用Arduino Leonardo PC音量控制的項(xiàng)目

    描述Arduino Leonardo PC 音量控制這是一個(gè)使用 Arduino Leonardo、電位計(jì)和 10 個(gè) LED 控制計(jì)算機(jī)
    發(fā)表于 09-01 06:18

    Arduino_1.5.5_軟件下載

    。主體構(gòu)造和Arduino Leonardo的相同,但內(nèi)嵌了一塊ATmega32U4晶片,可以通過USB端口
    發(fā)表于 04-03 17:27 ?247次下載

    Arduino_1.5.6_軟件下載

    。主體構(gòu)造和Arduino Leonardo的相同,但內(nèi)嵌了一塊ATmega32U4晶片,可以通過USB端口
    發(fā)表于 04-03 18:43 ?224次下載

    怎樣Arduinopromicro將電腦觸控板轉(zhuǎn)換為USB設(shè)備

    microLeonardo,因?yàn)樗鼈?b class='flag-5'>可以被編程為顯示為HID(人機(jī)界面設(shè)備),這意味著它們可以
    的頭像 發(fā)表于 08-07 11:29 ?1.2w次閱讀

    怎樣將USB游戲控制器添加到Arduino Leonardo / Micro

    Arduino MicroArduino Leonardo應(yīng)該出現(xiàn)在已安裝游戲控制器的列表中。選擇Arduino
    的頭像 發(fā)表于 12-11 11:15 ?4414次閱讀
    怎樣將<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進(jìn)行編程

      如果您確實(shí)需要 USB 接口,另一個(gè)非常好的選擇是“ArduinoPro Micro。它的大小與 Arduino
    的頭像 發(fā)表于 06-10 07:48 ?4102次閱讀
    如何對便宜又緊湊的<b class='flag-5'>Arduino</b> <b class='flag-5'>Pro</b> Mini進(jìn)行編程

    Arduino Leonardo的電路原理圖

    Arduino Leonardo的電路原理圖
    發(fā)表于 06-17 14:51 ?0次下載

    使用Arduino pro micro板的USB宏墊

    電子發(fā)燒友網(wǎng)站提供《使用Arduino pro micro板的USB宏墊.zip》資料免費(fèi)下載
    發(fā)表于 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和紅外傳感器制作手勢控制設(shè)備

    電子發(fā)燒友網(wǎng)站提供《使用Arduino Leonardo和紅外傳感器制作手勢控制設(shè)備.zip》資料免費(fèi)下載
    發(fā)表于 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'>設(shè)備</b>

    Raspberry Pi和Arduino Micro制作的虛擬窺視孔

    電子發(fā)燒友網(wǎng)站提供《Raspberry Pi和Arduino Micro制作的虛擬窺視孔.zip》資料免費(fèi)下載
    發(fā)表于 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適配器

    方案介紹這是個(gè)基于 Arduino MicroLeonardo 的簡單 USB 到 MIDI 適配器,可選擇過濾 MIDI 數(shù)據(jù)。我在網(wǎng)上搜索了如何使用
    發(fā)表于 12-13 16:08 ?2次下載