ESP32 - DIYables Bluetooth App Digital Pins

Overview

The Bluetooth Digital Pins example provides remote control and monitoring of ESP32 GPIO pins through the DIYables Bluetooth STEM app. Designed for ESP32 boards with support for both BLE (Bluetooth Low Energy) and Classic Bluetooth connections. Configure pins as inputs or outputs, toggle output pins, read digital and analog input states, and receive real-time pin change notifications — perfect for home automation, relay control, button monitoring, and sensor reading.

This example supports two Bluetooth modes:

  • ESP32 BLE (Bluetooth Low Energy): Works on both Android and iOS
  • ESP32 Classic Bluetooth: Works on Android only. iOS does not support Classic Bluetooth. Use BLE if you need iOS support.
ESP32 Bluetooth Digital Pins Example - Pin Control and Monitor Interface Tutorial

Features

  • Output Pin Control: Toggle digital output pins HIGH/LOW from the app
  • Input Pin Monitoring: Read digital and analog input pin states
  • Custom Pin Names: Assign friendly names to each pin (e.g., "LED", "Btn1", "A34")
  • Real-Time Updates: Automatic notifications when input pin states change
  • Up to 16 Pins: Support for up to 16 configurable pins simultaneously
  • Mixed Modes: Combine input and output pins in the same setup
  • BLE & Classic Bluetooth: Choose the Bluetooth mode that suits your project
  • Cross-Platform: BLE mode works on both Android and iOS; Classic Bluetooth works on Android

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for ESP32
1×Recommended: Breakout Expansion Board for ESP32
1×Recommended: Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables .

ESP32 Code

Quick Instructions

Follow these instructions step by step:

  • If this is your first time using the ESP32, refer to the tutorial on setting up the environment for ESP32 in the Arduino IDE.
  • Connect the ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate ESP32 board and COM port.
  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search "DIYables Bluetooth", then find the DIYables Bluetooth library by DIYables
  • Click Install button to install the library.
ESP32 DIYables Bluetooth library
  • You will be asked for installing some other library dependencies
  • Click Install All button to install all library dependencies.
ESP32 DIYables Bluetooth dependency

Choose one of the two Bluetooth modes below depending on your needs:

ESP32 Classic Bluetooth Code (works with app on Android only)

Note: Classic Bluetooth is NOT supported on iOS. If you need iOS support, use the BLE code below.

  • On Arduino IDE, Go to File Examples DIYables Bluetooth Esp32Bluetooth_PinControl example, or copy the above code and paste it to the editor of Arduino IDE
