ESP32 - DIYables Bluetooth App Digital Pins
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:
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
Or you can buy the following kits:
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 .
Follow these instructions step by step:
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.
Choose one of the two Bluetooth modes below depending on your needs:
Note: Classic Bluetooth is NOT supported on iOS. If you need iOS support, use the BLE code below.
#include <DIYables_BluetoothServer.h>
#include <DIYables_BluetoothPinControl.h>
#include <platforms/DIYables_Esp32Bluetooth.h>
DIYables_Esp32Bluetooth bluetooth("ESP32_Pins");
DIYables_BluetoothServer bluetoothServer(bluetooth);
DIYables_BluetoothPinControl bluetoothPins;
const int LED_PIN = 2;
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;
const int ANALOG_PIN_2 = 35;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("DIYables Bluetooth - ESP32 Pin Control/Monitor Example");
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);
bluetoothServer.begin();
bluetoothServer.addApp(&bluetoothPins);
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");
bluetoothServer.setOnConnected([]() {
Serial.println("Bluetooth connected!");
});
bluetoothServer.setOnDisconnected([]() {
Serial.println("Bluetooth disconnected!");
});
bluetoothPins.onPinWrite([](int pin, int state) {
digitalWrite(pin, state);
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" set to ");
Serial.println(state ? "HIGH" : "LOW");
});
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;
});
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");
}
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);
}
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);
}
DIYables Bluetooth - ESP32 Pin Control/Monitor Example
Waiting for Bluetooth connection...
Enabled pins: 7
#include <DIYables_BluetoothServer.h>
#include <DIYables_BluetoothPinControl.h>
#include <platforms/DIYables_Esp32BLE.h>
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";
DIYables_Esp32BLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID);
DIYables_BluetoothServer bluetoothServer(bluetooth);
DIYables_BluetoothPinControl bluetoothPins;
const int LED_PIN = 2;
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;
const int ANALOG_PIN_2 = 35;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("DIYables Bluetooth - ESP32 BLE Pin Control/Monitor Example");
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);
bluetoothServer.begin();
bluetoothServer.addApp(&bluetoothPins);
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");
bluetoothServer.setOnConnected([]() {
Serial.println("Bluetooth connected!");
});
bluetoothServer.setOnDisconnected([]() {
Serial.println("Bluetooth disconnected!");
});
bluetoothPins.onPinWrite([](int pin, int state) {
digitalWrite(pin, state);
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" set to ");
Serial.println(state ? "HIGH" : "LOW");
});
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;
});
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");
}
int currentAnalog1 = analogRead(ANALOG_PIN_1);
if (abs(currentAnalog1 - lastAnalogState1) > 40) {
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);
}
DIYables Bluetooth - ESP32 BLE Pin Control/Monitor Example
Waiting for Bluetooth connection...
Enabled pins: 7
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.

Find and tap your device in the scan results to connect:
Once connected, the app automatically goes back to the home screen. Select the Digital Pins app from the app menu.
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.
Now look back at the Serial Monitor on Arduino IDE. You will see:
Bluetooth connected!
Pin 2 set to HIGH
Pin 16 set to LOW
Input pin 18 changed to LOW
Analog pin 34 changed to 2048
Enable pins with custom names and modes:
bluetoothPins.enablePin(2, BT_PIN_OUTPUT, "LED");
bluetoothPins.enablePin(16, BT_PIN_OUTPUT, "Relay1");
bluetoothPins.enablePin(17, BT_PIN_OUTPUT, "Relay2");
bluetoothPins.enablePin(18, BT_PIN_INPUT, "Button");
bluetoothPins.enablePin(34, BT_PIN_INPUT, "Sensor");
bluetoothPins.disablePin(17);
bool isEnabled = bluetoothPins.isPinEnabled(2);
int mode = bluetoothPins.getPinMode(2);
int count = bluetoothPins.getEnabledPinCount();
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");
});
Use the onPinRead() callback to return pin states to the app:
bluetoothPins.onPinRead([](int pin) -> int {
if (pin == 34 || pin == 35) {
return analogRead(pin);
} else {
return digitalRead(pin);
}
});
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");
});
Push pin state changes to the app from your code:
int currentState = digitalRead(18);
if (currentState != lastState) {
lastState = currentState;
bluetoothPins.updatePinState(18, currentState);
}
int analogValue = analogRead(34);
if (abs(analogValue - lastAnalogValue) > 50) {
lastAnalogValue = analogValue;
bluetoothPins.updatePinState(34, analogValue);
}
bluetoothServer.setOnConnected([]() {
Serial.println("Bluetooth connected!");
});
bluetoothServer.setOnDisconnected([]() {
Serial.println("Bluetooth disconnected!");
digitalWrite(2, LOW);
digitalWrite(16, LOW);
digitalWrite(17, LOW);
});
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
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");
});
}
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();
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);
}
void setup() {
bluetoothPins.enablePin(2, BT_PIN_OUTPUT, "LED");
bluetoothPins.enablePin(16, BT_PIN_OUTPUT, "Relay");
bluetoothPins.enablePin(18, BT_PIN_INPUT, "Motion");
bluetoothPins.enablePin(19, BT_PIN_INPUT, "Door");
bluetoothPins.enablePin(34, BT_PIN_INPUT, "Light");
bluetoothPins.enablePin(35, BT_PIN_INPUT, "Temp");
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);
});
}
bool safetyEnabled = true;
bluetoothPins.onPinWrite([](int pin, int state) {
if (pin == 16 && state == HIGH) {
if (digitalRead(18) == LOW) {
bluetoothPins.updatePinState(16, 0);
Serial.println("SAFETY: Operation blocked - safety switch off");
return;
}
}
digitalWrite(pin, state);
});
unsigned long pinTimers[16] = {0};
unsigned long PIN_TIMEOUT = 300000;
bluetoothPins.onPinWrite([](int pin, int state) {
digitalWrite(pin, state);
if (state == HIGH) {
pinTimers[pin] = millis();
} else {
pinTimers[pin] = 0;
}
});
void loop() {
bluetoothServer.loop();
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);
}
| Feature | BLE (Esp32BLE_PinControl) | Classic Bluetooth (Esp32Bluetooth_PinControl) |
| iOS Support | ? Yes | ? No |
| Android Support | ? Yes | ? Yes |
| Power Consumption | Low | Higher |
| Range | ~30-100m | ~10-100m |
| Data Rate | Lower | Higher |
| Pairing Required | No (auto-connect) | Yes (manual pairing) |
| Best For | Battery-powered, cross-platform | High throughput, Android-only |
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
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("========================");
}
Bluetooth light switch controller
Multi-room relay control panel
Garage door opener
Garden irrigation valve control
Digital electronics learning tool
GPIO pin exploration
Input/output circuit testing
Breadboard project controller
Use digital pins for on/off and sliders for PWM intensity:
bluetoothPins.onPinWrite([](int pin, int state) {
if (state == HIGH) {
analogWrite(pin, map(currentSlider1, 0, 100, 0, 255));
} else {
analogWrite(pin, 0);
}
});
Log pin changes to the monitor:
bluetoothPins.onPinWrite([](int pin, int state) {
digitalWrite(pin, state);
bluetoothMonitor.send("Pin " + String(pin) + " -> " + String(state ? "ON" : "OFF"));
});
After mastering the Bluetooth Digital Pins example, try:
Bluetooth Slider - For analog-like PWM control
Bluetooth Monitor - For debugging pin states
Bluetooth Table - For structured pin data display
Multiple Bluetooth Apps - Combining pin control with other features