Originally published by Dec 12, 2019
This article was translated to English, and was originally published for deviceplus.jp.
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).
Est time:60 mins
Necessary parts
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 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.
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)
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; } |
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!
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.
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.
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); } |
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.
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!
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!
What else can you accomplish with Arduino? Explore related articles to find out: