Arduino UNO R4 WiFi Bluetooth Digitale Pinnen Voorbeeld - GPIO Pin Bediening via BLE Tutorial

Overzicht

Het Bluetooth Digitale Pinnen voorbeeld biedt externe GPIO pin bediening en monitoring toegankelijk via de DIYables Bluetooth STEM app. Ontworpen voor Arduino UNO R4 WiFi met BLE (Bluetooth Low Energy) om output pinnen te bedienen en input pinnen draadloos te monitoren vanaf uw smartphone. Perfect voor relais bediening, knop monitoring, LED schakeling, en elke toepassing die externe pin toegang vereist.

Let op: De Arduino UNO R4 WiFi ondersteunt alleen BLE (Bluetooth Low Energy). Het ondersteunt geen Classic Bluetooth. De DIYables Bluetooth App ondersteunt zowel BLE als Classic Bluetooth op Android, en BLE op iOS. Omdat dit board BLE gebruikt, werkt de app op zowel Android als iOS.

Arduino UNO R4 WiFi Bluetooth Digitale Pinnen Voorbeeld - GPIO Pin Bediening via BLE Tutorial

Functies

  • Output Bediening: Stel digitale pinnen HIGH/LOW extern in
  • Input Monitoring: Lees digitale en analoge pin toestanden
  • Benoemde Pinnen: Wijs vriendelijke namen toe aan elke pin (bijv. "LED", "Relais")
  • Real-Time Updates: Push pin toestandsveranderingen naar de app
  • Tot 16 Pinnen: Bedien meerdere pinnen tegelijkertijd
  • Werkt op Android & iOS: BLE wordt ondersteund op beide platformen
  • Geen Koppeling Vereist: BLE verbindt automatisch zonder handmatige koppeling

Arduino UNO R4 WiFi Code

Snelle Stappen

Volg deze instructies stap voor stap:

  • Nieuw bij Arduino UNO R4 WiFi? Begin met onze Arduino UNO R4 WiFi aan de slag gids om eerst de basis te leren.
  • Verbind het Arduino UNO R4 WiFi board met uw computer via een USB-kabel.
  • Start de Arduino IDE op uw computer.
  • Selecteer Arduino UNO R4 WiFi board en de juiste COM-poort.
  • Ga naar het Libraries icoon in de linkerbalk van de Arduino IDE.
  • Zoek "DIYables Bluetooth", vind dan de DIYables Bluetooth bibliotheek door DIYables
  • Klik op de Install knop om de bibliotheek te installeren.
Arduino UNO R4 DIYables Bluetooth library
  • U wordt gevraagd om enkele andere bibliotheek afhankelijkheden te installeren
  • Klik op de Install All knop om alle bibliotheek afhankelijkheden te installeren.
Arduino UNO R4 DIYables Bluetooth dependency

BLE Code

  • In Arduino IDE, ga naar File Examples DIYables Bluetooth ArduinoBLE_PinControl voorbeeld, of kopieer de bovenstaande code en plak het in de editor van Arduino IDE
