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 Leonardo (ATmega32U4, 16 MHz, 5 V) with native USB: the board can pose as a keyboard or a 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 Leonardo » and the port, then Upload. Caution: a sketch that sleeps too fast can « lose » the USB port; hold RESET for 12 s to wipe the bootloader.
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;
}