← Little Play Studio hello@littleplay.studio
ESP32 ROBOTICS • ANDROID APP & FIRMWARE

ESP32 BT REMOTE Car Controller

A fast, maker-friendly Android controller for 2WD & 4WD ESP32 Bluetooth Classic (SPP) robot cars using the L298N motor driver — featuring Hold-to-Drive safety, live command logs, and over-the-air GPIO port configuration.

Hold-to-Drive D-Pad

Press and hold Forward, Backward, Left, or Right to drive — releasing automatically sends Stop (S), or switch to Tap Mode anytime.

Live GPIO Port Config

Reassign MR1, MR2, ML1, and ML2 pins wirelessly from the app and save them permanently to ESP32 flash.

Safe Hardware Guardrails

Prevents duplicate GPIO pins and blocks internal flash (6-11), input-only (34-39), and boot-strap (12) pins.

Complete 38-Pin ESP32 + L298N Wiring Diagram

Download Diagram (PNG)
ESP32 BT Remote Car Complete 38-Pin Wiring Diagram
ESP32 Pin Signal L298N Pin Notes
GPIO 13 (G13)MR1IN1Right Motor Forward
GPIO 14 (G14)MR2IN2Right Motor Backward
GPIO 27 (G27)ML1IN3Left Motor Backward
GPIO 26 (G26)ML2IN4Left Motor Forward
V55V Input5VUnplug when USB-C is connected to Mac/PC
GNDGroundGNDCommon ground with battery (-)

ESP32 Arduino Code (esp32_bt_car.ino)

Supports instant driving commands and live GPIO port saving via NVS (Preferences.h).

Download .ino
// Program for Bluetooth control car using ESP32
// Little Play Studio (https://littleplaystudio.com/esp32-bt-car/)
// Companion firmware for "ESP32 BT REMOTE Car Controller" (com.lps.esp32btcar)
// Includes test logs, read-order fix, and live NVS port configuration

#include "BluetoothSerial.h"
#include <Preferences.h>

BluetoothSerial SerialBT;
Preferences prefs;

// Default L298N pins (38-pin ESP32 safe GPIOs)
const int DEFAULT_MR1 = 13;  // Right Motor Forward (IN1)
const int DEFAULT_MR2 = 14;  // Right Motor Backward (IN2)
const int DEFAULT_ML1 = 27;  // Left Motor Backward (IN3)
const int DEFAULT_ML2 = 26;  // Left Motor Forward (IN4)

int MR1 = DEFAULT_MR1;
int MR2 = DEFAULT_MR2;
int ML1 = DEFAULT_ML1;
int ML2 = DEFAULT_ML2;

bool wasConnected = false;
String cmdBuffer = "";
String btDeviceName = "LPSCAR";

// Safe output-capable GPIOs on 38-pin ESP32 (excludes flash 6-11, input-only 34-39, strapping 0/12, UART 1/3)
const int SAFE_GPIOS[] = {4, 5, 13, 14, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 32, 33};
const int SAFE_GPIO_COUNT = sizeof(SAFE_GPIOS) / sizeof(SAFE_GPIOS[0]);

bool isSafeGpio(int pin) {
  for (int i = 0; i < SAFE_GPIO_COUNT; i++) {
    if (SAFE_GPIOS[i] == pin) {
      return true;
    }
  }
  return false;
}

void logPinStates(const char *action) {
  Serial.printf("[MOTOR] %-8s | MR1(%d)=%d MR2(%d)=%d ML1(%d)=%d ML2(%d)=%d
",
                action,
                MR1, digitalRead(MR1),
                MR2, digitalRead(MR2),
                ML1, digitalRead(ML1),
                ML2, digitalRead(ML2));
}

void Forward() {
  digitalWrite(MR1, HIGH);
  digitalWrite(MR2, LOW);
  digitalWrite(ML1, LOW);
  digitalWrite(ML2, HIGH);
  logPinStates("Forward");
}

void Backward() {
  digitalWrite(MR1, LOW);
  digitalWrite(MR2, HIGH);
  digitalWrite(ML1, HIGH);
  digitalWrite(ML2, LOW);
  logPinStates("Backward");
}

