Geert Roumen
Published © CC BY

ESP32 Bluetooth LE HID Keyboard

With a keyboard, you can control anything, from your phone, iPad, laptop. This will give Arduino the power it needs to control any screen.

AdvancedFull instructions provided38,845

Things used in this project

Hardware components

Geekworm EASY KIT ESP32-C1
Any ESP32 dev board would do, please note that the Arduino WiFi and Arduino BLE NANO aren't yet supported by the library
×1
Adafruit Large Arcade button white
Optional, any button or wire would do
×1
Breadboard (generic)
Breadboard (generic)
×1
Micro-USB to USB Cable (Generic)
Micro-USB to USB Cable (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Story

Read more

Schematics

Most simple setup, with breadboard button

Breadboard button, connected to pin 13 and the 3.3V

Arcade setup

This is the arcade setup where longer wires are connected to the Arcade button. Pin 13 and 3.3V are connected to the two connectors of the Arcade button

Arcade button connections

How the Arcade button is connected to the different coloured wires

Code

ESP32 to ble keyboard to phone

Arduino
This code makes the ESP32 communicate with devices using the BLE keyboard protocol
/*
@Title BLE keyboard, arcade button to phone
@Author Geert Roumen
@Institute Umeå Institute of Design
@Date 29-01-2020
 
@Hardware
 
Any ESP32 dev board would do, but I used the:
Geekworm EASY KIT ESP32-C1
https://geekworm.com/products/official-geekworm-esp32-development-board

Arcade button
Adafruit Large Arcade button white
https://www.adafruit.com/product/1192

D13       Button pin
3.3V      Connected to the other leg of the button
 
In this example sketch one single button will send a spacebar when being pressed.
*/
#include <BleKeyboard.h>
//Se the name of the bluetooth keyboard (that shows up in the bluetooth menu of your device)
BleKeyboard bleKeyboard("Arcade Phone");

const int buttonPin = 13;
//Set the old button state to be LOW/false; which means not pressed
boolean oldPinState = LOW;

void setup() {
  //Start the Serial communication (with the computer at 115200 bits per second)
  Serial.begin(115200);
  //Send this message to the computer
  Serial.println("Starting BLE work!");
  //Begin the BLE keyboard/start advertising the keyboard (so phones can find it)
  bleKeyboard.begin();
  //Make the button pin an INPUT_PULLDOWN pin, which means that it is normally LOW, untill it is pressed/ connected to the 3.3V
  pinMode(buttonPin, INPUT_PULLDOWN);
}

void loop() {

  if (bleKeyboard.isConnected()) {
    //if the keyboard is connected to a device
    if (digitalRead(buttonPin) == HIGH) {
      //If the button pin is pressed (since it is pulled down, it is pressed when it is high
      if (oldPinState == LOW) {
        //if the old Pin state was LOW and the button pin is HIGH than...
        //send the spacebar
        bleKeyboard.print(" ");
        //or you can comment this one out, and it will send the newline character
        //bleKeyboard.write(KEY_RETURN);
      }
      oldPinState = HIGH;
    } else {
      oldPinState = LOW;
    }
  }
  delay(10);
}

ESP32 Game controller

Arduino
This is code for having 5 buttons connected at the same time.
/*
@Title BLE keyboard
@Author Geert Roumen
@Institute Umeå Institute of Design
@Date 20-03-2020
 
@Hardware
 
Any ESP32 dev board would do, but I used the:
Geekworm EASY KIT ESP32-C1
https://geekworm.com/products/official-geekworm-esp32-development-board

Any buttons

D18-D23   Button pins  
GND       Connected to the other leg of the button
*/

#include <BleKeyboard.h>
//Set the name of the bluetooth keyboard (that shows up in the bluetooth menu of your device)
BleKeyboard bleKeyboard("Arcade Phone");
byte buttonPins[] =    {23,  22,   21,   19,   18};
char buttonChars[] =  {'a', 'b',  'c',  'd',  'e'};
boolean buttonStates[] = {false, false, false, false, false};
//Set the old button state to be LOW/false; which means not pressed
boolean oldPinState = LOW;

void setup() {
  
  //Start the Serial communication (with the computer at 115200 bits per second)
  Serial.begin(115200);
  //Send this message to the computer
  Serial.println("Starting BLE work!");
  //Begin the BLE keyboard/start advertising the keyboard (so phones can find it)
  bleKeyboard.begin();
  //Make the button pin an INPUT_PULLUP pin, which means that it is normally HIGH, untill it is pressed/ connected to the GND/0V
  for(int i=0;i<sizeof(buttonPins);i++){
    pinMode(buttonPins[i], INPUT_PULLUP);
    
    }
}

void loop() {

  if (bleKeyboard.isConnected()) {
    for(int i=0;i<sizeof(buttonPins);i++){
      boolean state = digitalRead(buttonPins[i]);
      if (state != buttonStates[i]){
        if (state == LOW){
          bleKeyboard.press(buttonChars[i]);
          }else{
            bleKeyboard.release(buttonChars[i]);
            }
            buttonStates[i] = state;
        }
    }
  }
}

Credits

Geert Roumen

Geert Roumen

6 projects • 10 followers
I’m a maker and interaction designer, bridging the digital and physical world. and make prototypes and do research in a playful way.

Comments