When connecting an Arduino to a computer, it normally connects as a generic serial interface device which if you’ve had much experience with Arduino before, you will know just allows for basic ASCII to be sent to and from the PC. This is very useful for basic applications like debugging or data logging, but what do you do if you want to make something like a controller? Well you make a HID of course

This will only work on certain Arduinos, the Leonardo, Micro, Zero and Due (or any 32U4 based board). This is because they have a USB module built in whereas something like the Uno or the Mega does not.

If you don’t know what a HID (Human Interface Device) is, then you will be pleased to know you probably used one to get to this page. A computer mouse, a keyboard and an Xbox remote are all HIDs and you can emulate them using an Arduino as a HID.

Connnecting

Very simply you connect your Arduino Leonardo (or HID supporting Arduino) over a USB. The Leonardo uses a micro USB-B but just make sure the cable supports data transfer, some cheaper ones might only support power.

 

Mouse and keyboard

To start with, lets load some very basic code onto the Arduino. This first code is going to be very basic and will just have the Arduino click the mouse every 3 seconds. The following code can be copy and pasted into the Arduino IDE and uploaded (don’t forget to select the correct board and port in the tools menu)

#include "Mouse.h"

void setup() {
  //begin the mouse program
  Mouse.begin();
}

void loop() {
  Mouse.press(MOUSE_LEFT); //press the left mouse button
  delay(500); //press button for half a second
  Mouse.release(MOUSE_LEFT); //release left the button
  delay(2500); //wait 2.5 seconds
}

The Arduino can however take over your mouse when this occurs so it is advised that you don’t do anything important when you test it and be ready to pull the cable!

You can also use the Arduino as a keyboard, the following code can again be copy and pasted, but this time will spell “Hello World!” every 3 seconds

#include "Keyboard.h"

void setup() {
  //initialize keyboard
  Keyboard.begin();
}

void loop() {
  Keyboard.print("Hello World!"); //print "Hello World!"
  delay(3000); //wait 3 seconds
}

This one is easy to test, just open a blank notepad file, click in it and plug the Arduino in

 

Joystick

Keyboards and mice are boring I hear you say? Well why not create a Joystick instead. This can be particularly useful if you want to make a custom flight simulator controller or maybe even a play pause button for your PC.

To do this though you will need to install the Joystick 2.0 library by found at https://github.com/MHeironimus/ArduinoJoystickLibrary/tree/version-2.0

This will need to be installed in your libraries folder found at %userprofile%DocumentsArduinolibraries if you’re using a Windows device. Once you install this, it is often a good idea to restart Arduino to make sure it picks the files up.

There are lots of examples you will now find in the Joystick folder found in File->Examples. But for a very basic tutorial to get you started, a copy paste bit of code to just press a button on and off

#include <Joystick.h>

Joystick_ Joystick;

void setup() {
  //initialize library
  Joystick.begin();
}

void loop() {
  Joystick.setButton(0, 1); //set button 0 to 1
  delay(1500); //wait 1.5 seconds
  Joystick.setButton(0, 0); //set button 0 to 0
  delay(1500); //wait 1.5 seconds
}

From here on in, it’s simply time for you to implement your own buttons and code and making whatever you dream up. I advise looking through the examples given in the Joystick library for more functions. The GitHub page is also a great place to find all of the documentation.

To check if it is working, simply go to Control Panel->devices and printers find the controller labeled Arduino Micro (or whatever arduino you are using), right click it, go to “Game controller settings” and click “Properties” and you should see button 1 flashing.

 

Like what you read? Why not show your appreciation by giving some love.

From a quick tap to smashing that love button and show how much you enjoyed this project.

Privacy

Our website uses cookies and similar technologies to provide you with a better service while searching or placing an order, for analytical purposes and to personalise our advertising. You can change your cookie settings by reading our cookie policy. Otherwise, we’ll assume you’re OK with our use of cookies.

Location

Please select an option to see content specific to your location and shop online.

Browse the US site