void Left() {
  digitalWrite(MR1, HIGH);
  digitalWrite(MR2, LOW);
  digitalWrite(ML1, HIGH);
  digitalWrite(ML2, LOW);
  logPinStates("Left");
}

void Right() {
  digitalWrite(MR1, LOW);
  digitalWrite(MR2, HIGH);
  digitalWrite(ML1, LOW);
  digitalWrite(ML2, HIGH);
  logPinStates("Right");
}

void Stop() {
  digitalWrite(MR1, LOW);
  digitalWrite(MR2, LOW);
  digitalWrite(ML1, LOW);
  digitalWrite(ML2, LOW);
  logPinStates("Stop");
}

void applyPinModes() {
  pinMode(MR1, OUTPUT);
  pinMode(MR2, OUTPUT);
  pinMode(ML1, OUTPUT);
  pinMode(ML2, OUTPUT);
  Stop();
}

void loadPinConfig() {
  prefs.begin("lpscar", true);
  int p1 = prefs.getInt("mr1", DEFAULT_MR1);
  int p2 = prefs.getInt("mr2", DEFAULT_MR2);
  int p3 = prefs.getInt("ml1", DEFAULT_ML1);
  int p4 = prefs.getInt("ml2", DEFAULT_ML2);
  btDeviceName = prefs.getString("btName", "LPSCAR");
  prefs.end();
  btDeviceName.trim();
  if (btDeviceName.length() == 0 || btDeviceName.length() > 20) {
    btDeviceName = "LPSCAR";
  }

  if (isSafeGpio(p1) && isSafeGpio(p2) && isSafeGpio(p3) && isSafeGpio(p4) &&
      p1 != p2 && p1 != p3 && p1 != p4 && p2 != p3 && p2 != p4 && p3 != p4) {
    MR1 = p1;
    MR2 = p2;
    ML1 = p3;
    ML2 = p4;
  } else {
    MR1 = DEFAULT_MR1;
    MR2 = DEFAULT_MR2;
    ML1 = DEFAULT_ML1;
    ML2 = DEFAULT_ML2;
  }
}

void savePinConfig() {
  prefs.begin("lpscar", false);
  prefs.putInt("mr1", MR1);
  prefs.putInt("mr2", MR2);
  prefs.putInt("ml1", ML1);
  prefs.putInt("ml2", ML2);
  prefs.end();
}

void sendCurrentConfig() {
  String msg = "CFG:" + String(MR1) + "," + String(MR2) + "," + String(ML1) + "," + String(ML2) + "
";
  SerialBT.print(msg);
  Serial.print("[CFG] Sent -> " + msg);
}

bool updatePinConfig(int p1, int p2, int p3, int p4) {
  if (!isSafeGpio(p1) || !isSafeGpio(p2) || !isSafeGpio(p3) || !isSafeGpio(p4)) {
    SerialBT.println("ERR:UNSAFE_GPIO");
    Serial.println("[CFG] Rejected: unsafe GPIO");
    return false;
  }
  if (p1 == p2 || p1 == p3 || p1 == p4 || p2 == p3 || p2 == p4 || p3 == p4) {
    SerialBT.println("ERR:DUPLICATE_GPIO");
    Serial.println("[CFG] Rejected: duplicate GPIO");
    return false;
  }

  // Stop and release old pins safely before switching
  Stop();

  MR1 = p1;
  MR2 = p2;
  ML1 = p3;
  ML2 = p4;
  applyPinModes();
  savePinConfig();

  String okMsg = "OK:CFG=" + String(MR1) + "," + String(MR2) + "," + String(ML1) + "," + String(ML2) + "
";
  SerialBT.print(okMsg);
  Serial.print("[CFG] Updated -> " + okMsg);
  return true;
}

void handleLineCommand(String line) {
  line.trim();
  if (line.length() == 0) {
    return;
  }

  if (line == "GETCFG") {
    sendCurrentConfig();
    return;
  }

  if (line == "GETNAME") {
    SerialBT.println("NAME:" + btDeviceName);
    Serial.println("[BT] Sent name -> " + btDeviceName);
    return;
  }

  if (line.startsWith("BTNAME:")) {
    String newName = line.substring(7);
    newName.trim();
    if (newName.length() < 1 || newName.length() > 20) {
      SerialBT.println("ERR:INVALID_NAME");
      return;
    }
    Stop();
    prefs.begin("lpscar", false);
    prefs.putString("btName", newName);
    prefs.end();
    SerialBT.println("OK:BTNAME=" + newName);
    SerialBT.flush();
    Serial.println("[BT] Renamed to '" + newName + "'. Restarting...");
    delay(400);
    ESP.restart();
    return;
  }

  if (line == "RESETCFG") {
    updatePinConfig(DEFAULT_MR1, DEFAULT_MR2, DEFAULT_ML1, DEFAULT_ML2);
    return;
  }

  if (line.startsWith("CFG:")) {
    String payload = line.substring(4);
    int c1 = payload.indexOf(',');
    int c2 = payload.indexOf(',', c1 + 1);
    int c3 = payload.indexOf(',', c2 + 1);

    if (c1 <= 0 || c2 <= c1 + 1 || c3 <= c2 + 1) {
      SerialBT.println("ERR:BAD_FORMAT");
      Serial.println("[CFG] Rejected: bad format -> " + line);
      return;
    }

    int p1 = payload.substring(0, c1).toInt();
    int p2 = payload.substring(c1 + 1, c2).toInt();
    int p3 = payload.substring(c2 + 1, c3).toInt();
    int p4 = payload.substring(c3 + 1).toInt();
    updatePinConfig(p1, p2, p3, p4);
    return;
  }

  // If a single-letter drive command was sent with a newline (e.g. from Serial Bluetooth Terminal)
  if (line.length() == 1) {
    char c = line.charAt(0);
    if (c == 'F') Forward();
    else if (c == 'G') Backward();
    else if (c == 'L') Left();
    else if (c == 'R') Right();
    else if (c == 'S') Stop();
    else Serial.printf("[WARN] Unknown command '%c'
", c);
  } else {
    Serial.println("[WARN] Unknown line command: " + line);
  }
}

void setup() {
  Serial.begin(115200);
  delay(500);
  Serial.println("
=== ESP32 Bluetooth Car (Test Mode + Live Port Config) ===");

  loadPinConfig();
  applyPinModes();

  if (!SerialBT.begin(btDeviceName)) {
    Serial.println("[ERROR] Bluetooth initialization failed!");
    while (true) {
      delay(1000);
    }
  }

  Serial.printf("[BT] Started as '%s'. Waiting for Android connection...
", btDeviceName.c_str());
  Serial.printf("[CFG] Active pins -> MR1=%d, MR2=%d, ML1=%d, ML2=%d
", MR1, MR2, ML1, ML2);
  Serial.println("[INFO] Drive: F/G/L/R/S | Config: GETCFG, CFG:mr1,mr2,ml1,ml2, RESETCFG");
}

void loop() {
  bool isConnected = SerialBT.hasClient();
  if (isConnected && !wasConnected) {
    wasConnected = true;
    Serial.println("[BT] Client connected!");
  } else if (!isConnected && wasConnected) {
    wasConnected = false;
    cmdBuffer = "";
    Serial.println("[BT] Client disconnected! Stopping motors.");
    Stop();
  }

  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }

  while (SerialBT.available()) {
    char ch = (char)SerialBT.read();

    // If we are NOT accumulating a multi-char config line, handle instant drive commands immediately
    if (cmdBuffer.length() == 0) {
      if (ch == '
' || ch == '
') {
        continue;
      }
      if (ch == 'F') { Forward(); continue; }
      if (ch == 'G') { Backward(); continue; }
      if (ch == 'L') { Left(); continue; }
      if (ch == 'R') { Right(); continue; }
      if (ch == 'S') { Stop(); continue; }
    }

    if (ch == '
' || ch == '
') {
      if (cmdBuffer.length() > 0) {
        handleLineCommand(cmdBuffer);
        cmdBuffer = "";
      }
    } else if (cmdBuffer.length() < 64) {
      cmdBuffer += ch;
    }
  }

  delay(10);
}
VERSION 2.0 UPGRADE • OPTIONAL PWM SPEED CONTROL

Throttle Ramp (5s) & Fixed Speed Control

Remove the L298N ENA & ENB jumpers and connect G25 → ENA and G33 → ENB. Backward-compatible with v1.

ESP32 BT Remote Car v2.0 PWM Speed Wiring Diagram