In the examples folder of the TinyUSB_Mouse_and_Keyboard library, you will find 3 sample programs. The BLE52_Mouse_and_Keyboard library also has three similar files. There are only minor differences between the two. The BLE52 version will attempt to pair your nRF52840 with your PC or other device. See the previous section on how to do the pairing. Once it is paired you should not need to pair it again and you can run on the examples with no further effort. The TinyUSB mouse example looks like this.

 * Sample program demonstrating TinyUSB_Mouse_and_Keyboard.h module which
 * auto selects between standard Arduino Mouse.h API and TinyUSB mouse API.
 * This program tests the mouse portion alone.

//Include this module whether using Arduino stack or TinyUSB stack
#include <TinyUSB_Mouse_and_Keyboard.h>

void Status (void) {  //prints out mouse button status
  Serial.print("Mouse Buttons: Left:"); Serial.print(Mouse.isPressed(MOUSE_LEFT),DEC);
  Serial.print(" Right:"); Serial.print(Mouse.isPressed(MOUSE_RIGHT),DEC);
  Serial.print(" Middle:"); Serial.println(Mouse.isPressed(MOUSE_MIDDLE),DEC);

void setup() {
  Mouse.begin();  //Unlike Arduino Mouse.h, you must use "begin" method.
  while (! Serial)delay (1);
  Serial.println("USB mouse test");;delay(1000);  //do a click
  Mouse.move(50,0);delay(1000);         //movie in all four directions
  Mouse.move(0,0,-5);delay(1000);       //scroll down and up
  Mouse.move(0,0,5);delay(1000);;delay(1000);  //hold the left button
  Mouse.move(50,0,0); delay(1000);      //drag
  Mouse.release(MOUSE_LEFT);delay(1000);//release the button;delay(1000); //try a right click
  Mouse.move(-30,-30); delay(1000);     //move away from the right-click menu that popped up;delay(1000);  //do a left click to clear the pop-up
  Status();                             //print the buttons status; delay(1000); //press each button, print status, then release all
  Status();; delay(1000);
  Status();; delay(1000);
  Mouse.release(MOUSE_ALL); delay(1000);
  Mouse.move(-30,-30); delay(1000);     //move slightly to clear the pop-up;delay(1000);  //clear of the pop-up
  Serial.println("Test completed");
void loop() {

Note that all of the code is in the setup section and there is nothing in the loop. This code will run just once each time you upload it.

Configure the Arduino IDE for your particular board and port to upload the code. If you are using an M0 or M4 based board be sure to select the Tools->USB Stack->TinyUSB option in your Arduino IDE.

If you are using USB connection on the nRF52840 or using the BLE52 version on the nRF52840 you will not have to select a different stack.

Open your serial monitor and compile and upload the code. While it is compiling, place your mouse over some of the text of the program in the Arduino IDE. During the test we will be moving the mouse around and occasionally dragging it to highlight some text. (Don't worry, we won't delete anything).

This sample program makes use of all of the methods and features of the Mouse class.

First we move the mouse 50 units in four directions with a one second delay between each movement. First 50 to the right, then down 50, left 50, and back up 50 returning to our original location. Note that X values increase to the right and Y values increase downwards.

It will move the scroll wheel up and down five units. Depending on the window size of your Arduino IDE and the amount of text you have on the screen you may or may not be able to see the scrolling action.

It does a left button press and leaves it held in place. It then moves 50 units to the right in a drag movement. It then releases the left mouse button. That should highlight whatever text was beneath it.

Next we are going to try a right click operation. That should pop up a context menu on your Arduino IDE. We then move out of the way and do another left click to clear the pop up menu. Now we will try pressing and holding all three mouse buttons and then releasing all of them. When we press the left button we get another pop-up and we clear that and the test is complete.

If you are using an M0 or M4 board you might want to try compiling the same code using the Arduino Stack. It should operate the same way using the traditional Arduino Mouse.h library.

This guide was first published on May 13, 2020. It was last updated on May 13, 2020.

This page (Mouse Example) was last updated on Apr 30, 2020.

Text editor powered by tinymce.