logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Arduino

Can Arduino make USB devices? Try using the HID function with Arduino Pro Micro (Leonardo)

DevicePlus Editorial Team
Published by DevicePlus Editorial Team at January 11, 2022
Categories
  • Arduino
Tags
using the HID function with Arduino Pro Micro

Originally published by Dec 12, 2019

arduino connected to PC

This article was translated to English, and was originally published for deviceplus.jp.

Table of Contents

  1. Introduction
  2. Electronic work recipe
    1. About Arduino Pro Micro
    2. Try to detect components as an HID
    3. How to make a mouse device out of a joystick
  3. Conclusion
  4. Related articles

Introduction

Today, I’ll be introducing a different way to use Arduino. At first glance, it looks like the Arduino Pro Mini used in our previous series, but this Arduino is actually a different type. It’s called the “Arduino Pro Micro.” Although the Mini became the Micro, the size hasn’t changed at all, so it’s a little difficult to distinguish between the two. However, this Arduino is different once it’s connected to a PC since it can detect your mouse and keyboard like an HID (Human Interface Device).

Electronic work recipe

Est time:60 mins
Necessary parts

  • Arduino main console (Arduino Pro Micro)
  • Breadboard
  • 2-axis joystick module # 27800
  • Tact switch
  • 220Ω resistor
  • LED

1. About Arduino Pro Micro

Arduino Pro Micro is an Arduino equipped with a chip called ATmega32U4 (UNO etc. is equipped with ATmega328P etc.). This chip’s biggest characteristic is being able to pretend that it’s a human interface device (HID), such as using a keyboard and mouse when connected by USB. Arduino equipped with ATmega32U4 is famous for the Arduino Leonardo board in addition to Pro Micro.

When writing a program, you can select and write a board called “Arduino Leonardo.”

Arduino Pro Micro

Arduino Pro Mini at a glance next to the similar-looking Arduino Pro Micro.

However, Pro Micro has a USB connector that can be connected to a smartphone, etc., compared to Pro Mini with a serial connector.

2. Try to detect components as an HID

Let’s try having the similar-looking Arduino Pro Micro read the sample program and try to get the computer to recognize it as an HID.

Try to run Arduino IDE’s “File”-“Sketch Example”-“09.USB”-“Keyboard”-“KeyboardMessage” program.

In this program, create a simple circuit with a switch on the 4th pin. When the 4th pin is pressed, the number of times pressed should be displayed by keyboard input.

(This time, I changed from Pin 4 to Pin 7)

 

Code Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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;
}

 

program to detect as an HID

 

After writing a program and opening a notepad, every time you press a button, it’ll count up with the above description without touching the keyboard.

If you can make a USB device this easily, then you can dream big!

3. How to make a mouse device out of a joystick

Now that we know that it can be used as an HID, I’d like to make a mouse device by combining it with some other parts. This time, I’ll use a joystick that I used once for radio control production, and try to make it a device that can be used as a mouse with a joystick and tact switch.

First, prepare a program that you can use to set up the directions for the joystick.

 

mouse device from a joystick

 

mouse device from a joystick

 

The circuit will be added to the previous tact switch circuit. The joystick and the LED to be used later are connected to the 2nd pin.

Code Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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);
}

 

program confirmation joystick moved

 

Looks like we were able to confirm that it read the program since the digits change as you rotate the joystick.

Let’s actually convert the joystick digits to mouse coordinates. This program is actually already prepared on the sample, so let’s use it. Select “File”-“Sketch Example”-“09.USB”-“Mouse”-“JoystickMouseControl.”

When this program is executed, the top and bottom analog A2 pins and the left and right A1 pin numbers are reflected on the mouse coordinates. In addition, since a switch is turned on by turning on the 5V power supply to No.2, the device can be turned on/off by connecting No.2 to VCC or sandwiching the switch.

Code Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#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;
}

 

Once you’ve written the program, try moving it.

Oh, it’s actually moving!

Conclusion

This time, we learned the basic process to make a USB device with Arduino using Arduino Pro Micro. Next time, I’ll take it to a more challenging level by trying to make a USB device look like device plus!

Related Articles

What else can you accomplish with Arduino? Explore related articles to find out:

  • Can Arduino make USB devices? Creating devices on Arduino Pro Micro (Leonardo)
  • How to connect your Arduino to Wi-Fi
  • How to control an Arduino from a Windows computer
  • How to control your computer using hand gestures
  • How to Send IR Commands to Your Project with a Remote Control

 

DevicePlus Editorial Team
DevicePlus Editorial Team

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • Can Arduino make USB devices? Creating devices on Arduino Pro Micro (Leonardo)Can Arduino make USB devices? Creating devices on Arduino Pro Micro (Leonardo)
  • A Smart Bike Lighting System Using ArduinoA Smart Bike Lighting System Using Arduino
  • USB Volume Control with ArduinoUSB Volume Control with Arduino
  • Using Arduino with Parts and Sensors – Solar Powered Arduino (Part 1)Using Arduino with Parts and Sensors – Solar Powered Arduino (Part 1)
  • Use Arduino to Control a Motor Part 3 – Making an RC Car Using a Servo Motor for the SteeringUse Arduino to Control a Motor Part 3 – Making an RC Car Using a Servo Motor for the Steering
  • Let’s Play with ESP-WROOM-32 on Arduino (Environment Construction Setup – LED Blinking)Let’s Play with ESP-WROOM-32 on Arduino (Environment Construction Setup – LED Blinking)
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort