Back to engineering archive

Record 057 / Firmware and control

ESP32 Bluetooth keyboard controller

An ESP32 application implementing a Bluetooth keyboard control interface.

Firmware development
ESP32BLEKeyboard interface

Firmware flow

How a joystick creates left and right key presses

Selected horizontal-axis Bluetooth keyboard handling.

When Bluetooth is connected, read the joystick and ignore small horizontal movement. A larger movement sends the matching arrow key, waits 150 ms and releases the keys.

The snippet reads both axes but only shows horizontal key handling. It does not establish behaviour after disconnection or the complete keyboard mapping.

Read the implementation

loop excerpt · selected source lines 40–58. Source inspection is not a hardware test.

void loop() {
  // Ensure BLE keyboard is connected
  if (bleKeyboard.isConnected()) {
    // Read joystick values
    int joystickX = analogRead(JOYSTICK_X) - 2048; // Adjust for ESP32 ADC range
    int joystickY = analogRead(JOYSTICK_Y) - 2048;

    // Handle joystick movements
    if (abs(joystickX) > DEADZONE) {
      if (joystickX > 0) {
        bleKeyboard.press(KEY_RIGHT_ARROW); // Joystick moved RIGHT
        Serial.println("Joystick RIGHT");
      } else {
        bleKeyboard.press(KEY_LEFT_ARROW); // Joystick moved LEFT
        Serial.println("Joystick LEFT");
      }
      delay(150);
      bleKeyboard.releaseAll(); // Release after press
    }
Open the exact source-level diagram
Action · YesRead and centre the joystick values
Action · YesPress the left or right arrow keyPositive horizontal movement selects right; negative selects left.
Result · NoNo key press in this shown branch
ResultWait briefly, then release the keysThe shown delay is 150 ms.

Technical notes

Input-to-key mapping

The firmware translates local control actions into keyboard input for the host.

BLE host connection

BLE carries keyboard input between the ESP32 and its host.

Keyboard interface scope

The module provides an embedded keyboard interface for custom controls used with applications that accept keyboard input.

Engineering archiveArchive record

Project enquiries

Discuss your project.

Tell us what you’re developing and where you need engineering support.

Discuss your project