/* * DIYables Bluetooth Library - ESP32 Classic Bluetooth Pin Control Example * Works with DIYables Bluetooth STEM app on Android * Note: Classic Bluetooth is NOT supported on iOS. Use BLE examples for iOS support. * * This example demonstrates the Bluetooth Pin Control/Monitor feature: * - Control digital output pins via Bluetooth * - Monitor digital input pins * - Monitor analog input pins * - Configure pin modes (INPUT/OUTPUT) * * Compatible Boards: * - ESP32 (all variants with Classic Bluetooth) * - ESP32-WROOM-32 * - ESP32-DevKitC * - ESP32-WROVER * * Note: Select "Huge APP (3MB No OTA/1MB SPIFFS)" partition scheme * in Arduino IDE: Tools > Partition Scheme * * Setup: * 1. Upload the sketch to your ESP32 * 2. Open Serial Monitor (115200 baud) to see connection status * 3. Use DIYables Bluetooth App to connect and control pins * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */ #include <DIYables_BluetoothServer.h> #include <DIYables_BluetoothPinControl.h> #include <platforms/DIYables_Esp32Bluetooth.h> // Create Bluetooth instances DIYables_Esp32Bluetooth bluetooth("ESP32_Pins"); DIYables_BluetoothServer bluetoothServer(bluetooth); // Create Pin Control/Monitor app instance DIYables_BluetoothPinControl bluetoothPins; // Pin configuration (adjust for your ESP32 board) const int LED_PIN = 2; // Built-in LED on most ESP32 boards const int OUTPUT_PIN_1 = 16; const int OUTPUT_PIN_2 = 17; const int INPUT_PIN_1 = 18; const int INPUT_PIN_2 = 19; const int ANALOG_PIN_1 = 34; // ESP32 ADC1 pins (input only) const int ANALOG_PIN_2 = 35; void setup() { Serial.begin(115200); delay(1000); Serial.println("DIYables Bluetooth - ESP32 Pin Control/Monitor Example"); // Initialize pins pinMode(LED_PIN, OUTPUT); pinMode(OUTPUT_PIN_1, OUTPUT); pinMode(OUTPUT_PIN_2, OUTPUT); pinMode(INPUT_PIN_1, INPUT_PULLUP); pinMode(INPUT_PIN_2, INPUT_PULLUP); // Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin(); // Add digital pins app to server bluetoothServer.addApp(&bluetoothPins); // Configure which pins are accessible via Bluetooth with custom names bluetoothPins.enablePin(LED_PIN, BT_PIN_OUTPUT, "LED"); bluetoothPins.enablePin(OUTPUT_PIN_1, BT_PIN_OUTPUT, "Out1"); bluetoothPins.enablePin(OUTPUT_PIN_2, BT_PIN_OUTPUT, "Out2"); bluetoothPins.enablePin(INPUT_PIN_1, BT_PIN_INPUT, "Btn1"); bluetoothPins.enablePin(INPUT_PIN_2, BT_PIN_INPUT, "Btn2"); bluetoothPins.enablePin(ANALOG_PIN_1, BT_PIN_INPUT, "A34"); bluetoothPins.enablePin(ANALOG_PIN_2, BT_PIN_INPUT, "A35"); // Set up connection event callbacks bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); }); // Set up callback for pin write commands bluetoothPins.onPinWrite([](int pin, int state) { digitalWrite(pin, state); Serial.print("Pin "); Serial.print(pin); Serial.print(" set to "); Serial.println(state ? "HIGH" : "LOW"); }); // Set up callback for pin read commands bluetoothPins.onPinRead([](int pin) -> int { // Read analog pins with analogRead, digital pins with digitalRead int state; if (pin == ANALOG_PIN_1 || pin == ANALOG_PIN_2) { state = analogRead(pin); Serial.print("Analog pin "); Serial.print(pin); Serial.print(" read: "); Serial.println(state); } else { state = digitalRead(pin); Serial.print("Digital pin "); Serial.print(pin); Serial.print(" read: "); Serial.println(state ? "HIGH" : "LOW"); } return state; }); // Set up callback for pin mode changes bluetoothPins.onPinModeChange([](int pin, int mode) { pinMode(pin, mode == BT_PIN_OUTPUT ? OUTPUT : INPUT_PULLUP); Serial.print("Pin "); Serial.print(pin); Serial.print(" mode changed to "); Serial.println(mode == BT_PIN_OUTPUT ? "OUTPUT" : "INPUT"); }); Serial.println("Waiting for Bluetooth connection..."); Serial.print("Enabled pins: "); Serial.println(bluetoothPins.getEnabledPinCount()); } void loop() { // Handle Bluetooth server communications bluetoothServer.loop(); // Optional: Monitor input pins and send updates static unsigned long lastInputCheck = 0; static int lastInputState1 = HIGH; static int lastInputState2 = HIGH; static int lastAnalogState1 = 0; static int lastAnalogState2 = 0; if (millis() - lastInputCheck >= 100) { lastInputCheck = millis(); // Check digital input pin 1 int currentState1 = digitalRead(INPUT_PIN_1); if (currentState1 != lastInputState1) { lastInputState1 = currentState1; bluetoothPins.updatePinState(INPUT_PIN_1, currentState1); Serial.print("Input pin "); Serial.print(INPUT_PIN_1); Serial.print(" changed to "); Serial.println(currentState1 ? "HIGH" : "LOW"); } // Check digital input pin 2 int currentState2 = digitalRead(INPUT_PIN_2); if (currentState2 != lastInputState2) { lastInputState2 = currentState2; bluetoothPins.updatePinState(INPUT_PIN_2, currentState2); Serial.print("Input pin "); Serial.print(INPUT_PIN_2); Serial.print(" changed to "); Serial.println(currentState2 ? "HIGH" : "LOW"); } // Check analog input 1 (send update if changed by more than 50 - ESP32 has 12-bit ADC) int currentAnalog1 = analogRead(ANALOG_PIN_1); if (abs(currentAnalog1 - lastAnalogState1) > 50) { lastAnalogState1 = currentAnalog1; bluetoothPins.updatePinState(ANALOG_PIN_1, currentAnalog1); Serial.print("Analog pin "); Serial.print(ANALOG_PIN_1); Serial.print(" changed to "); Serial.println(currentAnalog1); } // Check analog input 2 (send update if changed by more than 50) int currentAnalog2 = analogRead(ANALOG_PIN_2); if (abs(currentAnalog2 - lastAnalogState2) > 50) { lastAnalogState2 = currentAnalog2; bluetoothPins.updatePinState(ANALOG_PIN_2, currentAnalog2); Serial.print("Analog pin "); Serial.print(ANALOG_PIN_2); Serial.print(" changed to "); Serial.println(currentAnalog2); } } delay(10); }
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Open the Serial Monitor
  • Check out the result on Serial Monitor. It looks like the below:
