Using Arduino Leonardo as a Bluetooth Keyboard (Typing Messages Sent from Phone as if with a Keyboard)

Using Arduino Leonardo as a Bluetooth Keyboard (Typing Messages Sent from Phone as if with a Keyboard)

 Hello friends, in this post I'll explain how we can use Arduino Leonardo to print messages sent from a phone via Bluetooth to a connected device, essentially creating a Bluetooth keyboard.

If you're wondering why I did this, I had a Raspberry and a Bluetooth keyboard, but unfortunately when I installed Ubuntu Desktop on the RPI4, I couldn't connect the Bluetooth keyboard directly, and since I was too lazy to set up connection settings via remote access, I thought of connecting the Arduino Leonardo via USB and then sending messages from the phone via Bluetooth and printing them. Since this seemed easier than accessing the RPI remotely, I tried it and it worked.

After completing the Ubuntu Desktop installation and being able to access the desktop, I was able to connect the keyboard smoothly thanks to a program called blueman. But until then, I managed this way. Now that we've explained why, let's get into the coding part.

Connection Diagram


Arduino Codes and Explanations

#include <Keyboard.h> // Library to Control the Keyboard

void setup() {
  Serial1.begin(38400); // We Connect to Bluetooth Module.

}
char gelenKarakter; // Variable to Hold the Character Received from the Phone

void loop() {
  BT(); // We Call the Function
 
}

void BT() {
  if (Serial1.available() > 0) { // If Data is Coming from Bluetooth
    gelenKarakter = Serial1.read(); // Set incomingCharacter to the Received Data
    Keyboard.print(gelenKarakter); // Press the Key on the Keyboard
    Keyboard.releaseAll(); // Think of it as Lifting Your Finger from the Key
  }

}

Note 1: The baud rate of the HC-05 Bluetooth module is set to 9600 when first connected, don't forget to change it to 38400!
Note 2: Make sure the device you are going to use with this system has the keyboard layout set to English, because it presses a different key for characters like "ŞÇİ" and also presses "*" instead of "-" if Turkish keyboard is selected on the system.

If you've uploaded the codes to your device without any problems up to this point, you can now download the program here to your phone, then connect your Leonardo device to the device you want to use as a keyboard, and start sending messages and typing from your phone.