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.
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
Openbaarmaking: Sommige van de links in deze sectie zijn Amazon-affiliate links. We kunnen een commissie ontvangen voor aankopen die via deze links worden gedaan, zonder extra kosten voor u. We waarderen uw steun.
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.
U wordt gevraagd om enkele andere bibliotheek afhankelijkheden te installeren
Klik op de Install All knop om alle bibliotheek afhankelijkheden te installeren.
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 Configurationconst 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 instancesDIYables_ArduinoBLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID);DIYables_BluetoothServer bluetoothServer(bluetooth);// Create Pin Control/Monitor app instanceDIYables_BluetoothPinControl bluetoothPins;// Pin configurationconstint LED_PIN = 13;constint OUTPUT_PIN_1 = 12;constint OUTPUT_PIN_2 = 11;constint INPUT_PIN_1 = 7;constint INPUT_PIN_2 = 6;constint ANALOG_PIN_1 = A0;constint ANALOG_PIN_2 = A1;voidsetup() {Serial.begin(9600);while (!Serial);Serial.println("DIYables Bluetooth - Pin Control/Monitor Example");// Initialize pinspinMode(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 digitalReadint 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());}voidloop() {// Handle Bluetooth server communications bluetoothServer.loop();// Optional: Monitor input pins and send updatesstaticunsignedlong lastInputCheck = 0;staticint lastInputState1 = HIGH;staticint lastInputState2 = HIGH;staticint lastAnalogState1 = 0;staticint lastAnalogState2 = 0;if (millis() - lastInputCheck >= 100) { lastInputCheck = millis();// Check digital input pin 1int 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 2int 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:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
DIYables Bluetooth - Pin Control/Monitor Example
Waiting for Bluetooth connection...
Enabled pins: 7
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
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.
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.
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
Kijk nu terug naar de Serial Monitor in Arduino IDE. U zult zien:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
Bluetooth connected!
Pin 13 set to HIGH
Pin 13 set to LOW
Digital pin 7 read: HIGH
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
Creatieve Aanpassing - Pas de Code aan uw Project aan
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!