COM6
Send
DIYables Bluetooth - ESP32 Pin Control/Monitor Example Waiting for Bluetooth connection... Enabled pins: 7
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

ESP32 BLE Code (works with app on both Android and iOS)

  • On Arduino IDE, Go to File Examples DIYables Bluetooth Esp32BLE_PinControl example, or copy the above code and paste it to the editor of Arduino IDE
/* * DIYables Bluetooth Library - ESP32 BLE Pin Control/Monitor Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Pin Control/Monitor feature: * - Control digital output pins via Bluetooth * - Monitor digital input pins * - Monitor analog input pins * - Configure pin modes (INPUT/OUTPUT) * * Compatible Boards: * - ESP32-WROOM-32 * - ESP32-DevKitC * - ESP32-WROVER * - ESP32-S3 * - ESP32-C3 * - Any ESP32 board supporting BLE * * Note: Select "Huge APP (3MB No OTA/1MB SPIFFS)" partition scheme * in Arduino IDE: Tools > Partition Scheme * * Setup: * 1. Upload the sketch to your ESP32 * 2. Open Serial Monitor (115200 baud) to see connection status * 3. Use DIYables Bluetooth App to connect and control pins * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */ #include <DIYables_BluetoothServer.h> #include <DIYables_BluetoothPinControl.h> #include <platforms/DIYables_Esp32BLE.h> // BLE Configuration const char* DEVICE_NAME = "ESP32BLE_Pins"; const char* SERVICE_UUID = "19B10000-E8F2-537E-4F6C-D104768A1214"; const char* TX_UUID = "19B10001-E8F2-537E-4F6C-D104768A1214"; const char* RX_UUID = "19B10002-E8F2-537E-4F6C-D104768A1214"; // Create Bluetooth instances DIYables_Esp32BLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID); DIYables_BluetoothServer bluetoothServer(bluetooth); // Create Pin Control/Monitor app instance DIYables_BluetoothPinControl bluetoothPins; // Pin configuration (ESP32 GPIOs) const int LED_PIN = 2; // Built-in LED const int OUTPUT_PIN_1 = 16; const int OUTPUT_PIN_2 = 17; const int INPUT_PIN_1 = 25; const int INPUT_PIN_2 = 26; const int ANALOG_PIN_1 = 34; // Input-only ADC pin const int ANALOG_PIN_2 = 35; // Input-only ADC pin void setup() { Serial.begin(115200); delay(1000); Serial.println("DIYables Bluetooth - ESP32 BLE Pin Control/Monitor Example"); // Initialize pins pinMode(LED_PIN, OUTPUT); pinMode(OUTPUT_PIN_1, OUTPUT); pinMode(OUTPUT_PIN_2, OUTPUT); pinMode(INPUT_PIN_1, INPUT_PULLUP); pinMode(INPUT_PIN_2, INPUT_PULLUP); // Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin(); // Add digital pins app to server bluetoothServer.addApp(&bluetoothPins); // Configure which pins are accessible via Bluetooth with custom names bluetoothPins.enablePin(LED_PIN, BT_PIN_OUTPUT, "LED"); bluetoothPins.enablePin(OUTPUT_PIN_1, BT_PIN_OUTPUT, "Out1"); bluetoothPins.enablePin(OUTPUT_PIN_2, BT_PIN_OUTPUT, "Out2"); bluetoothPins.enablePin(INPUT_PIN_1, BT_PIN_INPUT, "Btn1"); bluetoothPins.enablePin(INPUT_PIN_2, BT_PIN_INPUT, "Btn2"); bluetoothPins.enablePin(ANALOG_PIN_1, BT_PIN_INPUT, "A34"); bluetoothPins.enablePin(ANALOG_PIN_2, BT_PIN_INPUT, "A35"); // Set up connection event callbacks bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); }); // Set up callback for pin write commands bluetoothPins.onPinWrite([](int pin, int state) { digitalWrite(pin, state); Serial.print("Pin "); Serial.print(pin); Serial.print(" set to "); Serial.println(state ? "HIGH" : "LOW"); }); // Set up callback for pin read commands bluetoothPins.onPinRead([](int pin) -> int { int state; if (pin == ANALOG_PIN_1 || pin == ANALOG_PIN_2) { state = analogRead(pin); Serial.print("Analog pin "); Serial.print(pin); Serial.print(" read: "); Serial.println(state); } else { state = digitalRead(pin); Serial.print("Digital pin "); Serial.print(pin); Serial.print(" read: "); Serial.println(state ? "HIGH" : "LOW"); } return state; }); // Set up callback for pin mode changes bluetoothPins.onPinModeChange([](int pin, int mode) { pinMode(pin, mode == BT_PIN_OUTPUT ? OUTPUT : INPUT_PULLUP); Serial.print("Pin "); Serial.print(pin); Serial.print(" mode changed to "); Serial.println(mode == BT_PIN_OUTPUT ? "OUTPUT" : "INPUT"); }); Serial.println("Waiting for Bluetooth connection..."); Serial.print("Enabled pins: "); Serial.println(bluetoothPins.getEnabledPinCount()); } void loop() { bluetoothServer.loop(); static unsigned long lastInputCheck = 0; static int lastInputState1 = HIGH; static int lastInputState2 = HIGH; static int lastAnalogState1 = 0; static int lastAnalogState2 = 0; if (millis() - lastInputCheck >= 100) { lastInputCheck = millis(); int currentState1 = digitalRead(INPUT_PIN_1); if (currentState1 != lastInputState1) { lastInputState1 = currentState1; bluetoothPins.updatePinState(INPUT_PIN_1, currentState1); Serial.print("Input pin "); Serial.print(INPUT_PIN_1); Serial.print(" changed to "); Serial.println(currentState1 ? "HIGH" : "LOW"); } int currentState2 = digitalRead(INPUT_PIN_2); if (currentState2 != lastInputState2) { lastInputState2 = currentState2; bluetoothPins.updatePinState(INPUT_PIN_2, currentState2); Serial.print("Input pin "); Serial.print(INPUT_PIN_2); Serial.print(" changed to "); Serial.println(currentState2 ? "HIGH" : "LOW"); } // ESP32 has 12-bit ADC (0-4095) int currentAnalog1 = analogRead(ANALOG_PIN_1); if (abs(currentAnalog1 - lastAnalogState1) > 40) { // ~1% of 4095 lastAnalogState1 = currentAnalog1; bluetoothPins.updatePinState(ANALOG_PIN_1, currentAnalog1); Serial.print("Analog pin "); Serial.print(ANALOG_PIN_1); Serial.print(" changed to "); Serial.println(currentAnalog1); } int currentAnalog2 = analogRead(ANALOG_PIN_2); if (abs(currentAnalog2 - lastAnalogState2) > 40) { lastAnalogState2 = currentAnalog2; bluetoothPins.updatePinState(ANALOG_PIN_2, currentAnalog2); Serial.print("Analog pin "); Serial.print(ANALOG_PIN_2); Serial.print(" changed to "); Serial.println(currentAnalog2); } } delay(10); }
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Open the Serial Monitor
  • Check out the result on Serial Monitor. It looks like the below:
COM6
Send
DIYables Bluetooth - ESP32 BLE Pin Control/Monitor Example Waiting for Bluetooth connection... Enabled pins: 7
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Mobile App

  • Install the DIYables Bluetooth App on your smartphone: Android | iOS
  • If you are using the ESP32 Classic Bluetooth code, you need to pair the ESP32 with your Android phone before opening the app:
    • Go to your phone's Settings > Bluetooth
    • Make sure Bluetooth is turned on
    • Your phone will scan for available devices
    • Find and tap "ESP32_Pins" in the list of available devices
    • Confirm the pairing request (no PIN required)
    • Wait until it shows "Paired" under the device name
  • If you are using the ESP32 BLE code, no pairing is needed. Just proceed to the next step.
  • Open the DIYables Bluetooth App
  • When opening the app for the first time, it will ask for permissions. Please grant the following:
    • Nearby Devices permission (Android 12+) / Bluetooth permission (iOS) - required to scan and connect to Bluetooth devices
    • Location permission (Android 11 and below only) - required by older Android versions to scan for BLE devices
  • Make sure Bluetooth is turned on on your phone
  • On the home screen, tap the Connect button. The app will scan for both BLE and Classic Bluetooth devices.
DIYables Bluetooth App - Home Screen with Scan Button
  • Find and tap your device in the scan results to connect:
    • For Classic Bluetooth: tap "ESP32_Pins"
    • For BLE: tap "ESP32BLE_Pins"
  • Once connected, the app automatically goes back to the home screen. Select the Digital Pins app from the app menu.
