Special note about Arduino Media Keys
This is not my original article. This was on https://uboopenfactory.univ-brest.fr/Les-Labs/MusicLab/Projets/Arduino-Media-Keys but has not 404'ed into the sunset. Instead of loss of a valuable resource or sending you off to the wayback machine. We're going to repost the article word-for-word here. We make ZERO money doing this.
Arduino media keys.
In this project, an Arduino Micro is used as a USB-HID (Human Interface Device). The same standard is used for computer devices that take input from humans and/or gives output to humans, such as a computer keyboard, mouse or joystick.
OVERVIEW.
In this project, an Arduino Micro is used as a USB-HID (Human Interface Device). The same standard is used for computer devices that take input from humans and/or gives output to humans, such as a computer keyboard, mouse or joystick. More precisely, the device using the standard needs to identify itself using one of the available groups of devices, e.g. Generic Desktop, Simulation Control, VR Control, Game Control, Keyboard, etc. Groups with their corresponding functions and control codes can be found here:
http://www.freebsddiary.org/APC/usb_hid_usages
In this project we will use the group Consumer, which enables us to control the computers media player functions (e.g. Play/Pause, Next song, etc.) and global volume control.
The finished device can be used on Windows, Linux, Mac and Android systems or any other device that supports USB-HID.
(Disclaimer: for now the device was tested only on a Mac Pro.)
SCHEMATIC.
In the below example you can see how to connect a simple push button to an Arduino pin.
In this case one pin of the push button is connected to the GND pin of the Arduino via a black wire. The second pin of the push button is connected to the digital pin number 2 of the Arduino via an orange wire. Programmatically, we will define the digital pins as digital inputs with the enabled internal pull-up resistor. Therefore the pins are normally HIGH, except when the button is pressed, (connected to the ground) and the digital input is read as LOW.
The 6 push button for this project will be connected to the Arduino in the same way, each to a different digital pin on the Arduino.
The button connections to the digital pins are as follows:
- Play = pin 2
- Forward = pin 3
- Back = pin 4
- Volume Up = pin 5
- Volume Down = pin 6
- Volume Mute = pin 7
The GND pins can be daisy chained.
ARDUINO SKETCH
HID LIBRARY.
- Menu→Sketch→Include Library→Manage Libraries
- Search: "HID"
- Select: HID-Project by NicoHood Version 2.5.0 or newer
- Install
SKETCH
/* Arduino Media Keys Version 1.0 9/11/2018 Author: Tomo Murovec UBO Open Factory https://uboopenfactory.univ-brest.fr Brest, France Dependencies: HID-Project by NicoHood Version 2.5.0 or above - can be found in Arduino Lirary Manager - or: https://github.com/NicoHood/HID */ // include the HID library #include "HID-Project.h" // definitions for each pin used const int pinLed = LED_BUILTIN; const int playButton = 2; const int fwdButton = 3; const int backButton = 4; const int volUpButton = 5; const int volDwnButton = 6; const int muteButton = 7; void setup() { // define the pin mode for each pin used pinMode(pinLed, OUTPUT); pinMode(playButton, INPUT_PULLUP); pinMode(fwdButton, INPUT_PULLUP); pinMode(backButton, INPUT_PULLUP); pinMode(volUpButton, INPUT_PULLUP); pinMode(volDwnButton, INPUT_PULLUP); pinMode(muteButton, INPUT_PULLUP); // begin HID connection Consumer.begin(); } void loop() { // if the play button is pressed if (!digitalRead(playButton)) { digitalWrite(pinLed, HIGH); // turn on LED Consumer.write(MEDIA_PLAY_PAUSE); // send HID command delay(500); // wait digitalWrite(pinLed, LOW); // turn off LED } // all the buttons follow the same pattern ... if (!digitalRead(fwdButton)) { digitalWrite(pinLed, HIGH); Consumer.write(MEDIA_NEXT); delay(500); digitalWrite(pinLed, LOW); } if (!digitalRead(backButton)) { digitalWrite(pinLed, HIGH); Consumer.write(MEDIA_PREVIOUS); delay(500); digitalWrite(pinLed, LOW); } if (!digitalRead(volUpButton)) { digitalWrite(pinLed, HIGH); Consumer.write(MEDIA_VOLUME_UP); delay(500); digitalWrite(pinLed, LOW); } if (!digitalRead(volDwnButton)) { digitalWrite(pinLed, HIGH); Consumer.write(MEDIA_VOLUME_DOWN); delay(500); digitalWrite(pinLed, LOW); } if (!digitalRead(muteButton)) { digitalWrite(pinLed, HIGH); Consumer.write(MEDIA_VOLUME_MUTE); delay(500); digitalWrite(pinLed, LOW); } }
References
Some interesting links:
https://www.instructables.com/id/Digispark-Volume-Control/
https://www.stefanjones.ca/blog/arduino-leonardo-remote-multimedia-keys/
https://arduino.stackexchange.com/questions/8934/send-keyboard-media-keys-with-keyboard-library
https://www.instructables.com/id/USB-Volume-Control-and-Caps-Lock-LED-Simple-Cheap-/
https://github.com/NicoHood/HID
Appendix
Example: Volume Up Button
Taken from the HID-Project arduino library.
/* Copyright (c) 2014-2015 NicoHood See the readme for credit to other people. Consumer example Press a button to play/pause music player You may also use SingleConsumer to use a single report. See HID Project documentation for more Consumer keys. https://github.com/NicoHood/HID/wiki/Consumer-API */ #include "HID-Project.h" const int pinLed = LED_BUILTIN; const int pinButton = 2; void setup() { pinMode(pinLed, OUTPUT); pinMode(pinButton, INPUT_PULLUP); // Sends a clean report to the host. This is important on any Arduino type. Consumer.begin(); } void loop() { if (!digitalRead(pinButton)) { digitalWrite(pinLed, HIGH); // See HID Project documentation for more Consumer keys Consumer.write(MEDIA_PLAY_PAUSE); // Simple debounce delay(300); digitalWrite(pinLed, LOW); } }
Some media key definitions from the HID-Project arduino library.
Additional commands can be easily added using the same pattern (#define) and HID codes found in the next section.
// Media key definitions, see official USB docs for more #define MEDIA_FAST_FORWARD 0xB3 #define MEDIA_REWIND 0xB4 #define MEDIA_NEXT 0xB5 #define MEDIA_PREVIOUS 0xB6 #define MEDIA_STOP 0xB7 #define MEDIA_PLAY_PAUSE 0xCD #define MEDIA_VOLUME_MUTE 0xE2 #define MEDIA_VOLUME_UP 0xE9 #define MEDIA_VOLUME_DOWN 0xEA #define CONSUMER_EMAIL_READER 0x18A #define CONSUMER_CALCULATOR 0x192 #define CONSUMER_EXPLORER 0x194 #define CONSUMER_BROWSER_HOME 0x223 #define CONSUMER_BROWSER_BACK 0x224 #define CONSUMER_BROWSER_FORWARD 0x225 #define CONSUMER_BROWSER_REFRESH 0x227 #define CONSUMER_BROWSER_BOOKMARKS 0x22A
Consumer HID codes
from: http://www.freebsddiary.org/APC/usb_hid_usages.php
12 Consumer 0x00 Unassigned 0x01 Consumer Control 0x02 Numeric Key Pad 0x03 Programmable Buttons 0x20 +10 0x21 +100 0x22 AM/PM 0x30 Power 0x31 Reset 0x32 Sleep 0x33 Sleep After 0x34 Sleep Mode 0x35 Illumination 0x36 Function Buttons 0x40 Menu 0x41 Menu Pick 0x42 Menu Up 0x43 Menu Down 0x44 Menu Left 0x45 Menu Right 0x46 Menu Escape 0x47 Menu Value Increase 0x48 Menu Value Decrease 0x60 Data On Screen 0x61 Closed Caption 0x62 Closed Caption Select 0x63 VCR/TV 0x64 Broadcast Mode 0x65 Snapshot 0x66 Still 0x80 Selection 0x81 Assign Selection 0x82 Mode Step 0x83 Recall Last 0x84 Enter Channel 0x85 Order Movie 0x86 Channel 0x87 Media Selection 0x88 Media Select Computer 0x89 Media Select TV 0x8A Media Select WWW 0x8B Media Select DVD 0x8C Media Select Telephone 0x8D Media Select Program Guide 0x8E Media Select Video Phone 0x8F Media Select Games 0x90 Media Select Messages 0x91 Media Select CD 0x92 Media Select VCR 0x93 Media Select Tuner 0x94 Quit 0x95 Help 0x96 Media Select Tape 0x97 Media Select Cable 0x98 Media Select Satellite 0x99 Media Select Security 0x9A Media Select Home 0x9B Media Select Call 0x9C Channel Increment 0x9D Channel Decrement 0x9E Media Select SAP 0xA0 VCR Plus 0xA1 Once 0xA2 Daily 0xA3 Weekly 0xA4 Monthly 0xB0 Play 0xB1 Pause 0xB2 Record 0xB3 Fast Forward 0xB4 Rewind 0xB5 Scan Next Track 0xB6 Scan Previous Track 0xB7 Stop 0xB8 Eject 0xB9 Random Play 0xBA Select DisC 0xBB Enter Disc 0xBC Repeat 0xBD Tracking 0xBE Track Normal 0xBF Slow Tracking 0xC0 Frame Forward 0xC1 Frame Back 0xC2 Mark 0xC3 Clear Mark 0xC4 Repeat From Mark 0xC5 Return To Mark 0xC6 Search Mark Forward 0xC7 Search Mark Backwards 0xC8 Counter Reset 0xC9 Show Counter 0xCA Tracking Increment 0xCB Tracking Decrement 0xE0 Volume 0xE1 Balance 0xE2 Mute 0xE3 Bass 0xE4 Treble 0xE5 Bass Boost 0xE6 Surround Mode 0xE7 Loudness 0xE8 MPX 0xE9 Volume Up 0xEA Volume Down 0xF0 Speed Select 0xF1 Playback Speed 0xF2 Standard Play 0xF3 Long Play 0xF4 Extended Play 0xF5 Slow 0x100 Fan Enable 0x101 Fan Speed 0x102 Light 0x103 Light Illumination Level 0x104 Climate Control Enable 0x105 Room Temperature 0x106 Security Enable 0x107 Fire Alarm 0x108 Police Alarm 0x150 Balance Right 0x151 Balance Left 0x152 Bass Increment 0x153 Bass Decrement 0x154 Treble Increment 0x155 Treble Decrement 0x160 Speaker System 0x161 Channel Left 0x162 Channel Right 0x163 Channel Center 0x164 Channel Front 0x165 Channel Center Front 0x166 Channel Side 0x167 Channel Surround 0x168 Channel Low Frequency Enhancement 0x169 Channel Top 0x16A Channel Unknown 0x170 Sub-channel 0x171 Sub-channel Increment 0x172 Sub-channel Decrement 0x173 Alternate Audio Increment 0x174 Alternate Audio Decrement 0x180 Application Launch Buttons 0x181 AL Launch Button Configuration Tool 0x182 AL Programmable Button Configuration 0x183 AL Consumer Control Configuration 0x184 AL Word Processor 0x185 AL Text Editor 0x186 AL Spreadsheet 0x187 AL Graphics Editor 0x188 AL Presentation App 0x189 AL Database App 0x18A AL Email Reader 0x18B AL Newsreader 0x18C AL Voicemail 0x18D AL Contacts/Address Book 0x18E AL Calendar/Schedule 0x18F AL Task/Project Manager 0x190 AL Log/Journal/Timecard 0x191 AL Checkbook/Finance 0x192 AL Calculator 0x193 AL A/V Capture/Playback 0x194 AL Local Machine Browser 0x195 AL LAN/WAN Browser 0x196 AL Internet Browser 0x197 AL Remote Networking/ISP Connect 0x198 AL Network Conference 0x199 AL Network Chat 0x19A AL Telephony/Dialer 0x19B AL Logon 0x19C AL Logoff 0x19D AL Logon/Logoff 0x19E AL Terminal Lock/Screensaver 0x19F AL Control Panel 0x1A0 AL Command Line Processor/Run 0x1A1 AL Process/Task Manager 0x1A2 AL Select Tast/Application 0x1A3 AL Next Task/Application 0x1A4 AL Previous Task/Application 0x1A5 AL Preemptive Halt Task/Application 0x200 Generic GUI Application Controls 0x201 AC New 0x202 AC Open 0x203 AC Close 0x204 AC Exit 0x205 AC Maximize 0x206 AC Minimize 0x207 AC Save 0x208 AC Print 0x209 AC Properties 0x21A AC Undo 0x21B AC Copy 0x21C AC Cut 0x21D AC Paste 0x21E AC Select All 0x21F AC Find 0x220 AC Find and Replace 0x221 AC Search 0x222 AC Go To 0x223 AC Home 0x224 AC Back 0x225 AC Forward 0x226 AC Stop 0x227 AC Refresh 0x228 AC Previous Link 0x229 AC Next Link 0x22A AC Bookmarks 0x22B AC History 0x22C AC Subscriptions 0x22D AC Zoom In 0x22E AC Zoom Out 0x22F AC Zoom 0x230 AC Full Screen View 0x231 AC Normal View 0x232 AC View Toggle 0x233 AC Scroll Up 0x234 AC Scroll Down 0x235 AC Scroll 0x236 AC Pan Left 0x237 AC Pan Right 0x238 AC Pan 0x239 AC New Window 0x23A AC Tile Horizontally 0x23B AC Tile Vertically 0x23C AC Format