/* * DIYables Bluetooth Library - Bluetooth 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: * - Arduino UNO R4 WiFi * - Arduino Nano 33 BLE / BLE Sense * - Arduino Nano 33 IoT * - Arduino MKR WiFi 1010 * - Arduino Nano RP2040 Connect * - Any board supporting the ArduinoBLE library * * Setup: * 1. Upload the sketch to your Arduino * 2. Open Serial Monitor 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_ArduinoBLE.h> // BLE Configuration const char* DEVICE_NAME = "Arduino_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_ArduinoBLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID); DIYables_BluetoothServer bluetoothServer(bluetooth); // Create Pin Control/Monitor app instance DIYables_BluetoothPinControl bluetoothPins; // Pin configuration const int LED_PIN = 13; const int OUTPUT_PIN_1 = 12; const int OUTPUT_PIN_2 = 11; const int INPUT_PIN_1 = 7; const int INPUT_PIN_2 = 6; const int ANALOG_PIN_1 = A0; const int ANALOG_PIN_2 = A1; void setup() { Serial.begin(9600); while (!Serial); Serial.println("DIYables Bluetooth - 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, "A0"); bluetoothPins.enablePin(ANALOG_PIN_2, BT_PIN_INPUT, "A1"); // 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 10) int currentAnalog1 = analogRead(ANALOG_PIN_1); if (abs(currentAnalog1 - lastAnalogState1) > 10) { 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 10) int currentAnalog2 = analogRead(ANALOG_PIN_2); if (abs(currentAnalog2 - lastAnalogState2) > 10) { 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); }
  • Klik op de Upload knop in Arduino IDE om de code naar Arduino UNO R4 WiFi te uploaden
  • Open de Serial Monitor
  • Bekijk het resultaat in Serial Monitor. Het ziet er als volgt uit:
COM6
Send
DIYables Bluetooth - Pin Control/Monitor Example Waiting for Bluetooth connection... Enabled pins: 7
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Mobiele App

  • Installeer de DIYables Bluetooth App op uw smartphone: Android | iOS

Let op: De DIYables Bluetooth App ondersteunt zowel BLE als Classic Bluetooth op Android, en BLE op iOS. Omdat de Arduino UNO R4 WiFi BLE gebruikt, werkt de app op zowel Android als iOS. Handmatige koppeling is niet nodig voor BLE — gewoon scannen en verbinden.

  • Open de DIYables Bluetooth App
  • Bij het voor de eerste keer openen van de app, vraagt het om machtigingen. Verleen de volgende:
    • Nearby Devices machtiging (Android 12+) / Bluetooth machtiging (iOS) - vereist om te scannen en verbinden met Bluetooth apparaten
    • Location machtiging (alleen Android 11 en lager) - vereist door oudere Android versies om te scannen naar BLE apparaten
  • Zorg dat Bluetooth is ingeschakeld op uw telefoon
  • Op het startscherm, tik op de Connect knop. De app scant naar BLE apparaten.
DIYables Bluetooth App - Home Screen with Scan Button
  • Zoek en tik op "Arduino_Pins" in de scanresultaten om te verbinden.
  • Eenmaal verbonden, gaat de app automatisch terug naar het startscherm. Selecteer de Digital Pins app uit het app menu.
DIYables Bluetooth App - Home Screen with Digital Pins App

Let op: U kunt op het instellingen icoon op het startscherm tikken om apps te verbergen/tonen op het startscherm. Voor meer details, zie de DIYables Bluetooth App Gebruikershandleiding.

  • U ziet de lijst van ingeschakelde pinnen met hun namen en huidige toestanden
  • Tik op output pinnen om tussen HIGH/LOW te schakelen, en kijk hoe input pin waarden bijwerken
DIYables Bluetooth App - Digital Pins Screen

Kijk nu terug naar de Serial Monitor in Arduino IDE. U zult zien:

COM6
Send
Bluetooth connected! Pin 13 set to HIGH Pin 13 set to LOW Digital pin 7 read: HIGH
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Creatieve Aanpassing - Pas de Code aan uw Project aan

Pinnen Inschakelen

// Enable pins with mode and friendly name bluetoothPins.enablePin(13, BT_PIN_OUTPUT, "LED"); bluetoothPins.enablePin(12, BT_PIN_OUTPUT, "Relay"); bluetoothPins.enablePin(7, BT_PIN_INPUT, "Button"); bluetoothPins.enablePin(A0, BT_PIN_INPUT, "Sensor"); // Check enabled pin count int count = bluetoothPins.getEnabledPinCount();

Pin Write/Read/Mode Afhandelen

bluetoothPins.onPinWrite([](int pin, int state) { digitalWrite(pin, state); Serial.print("Pin "); Serial.print(pin); Serial.println(state ? " ? HIGH" : " ? LOW"); }); bluetoothPins.onPinRead([](int pin) -> int { if (pin >= A0) { return analogRead(pin); } return digitalRead(pin); }); bluetoothPins.onPinModeChange([](int pin, int mode) { pinMode(pin, mode == BT_PIN_OUTPUT ? OUTPUT : INPUT_PULLUP); });

Toestandsveranderingen Pushen

// Notify the app when a pin state changes bluetoothPins.updatePinState(7, digitalRead(7)); bluetoothPins.updatePinState(A0, analogRead(A0));

Programmeervoorbeelden

Relais Bediening met Knop Monitor

const int RELAY_PIN = 12; const int BUTTON_PIN = 7; void setup() { pinMode(RELAY_PIN, OUTPUT); pinMode(BUTTON_PIN, INPUT_PULLUP); bluetoothPins.enablePin(RELAY_PIN, BT_PIN_OUTPUT, "Relay"); bluetoothPins.enablePin(BUTTON_PIN, BT_PIN_INPUT, "Button"); bluetoothPins.onPinWrite([](int pin, int state) { digitalWrite(pin, state); }); } void loop() { bluetoothServer.loop(); // Monitor button and push changes static int lastState = HIGH; int state = digitalRead(BUTTON_PIN); if (state != lastState) { lastState = state; bluetoothPins.updatePinState(BUTTON_PIN, state); } delay(10); }

Multi-LED Controller

const int LED_PINS[] = {8, 9, 10, 11, 12, 13}; const char* LED_NAMES[] = {"Red", "Green", "Blue", "Yellow", "White", "Built-in"}; const int NUM_LEDS = 6; void setup() { for (int i = 0; i < NUM_LEDS; i++) { pinMode(LED_PINS[i], OUTPUT); bluetoothPins.enablePin(LED_PINS[i], BT_PIN_OUTPUT, LED_NAMES[i]); } bluetoothPins.onPinWrite([](int pin, int state) { digitalWrite(pin, state); }); }

Probleemoplossing

Veelvoorkomende Problemen

1. Kan het apparaat niet vinden in de app

  • Zorg dat de Arduino UNO R4 WiFi is ingeschakeld en de sketch is geüpload
  • Controleer dat Bluetooth van uw telefoon is ingeschakeld
  • Op Android 11 en lager, schakel ook Locatieservices in

2. Pin schakeling werkt niet

  • Controleer dat de pin is ingeschakeld met BT_PIN_OUTPUT modus
  • Controleer dat de onPinWrite callback is ingesteld
  • Verifieer bedradingsverbindingen

3. Input pinnen updaten niet

  • Zorg dat updatePinState() wordt aangeroepen wanneer pin toestand verandert
  • Controleer polling frequentie in de loop

4. Analoge waarden tonen niet

  • Gebruik analogRead() in de onPinRead callback voor analoge pinnen
  • Analoge pinnen geven 0-1023 waarden terug

5. Verbinding valt vaak weg

  • Kom dichter bij de Arduino (reduceer afstand)
  • Zorg voor stabiele USB stroomvoorziening

6. Upload faalt of board wordt niet herkend

  • Installeer het nieuwste Arduino UNO R4 board pakket via Board Manager
  • Probeer een andere USB-kabel of poort

Projectideeën

  • Multi-relais bedieningspaneel
  • Knop en schakelaar monitor
  • LED verlichtingscontroller
  • Domotica schakelpaneel
  • Sensor input dashboard

Volgende Stappen

Na het beheersen van het Bluetooth Digitale Pinnen voorbeeld, probeer:

  1. Bluetooth Slider - Voor analoge waarde bediening
  2. Bluetooth Monitor - Voor tekst-gebaseerde status feedback
  3. Bluetooth Table - Voor gestructureerde pin status weergave
  4. Meerdere Bluetooth Apps - Combineren van pin bediening met andere apps

Ondersteuning

Voor extra hulp:

  • Bekijk de API Referentie documentatie
  • Arduino community forums

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!