DIYables Bluetooth App - Home Screen with Digital Pins App

Note: You can tap the settings icon on the home screen to hide/show apps on the home screen. For more details, see the DIYables Bluetooth App User Manual.

  • Toggle output pins by tapping on them, and watch input pins update in real time
DIYables Bluetooth App - Digital Pins Screen

Now look back at the Serial Monitor on Arduino IDE. You will see:

COM6
Send
Bluetooth connected! Pin 2 set to HIGH Pin 16 set to LOW Input pin 18 changed to LOW Analog pin 34 changed to 2048
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Toggle pins in the app and watch the real-time feedback in the Serial Monitor

Creative Customization - Adapt the Code to Your Project

Configure Pins

Enable pins with custom names and modes:

// Enable individual pins with mode and custom name bluetoothPins.enablePin(2, BT_PIN_OUTPUT, "LED"); // Built-in LED bluetoothPins.enablePin(16, BT_PIN_OUTPUT, "Relay1"); // Relay control bluetoothPins.enablePin(17, BT_PIN_OUTPUT, "Relay2"); // Relay control bluetoothPins.enablePin(18, BT_PIN_INPUT, "Button"); // Button input bluetoothPins.enablePin(34, BT_PIN_INPUT, "Sensor"); // Analog sensor // Disable a specific pin bluetoothPins.disablePin(17); // Check pin status bool isEnabled = bluetoothPins.isPinEnabled(2); int mode = bluetoothPins.getPinMode(2); int count = bluetoothPins.getEnabledPinCount();

Handle Pin Write Commands

Use the onPinWrite() callback to control hardware when the app toggles a pin:

bluetoothPins.onPinWrite([](int pin, int state) { digitalWrite(pin, state); Serial.print("Pin "); Serial.print(pin); Serial.print(" set to "); Serial.println(state ? "HIGH" : "LOW"); });

Handle Pin Read Commands

Use the onPinRead() callback to return pin states to the app:

bluetoothPins.onPinRead([](int pin) -> int { if (pin == 34 || pin == 35) { // Read analog value for ADC pins return analogRead(pin); } else { // Read digital value for other pins return digitalRead(pin); } });

Handle Pin Mode Changes

Use the onPinModeChange() callback to dynamically change pin modes:

bluetoothPins.onPinModeChange([](int pin, int mode) { pinMode(pin, mode == BT_PIN_OUTPUT ? OUTPUT : INPUT_PULLUP); Serial.print("Pin "); Serial.print(pin); Serial.print(" mode changed to "); Serial.println(mode == BT_PIN_OUTPUT ? "OUTPUT" : "INPUT"); });

Send Real-Time Pin State Updates

Push pin state changes to the app from your code:

// Detect input changes and send updates int currentState = digitalRead(18); if (currentState != lastState) { lastState = currentState; bluetoothPins.updatePinState(18, currentState); } // Send analog value updates int analogValue = analogRead(34); if (abs(analogValue - lastAnalogValue) > 50) { lastAnalogValue = analogValue; bluetoothPins.updatePinState(34, analogValue); }

Handle Connection Events

bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); // Safety: turn off all outputs when disconnected digitalWrite(2, LOW); digitalWrite(16, LOW); digitalWrite(17, LOW); });

How to Use the Digital Pins

App Interface Controls

The digital pins interface in the DIYables Bluetooth App provides:

  • Pin List: Shows all enabled pins with names and current states
  • Toggle Buttons: Tap output pins to toggle HIGH/LOW
  • Input Display: Real-time display of input pin states
  • Analog Values: Shows raw analog values for ADC pins

Pin Modes

  • BT_PIN_OUTPUT (0): Controls digital output — toggle from the app
  • BT_PIN_INPUT (1): Monitors digital or analog input — displays current state

Programming Examples

Basic Relay Control

const int RELAY_1 = 16; const int RELAY_2 = 17; const int RELAY_3 = 18; const int RELAY_4 = 19; void setup() { pinMode(RELAY_1, OUTPUT); pinMode(RELAY_2, OUTPUT); pinMode(RELAY_3, OUTPUT); pinMode(RELAY_4, OUTPUT); bluetoothPins.enablePin(RELAY_1, BT_PIN_OUTPUT, "Light"); bluetoothPins.enablePin(RELAY_2, BT_PIN_OUTPUT, "Fan"); bluetoothPins.enablePin(RELAY_3, BT_PIN_OUTPUT, "Pump"); bluetoothPins.enablePin(RELAY_4, BT_PIN_OUTPUT, "Heater"); bluetoothPins.onPinWrite([](int pin, int state) { digitalWrite(pin, state); Serial.print("Relay on pin "); Serial.print(pin); Serial.println(state ? " activated" : " deactivated"); }); }

Button and Switch Monitor

const int BUTTON_PINS[] = {18, 19, 21, 22}; const char* BUTTON_NAMES[] = {"Btn1", "Btn2", "Btn3", "Btn4"}; const int NUM_BUTTONS = 4; int lastButtonStates[4] = {HIGH, HIGH, HIGH, HIGH}; void setup() { for (int i = 0; i < NUM_BUTTONS; i++) { pinMode(BUTTON_PINS[i], INPUT_PULLUP); bluetoothPins.enablePin(BUTTON_PINS[i], BT_PIN_INPUT, BUTTON_NAMES[i]); } bluetoothPins.onPinRead([](int pin) -> int { return digitalRead(pin); }); } void loop() { bluetoothServer.loop(); // Check for button state changes for (int i = 0; i < NUM_BUTTONS; i++) { int state = digitalRead(BUTTON_PINS[i]); if (state != lastButtonStates[i]) { lastButtonStates[i] = state; bluetoothPins.updatePinState(BUTTON_PINS[i], state); } } delay(10); }

Mixed Input/Output System

void setup() { // Output pins for actuators bluetoothPins.enablePin(2, BT_PIN_OUTPUT, "LED"); bluetoothPins.enablePin(16, BT_PIN_OUTPUT, "Relay"); // Digital input pins for sensors bluetoothPins.enablePin(18, BT_PIN_INPUT, "Motion"); bluetoothPins.enablePin(19, BT_PIN_INPUT, "Door"); // Analog input pins for sensors bluetoothPins.enablePin(34, BT_PIN_INPUT, "Light"); bluetoothPins.enablePin(35, BT_PIN_INPUT, "Temp"); // Set up hardware pinMode(2, OUTPUT); pinMode(16, OUTPUT); pinMode(18, INPUT_PULLUP); pinMode(19, INPUT_PULLUP); bluetoothPins.onPinWrite([](int pin, int state) { digitalWrite(pin, state); }); bluetoothPins.onPinRead([](int pin) -> int { if (pin == 34 || pin == 35) return analogRead(pin); return digitalRead(pin); }); }

Advanced Programming Techniques

Safety Interlock

bool safetyEnabled = true; bluetoothPins.onPinWrite([](int pin, int state) { // Safety check: don't allow conflicting outputs if (pin == 16 && state == HIGH) { // Check if safety interlock allows this if (digitalRead(18) == LOW) { // Safety switch must be engaged bluetoothPins.updatePinState(16, 0); // Reject the command Serial.println("SAFETY: Operation blocked - safety switch off"); return; } } digitalWrite(pin, state); });

Timed Auto-Off

unsigned long pinTimers[16] = {0}; unsigned long PIN_TIMEOUT = 300000; // 5 minutes bluetoothPins.onPinWrite([](int pin, int state) { digitalWrite(pin, state); if (state == HIGH) { pinTimers[pin] = millis(); // Start timer } else { pinTimers[pin] = 0; // Clear timer } }); void loop() { bluetoothServer.loop(); // Check for auto-off timeouts for (int i = 0; i < 16; i++) { if (pinTimers[i] > 0 && millis() - pinTimers[i] >= PIN_TIMEOUT) { digitalWrite(i, LOW); pinTimers[i] = 0; bluetoothPins.updatePinState(i, 0); Serial.print("Auto-off: Pin "); Serial.println(i); } } delay(10); }

BLE vs Classic Bluetooth - Which to Choose?

FeatureBLE (Esp32BLE_PinControl)Classic Bluetooth (Esp32Bluetooth_PinControl)
iOS Support? Yes? No
Android Support? Yes? Yes
Power ConsumptionLowHigher
Range~30-100m~10-100m
Data RateLowerHigher
Pairing RequiredNo (auto-connect)Yes (manual pairing)
Best ForBattery-powered, cross-platformHigh throughput, Android-only

Troubleshooting

Common Issues

1. Cannot find the device in the app

  • Make sure the ESP32 is powered on and the sketch is uploaded
  • For BLE: Ensure your phone's Bluetooth and Location are enabled
  • For Classic Bluetooth: Pair the device first in phone's Bluetooth settings
  • Check that the correct partition scheme is selected (Huge APP)

2. Pins not appearing in the app

  • Ensure enablePin() is called before Bluetooth connection
  • Check getEnabledPinCount() in Serial Monitor
  • Verify pin numbers are between 0 and 15

3. Output pins not toggling

  • Verify the onPinWrite() callback calls digitalWrite()
  • Check that pins are configured as BT_PIN_OUTPUT
  • Verify hardware connections

4. Input values not updating

  • Ensure updatePinState() is called when states change
  • Check polling interval in the loop
  • For analog pins, adjust the change threshold

5. Too many pins cause errors

  • Maximum 16 pins supported (0-15)
  • Keep pin names short to avoid message truncation
  • Use shorter names if you get truncation warnings

6. Sketch too large / not enough space

  • In Arduino IDE, go to Tools > Partition Scheme and select "Huge APP (3MB No OTA/1MB SPIFFS)" or "No OTA (Large APP)"
  • The default partition scheme only provides ~1.2MB for app code, which is not enough for Bluetooth libraries
  • This setting gives ~3MB by sacrificing the OTA (over-the-air update) partition

Debug Tips

Add comprehensive debugging:

void debugPinConfig() { Serial.println("=== Pin Config Debug ==="); Serial.println("Enabled pins: " + String(bluetoothPins.getEnabledPinCount())); for (int i = 0; i < 16; i++) { if (bluetoothPins.isPinEnabled(i)) { Serial.print(" Pin " + String(i) + ": "); Serial.println(bluetoothPins.getPinMode(i) == BT_PIN_OUTPUT ? "OUTPUT" : "INPUT"); } } Serial.println("========================"); }

Project Ideas

Home Automation

  • Bluetooth light switch controller
  • Multi-room relay control panel
  • Garage door opener
  • Garden irrigation valve control

Security Projects

  • Door and window sensor monitor
  • Motion detector notification
  • Bluetooth alarm system
  • Access control panel

Industrial Projects

  • Machine start/stop control
  • Sensor array monitor
  • Valve and actuator control
  • Status indicator panel

Education Projects

  • Digital electronics learning tool
  • GPIO pin exploration
  • Input/output circuit testing
  • Breadboard project controller

Integration with Other Bluetooth Apps

Combine with Bluetooth Slider

Use digital pins for on/off and sliders for PWM intensity:

bluetoothPins.onPinWrite([](int pin, int state) { if (state == HIGH) { // When pin is on, apply slider-controlled PWM analogWrite(pin, map(currentSlider1, 0, 100, 0, 255)); } else { analogWrite(pin, 0); } });

Combine with Bluetooth Monitor

Log pin changes to the monitor:

bluetoothPins.onPinWrite([](int pin, int state) { digitalWrite(pin, state); bluetoothMonitor.send("Pin " + String(pin) + " -> " + String(state ? "ON" : "OFF")); });

Next Steps

After mastering the Bluetooth Digital Pins example, try:

  1. Bluetooth Slider - For analog-like PWM control
  2. Bluetooth Monitor - For debugging pin states
  3. Bluetooth Table - For structured pin data display
  4. Multiple Bluetooth Apps - Combining pin control with other features

Support

For additional help:

※ OUR MESSAGES