bouton-clic-souris.ino

Arduino Micro · File : bouton-clic-souris.ino · Language : Arduino · Use case : Un bouton physique qui fait un clic de souris gauche

The button acts like a mouse micro-switch: Mouse.click sends the left-click press/release. Handy for a presentation clicker or a physical panic button.

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). 1 push button.

Wiring

Button between pin 2 and GND (internal pull-up).

How it works

The two-state machine prevents double clicks; Mouse.click(MOUSE_LEFT) emits the full HID event. 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

Right click with MOUSE_RIGHT; wheel scroll with Mouse.move(0, 0, 1).

bouton-clic-souris.ino

// Bouton sur la broche 2 = clic gauche HID.
#include <Mouse.h>

const int BOUTON = 2;
bool enfonce = false;

void setup() {
  pinMode(BOUTON, INPUT_PULLUP);
  Mouse.begin();
}

void loop() {
  bool appui = digitalRead(BOUTON) == LOW;
  if (appui && !enfonce) {
    Mouse.click(MOUSE_LEFT);
    enfonce = true;
  } else if (!appui && enfonce) {
    enfonce = false;
  }
}

← Back to the board