telecommande-media.ino
Consumer keys (volume, play/pause) belong to the HID keyboard: two buttons make a desktop remote. Since the 32U4 also keeps a serial port, debugging stays possible.
Hardware required
An Arduino Micro (ATmega32U4, 16 MHz, 5 V) in a breadboard-friendly format with a micro-USB connector and native USB (keyboard/mouse). 2 push buttons.
Wiring
Buttons to GND on pins 2 and 3 (internal pull-ups).
How it works
Each press edge sends a media key (KEY_VOLUME_UP, KEY_MEDIA_PLAY_PAUSE) that the OS handles globally. In the Arduino IDE, select « Arduino Micro » and the port, then Upload. On Windows a .inf driver may be needed for the COM port. Since the 32U4 is the USB itself, a bad sketch can lock out reflashing (double reset).
Going further
Add KEY_NEXT_TRACK / KEY_PREVIOUS_TRACK, or a potentiometer for analog volume.
telecommande-media.ino
// 2 boutons = media HID : volume +/- et lecture/pause.
#include <Keyboard.h>
const int B_VOL = 2, B_PLAY = 3;
bool p1 = HIGH, p2 = HIGH;
void setup() {
pinMode(B_VOL, INPUT_PULLUP);
pinMode(B_PLAY, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
bool v = digitalRead(B_VOL);
if (v == LOW && p1 == HIGH) {
Keyboard.write(KEY_VOLUME_UP);
delay(200); // anti-rebond grossier
}
p1 = v;
bool p = digitalRead(B_PLAY);
if (p == LOW && p2 == HIGH) {
Keyboard.write(KEY_MEDIA_PLAY_PAUSE);
delay(200);
}
p2 = p;
}