T-vK/ESP32-BLE-Mouse
Use Git or checkout with SVN using the web URL.
Work fast with our official CLI. Learn more.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
ESP32 BLE Mouse library
This library allows you to make the ESP32 act as a Bluetooth Mouse and control what it does. E.g. move the mouse, scroll, make a click etc.
You might also be interested in:
- Left click
- Right click
- Middle click
- Back/Forwards click
- Move mouse pointer left/right
- Move mouse pointer up/down
- Scroll up/down
- Scroll left/right
- Report optional battery level to host (basically works, but it doesn’t show up in Android’s status bar)
- Customize Bluetooth device name/manufacturer
- Compatible with Android
- Compatible with Windows
- Compatible with Linux
- Compatible with MacOS X (not stable, some people have issues, doesn’t work with old devices)
- Compatible with iOS (not stable, some people have issues, doesn’t work with old devices)
- (Make sure you can use the ESP32 with the Arduino IDE. Instructions can be found here.)
- Download the latest release of this library from the release page.
- In the Arduino IDE go to «Sketch» -> «Include Library» -> «Add .ZIP Library. » and select the file you just downloaded.
- You can now go to «File» -> «Examples» -> «ESP32 BLE Mouse» and select any of the examples to get started.
The BleMouse interface is almost identical to the Mouse Interface, so you can use documentation right here: https://www.arduino.cc/reference/en/language/functions/usb/mouse/
Just remember that you have to use bleMouse instead of just Mouse and you need these two lines at the top of your script:
This library supports a few additional features that the Mouse library does not support at the time of writing:
- Scrolling left/right E.g.: bleMouse.move(0,0,0,1) (Scroll left) and bleMouse.move(0,0,0,-1) (Scroll right)
- Using the back and forward buttons E.g.: bleMouse.click(MOUSE_BACK) and bleMouse.click(MOUSE_FORWARD)
There is also Bluetooth specific information that you can use (optional):
Instead of BleMouse bleMouse; you can do BleMouse bleMouse(«Bluetooth Device Name», «Bluetooth Device Manufacturer», 100); . The third parameter is the initial battery level of your device. To adjust the battery level later on you can simply call e.g. bleMouse.setBatteryLevel(50) (set battery level to 50%). By default the battery level will be set to 100%, the device name will be ESP32 Bluetooth Mouse and the manufacturer will be Espressif .
Credits to chegewara as this library is based on this piece of code that he provided.
Mouse and Keyboard Control Using TinyUSB and BLE
Overview
New Subscription
Please sign in to subscribe to this guide.
You will be redirected back to this guide once you sign in, and can then subscribe to this guide.
With the introduction of the Arduino Leonardo and other ATMega32u4 based boards, Arduino introduced 3 new libraries HID.h, Mouse.h and Keyboard.h which allowed you to emulate a mouse or keyboard connected by USB to your computer. HID stands for «Human Interface Device» and refers to not only mouse and keyboard but other devices such as touchpads and game controllers. This opened up a world of possibilities especially for assistive technology applications for the disabled who need alternative ways to operate a computer.
This capability was extended when boards featuring the M0 (SAMD21 based) and M4 (SAMD51 based) systems became available. They were also able to perform mouse and keyboard emulation using the Arduino API originally developed for the Leonardo.
Newer boards and microprocessors have migrated to a new type of USB interface called TinyUSB. It is likely that most future boards will take advantage benefits of the TinyUSB platform. Additionally many new boards provide Bluetooth BLE capability that can emulate Bluetooth mouse and keyboard devices. Both TinyUSB and Adafruit Bluefruit libraries have powerful capabilities for emulating mouse and keyboard. However these APIs are not fully compatible with the old traditional Arduino Mouse.h and Keyboard.h APIs. Lots of legacy code has been written using these older Arduino APIs.
In this tutorial, 2 new libraries are presented which convert the traditional Arduino APIs into calls to the newer TinyUSB and Bluefruit commands. Although TinyUSB and Bluefruit HID interfaces provide capability not available in the traditional Arduino Mouse.h and Keyboard.h interfaces, these new conversion libraries will let you run legacy code and will be a stepping stone for new users to become familiar with mouse and keyboard emulation using a simpler interface. Using these simpler libraries does not require as much direct knowledge of USB and BLE protocols.
In the next section we will take a brief look at the traditional Arduino mouse and keyboard classes.
The parts may be any microcontroller compatible with TinyUSB. For Bluetooth use, this currently requires a nRF52840 microcontroller-based board. The following board is representable of the board that will work. See the TinyUSB GitHub repo for compatible microcontrollers.
Using Arduino Leonardo As Mouse and Keyboard, Controll by Bluetooth.
Introduction: Using Arduino Leonardo As Mouse and Keyboard, Controll by Bluetooth.
One awsome feature in Arduino Leonardo, which make it special board, is that it can act as mouse and keyboard on computer. this could be usefull to controll the computer by different way, such as joy stick and here BLUETOOTH!
things used here are :
*Arduino Leonardo board
*HC-05 bluetooth adabter ( that i got from genotronex store — jordan)
In this project we gonna controll the computer mouse pointer and keyboard input from the arduino leonardo board.
this instructable is availble in arabic in genotronex blog.
Step 1: Connect the Bluetooth
the bluetooth adabter HC 05 use the serial comunication to connect to the arduino, so we gonna use #Software Serial library to connect to it.
the software Serial is much like the Arduino hardware Serial(that it done by UART) but here it done by software method.
as I defined the Software Serial Tx and Rx pins to 8 and 9
the bluetooth sould be connected :
bluetooth Tx to arduino Rx pin (pin9)
bluetooth Rx to arduino Tx pin(pin8)
as shown in the image.
to learn about the arduino Software Serial library you should refere to the Arduino Reference
Step 2: Leonardo As a Mouse
making leonardo appear as a mouse to the computer is much easy.
there is a Build-in library in the arduino, this library used with leonardo, DUE, and micro boards, and it so easy to work with.
to learn about it you should refer to Arduino Reference.
to work with bluetooth you need to other teminal, I used my android device to controll the arduino.
there is a good app in the store called Arduino Bluetooth Controller. this app connect to the HC 05 and send ‘char’ data to the arduino, and you can modify the char sent by each button on the app with «Set command».
Attachments
Step 3: Leonardo As Keyboard
The same things done to make arduino appear as a mouse is done here, but you set it to act as a keyboard.
and here you should use a different app, one that send ‘strings’ not only some ‘char’. I used an app called Bluetooth Terminal.
here I sent words from my android to arduino to be printed on the text editor.
this is all I have here.
Attachments
Be the First to Share
Did you make this project? Share it with us!
Recommendations
Back to School: Student Design Challenge
Cheese Challenge
Plywood Contest
6 Comments
Thanks!! Your tutorial was really easy to understand even for a complete beginner with no Programming background person like me. I have few questions.
Recently I started making a Keyboard Encoder to use as a game controller for Arcade Mame and PC games using Arduino Leonardo. I used all the 20 digital keys for my 4 directional + 16 button controller. I tested it with my Android tv using USB port as well and it works really well.
I want to turn my controller to be used both as USB or bluetooth. I saw your tutorial and it looks like what I need.
I am currently using both my Tx and Rx as digital key for 2 buttons.
Is there a way that I can use Rx and Tx pin for buttons as well as for bluetooth just you mentioned above? And if I turn my controller to bluetooth, will I be able to use it as USB controller when I want?
If I connect the bluetooth controller to PC, will it be auto detect and can communicate with PC just like we connect Android phone to windows?
My TV is an android smart TV but he has no Bluetooth. So I will be using a USB dongle on Tv to make it Bluetooth capable? With your tutorial, will it connect to android Tv(Tv will be using Usb bluetooth dongle)?
Guide me in this regard as I am a complete beginner with no electronics background :)
DIY Gesture Controlled Arduino Based Air Mouse using Accelerometer
Ever wondered how our world is moving towards the immersive reality. We are continuously finding new ways and methods to interact with our surrounding using virtual reality, mixed reality, augmented reality etc. New devices are coming out every day with these fast pacing technologies to impress us by their new interactive technologies.
These immersive technologies are used in gaming, interactive activities, entertainment and many other applications. In this tutorial, we are going to know about such an interactive method which gives you a new way to interact with your system instead of using a boring mouse. Our gaming geeks must be knowing that few years back Nintendo a gaming company sells out an idea of a 3D interactive method to interact with their consoles with the help of a handheld controller known as Wii controller. It uses the accelerometer to locate your gestures for a game and send it to the system wirelessly. If you want to know more about this technology you can check out their patent EP1854518B1, this will give you a complete idea of how this technology works.
Inspired by this idea we are going to make an «Air mouse», to interact with systems just by moving the console in the air, but instead of using 3-dimensional coordinate references, we are only going to use 2-dimensional coordinate references so we can imitate the actions of the computer mouse since the mouse works in two dimensions X and Y.
The concept behind this Wireless 3D Air Mouse is very simple, we will use an accelerometer to get the value of the acceleration of the actions and motions of the “Air mouse” along the x and y-axis, and then based on the values of the accelerometer we will control the mouse cursor and perform certain actions with the help of the python software drivers running on computer.
Pre-requisites
- Arduino Nano (any model)
- Accelerometer ADXL335 Module
- Bluetooth HC-05 Module
- Push buttons
- Python Installed computer
To learn more about installing python in computer follow the previous tutorial on Arduino-Python LED Controlling.
Circuit Diagram
To control your computer with the movements of your hand you need an accelerometer that gives out the acceleration along the X and Y-axis and to make the whole system wireless a Bluetooth module is used to transfer the signal wirelessly to your system.
Here an ADXL335 accelerometer is used, it’s a MEMS-based triple axis module outputting the acceleration along X, Y, and Z-axis but as told previously for controlling the mouse we would be only needing the acceleration only along the X and Y-axis. Learn more about using ADXL335 accelerometer with Arduino with our previous projects:
Here the Xout and Yout pin of accelerometer are connected to the Analog, A0, and A1 pins of Arduino and for transmitting the signals from the Arduino to the system Bluetooth module HC-05 is used here, since the Bluetooth works over the Tx and Rx pin connections, so we use software serial pins D2 and D3. It is connected using Software serial because if we connect the Bluetooth with hardware serial and start getting the readings over the python console it would show errors for the mismatch baud rate as the Bluetooth would be communicating with the python on its own baud rate. Learn more about using Bluetooth module by going through various Bluetooth based projects using different microcontrollers including Arduino.
Here we have used three push buttons — one for triggering the Air mouse, and other two for left and right click as shown in the below image:
Process Flow for the Air Mouse
The flow chart shows the process flow of Arduino based Air Mouse:
1. The system continuously checks for the mechanical trigger to be pressed until it is not pressed we can work out normally with the computers mouse.
2. When the system detects button press, the control for the mouse transferred to the air mouse.
3. As the trigger button is pressed the system starts to transfer the readings of the mouse to the computer. The system reading consists of the accelerometer readings, and the readings for the left and right click.
4. The system readings consist of the data stream of 1 byte or 8 bits, in which the first three bits consist of the X-coordinates, second three bits consist of the Y-coordinates, the second last bit is the status bit for getting the status of the left click of the mouse and the last bit is the status bit for getting the status of the right click.
5. The value of the first three bits i.e. X-coordinate can range from 100
const int rxpin = 2, txpin = 3;
SoftwareSerial bluetooth(rxpin, txpin);
const int x=A0;
const int y=A1;
int xh, yh;
int xcord, ycord;
const int trigger = 5;
int lstate = 0;
int rstate = 0;
const int lclick = 6;
const int rclick = 7;
const int led = 8;
void setup()
<
pinMode(x,INPUT);
pinMode(y,INPUT);
pinMode(trigger,INPUT_PULLUP);
pinMode(lclick,INPUT);
pinMode(rclick,INPUT);
pinMode(led, OUTPUT);
digitalWrite(lclick,HIGH);
digitalWrite(rclick,HIGH);
Serial.begin(9600);
bluetooth.begin(9600);
>
void loop()
<
digitalWrite(led,LOW);
while(digitalRead(trigger)==LOW)
<
digitalWrite(led, HIGH);
lstate = digitalRead(lclick);
rstate = digitalRead(rclick);
xh=analogRead(x);
yh=analogRead(y);
xcord=map(xh,286,429,100,999);
ycord=map(yh,282,427,100,800);
Serial.print(xcord);
Serial.print(ycord);
if (lstate == LOW)
Serial.print(1);
else
Serial.print(0);
if (rstate == LOW)
Serial.print(1);
else
Serial.print(0);
bluetooth.print(xcord);
bluetooth.print(ycord);
if (lstate == LOW)
bluetooth.print(1);
else
bluetooth.print(0);
if (rstate == LOW)
bluetooth.print(1);
else
bluetooth.print(0);
delay(4000);
>
>
Python driver script
import serial
import pyautogui
ser=serial.Serial(‘com3’,9600)
while 1:
k=ser.read(8)
cursor=k[:6]
click=k[6:]
x=cursor[:3]
y=cursor[3:]
l=click[0]
r=click[1]
xcor=int(x.decode(‘utf-8’))
ycor=int(y.decode(‘utf-8’))
pyautogui.moveTo(xcor,ycor)
if l==49:
pyautogui.click(clicks=2)
elif r==49:
pyautogui.click(button=’right’, clicks=2)