Arduino - Bluetooth

In deze handleiding leert u:

Arduino Bluetooth

De reikwijdte van deze handleiding:

Deze handleiding gaat over Classic Bluetooth (Bluetooth 2.0). Als u Bluetooth Low Energy (BLE, Bluetooth 4.0) zoekt, zie dan deze gelijksoortige handleiding: Arduino - Bluetooth Low Energy

Hardware Benodigd

1×Arduino UNO of Genuino UNO
1×USB 2.0 kabel type A/B
1×HC-05 Bluetooth-Module
1×Jumper wires (jumperkabels)
1×(Aanbevolen) Schroefklem Block Shield voor Arduino Uno
1×(Aanbevolen) Breadboard-Shield voor Arduino Uno
1×(Aanbevolen) Behuizing voor Arduino Uno
1×(Aanbevolen) Prototyping Basisplaat & Breadboard Kit voor Arduino Uno

Of u kunt de volgende kits kopen:

1×DIYables Sensorkit (30 sensoren/displays)
1×DIYables Sensorkit (18 sensoren/displays)
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.

Over HC-05 Bluetooth Module

De HC-05 is een seriële Bluetooth-module. Het werkt als een seriële naar Bluetooth-converter. Het doet het volgende:

  • Ontvangt data van de seriële RX-pin en verzendt deze via Bluetooth naar het gekoppelde apparaat (zoals een smartphone)
  • Ontvangt data via Bluetooth (van het gekoppelde apparaat) en stuurt deze naar de seriële TX-pin

Meer specifiek, in de context van communicatie tussen Arduino en een smartphone-app (Android/iOS):

  • Arduino maakt verbinding met de HC-05 via seriële of SoftwareSerial pins
  • De HC-05 Bluetooth-module is gekoppeld aan een smartphone-app
  • Arduino verzendt data naar de smartphone-app door data naar de seriële of SoftwareSerial-interface te sturen
  • Arduino ontvangt data van de smartphone-app door data uit de seriële of SoftwareSerial-interface te lezen
  • Er is geen specifieke Bluetooth-code nodig op Arduino

Pinout

Bluetooth Pinout

De HC-05 Bluetooth-module heeft 6 pinnen:

  • Enable/Key pin: Deze pin wordt gebruikt om te schakelen tussen Data Mode (laag, LOW) en Command Mode (hoog, HIGH). Niet aangesloten staat de module standaard in Data Mode.
  • VCC pin: voedingspin, sluit deze aan op +5V voedingsspanning
  • GND pin: aardingspin, sluit deze aan op GND van de voeding
  • TX pin: seriële datapin, sluit aan op RX-pin van Arduino. Data die via Bluetooth binnenkomt, wordt via deze pin als seriële data doorgestuurd.
  • RX pin: seriële datapin, sluit aan op TX-pin van Arduino. Data gestuurd naar deze pin wordt via Bluetooth verzonden.
  • State: de statuspin is verbonden met een onboard LED en kan worden gebruikt om te controleren of Bluetooth correct werkt

Voor basisfuncties zijn alleen 4 pinnen van de HC-05 module nodig om te verbinden met Arduino.

De HC-05 Bluetooth-module heeft ook twee ingebouwde onderdelen:

  • Een LED: geeft de status van de module aan
    • Eén keer per 2 seconden knipperen: module is in Command Mode
    • Regelmatig knipperen: wacht op verbinding in Data Mode
    • Twee keer per seconde knipperen: succesvolle verbinding in Data Mode
  • Een knop: kan worden gebruikt om de Key/Enable pin te bedienen om de modus (Data of Command) te kiezen

Hoe het werkt

De HC-05 Bluetooth-module kent twee werkmodi:

  • Data Mode: voor het uitwisselen van data met het gekoppelde apparaat
  • Command Mode: om parameters te configureren

Gelukkig werkt de HC-05 met Arduino met standaardinstellingen zonder verdere configuratie.

Standaardinstellingen HC-05

Standaard Bluetooth-naam “HC-05”
Standaard Wachtwoord 1234 of 0000
Standaard Communicatie Slave
Standaard Modus Data Mode
Standaard Baudrate Data Mode 9600, 8, N, 1
Standaard Baudrate Command Mode 38400, 8, N, 1

Over Bluetooth Serial Monitor App

De Bluetooth Serial Monitor app is een mobiele applicatie met een gebruikersinterface die lijkt op de Serial Monitor van de Arduino IDE. Het communiceert met Arduino via Bluetooth. U kunt via deze app communiceren met Arduino alsof u de Serial Monitor op uw PC gebruikt, zonder dat u speciale Bluetooth-code hoeft toe te voegen aan uw Arduino-code, door de volgende stappen te volgen:

  • Verbind Arduino met de HC-05 Bluetooth-module
  • Installeer de Bluetooth Serial Monitor App op uw smartphone
  • Open de app en koppel deze met de HC-05 Bluetooth-module

Nu kunt u data verzenden en ontvangen van Arduino, net als met de Arduino IDE Serial Monitor. U hoeft de bestaande Arduino-code niet aan te passen of Bluetooth-code toe te voegen.

Bedradingsschema

Arduino Bluetooth Bedradingsschema

This image is created using Fritzing. Click to enlarge image

Bedradings Tabel

Arduino Pins HC-05 Bluetooth Pins
RX (Pin 0) TX
TX (Pin 1) RX
5V VCC
GND GND
Enable/Key (NIET aangesloten)
State (NIET aangesloten)

※ Notiz:

U kunt ook andere Arduino-pinnen gebruiken door het Serial-object in uw Arduino-code aan te passen naar Serial1, Serial2,... of SoftwareSerial als deze beschikbaar zijn.

Hoe programmeert u voor Bluetooth

Er is geen speciale Bluetooth-code nodig. We gebruiken gewoon de standaard Seriële (Serial) code.

Arduino verzendt data naar Bluetooth App op Smartphone

Om data van Arduino naar een Bluetooth-app op een smartphone te verzenden, gebruiken we de volgende Arduino-code:

In dit voorbeeld sturen we elke seconde de tekst “Arduino here, command me!” van Arduino naar de Bluetooth-app op de smartphone.

/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/arduino-bluetooth */ void setup() { Serial.begin(9600); } void loop() { Serial.println("Arduino here, command me!"); delay(1000); }

Stappenplan

  • Installeer de Bluetooth Serial Monitor App op uw smartphone
  • Verbind de HC-05 Bluetooth-module met Arduino volgens bovenstaand bedradingsschema
  • Kopieer de bovenstaande code en open deze in de Arduino IDE
  • Klik op de Upload-knop in de Arduino IDE om de code naar Arduino te uploaden. Soms kan het uploaden mislukken; in dat geval verbindt u de TX- en RX-pinnen los van de Bluetooth-module, upload de code en sluit daarna de RX/TX-pinnen weer aan
  • Open de Serial Monitor in de Arduino IDE
  • Open de Bluetooth Serial Monitor App op uw smartphone
  • Selecteer de Classic Bluetooth modus
Bluetooth Serial Monitor App
  • Koppel met de HC-05 Bluetooth-module
Bluetooth Serial Monitor koppeling
  • Bekijk het resultaat op de Android app.
Bluetooth Serial Monitor App
  • Bekijk het resultaat op de Serial Monitor van Arduino IDE.
COM6
Send
Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me!
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

U ziet dat de data op de Serial Monitor van Arduino IDE en in de Android-app identiek zijn.

Bluetooth-app verstuurt data naar Arduino

De onderstaande code doet:

  • Bluetooth-app verstuurt data naar Arduino
  • Arduino leest data en stuurt een reactie terug naar de Bluetooth-app:
/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/arduino-bluetooth */ // NOTE: change the Serial to other Serial/Software Serial if you connects Bluetooth module to other pins void setup() { Serial.begin(9600); } void loop() { Serial.println(F("Arduino here, command me!")); if (Serial.available()) { // if there is data comming String command = Serial.readStringUntil('\n'); // read string until meet newline character if (command == "LED OFF") { Serial.println("LED is turned OFF"); // reports action to smartphone app // TODO: control your LED here } else if (command == "LED ON") { Serial.println("LED is turned ON"); // reports action to smartphone app // TODO: control your LED here } } delay(500); }

Stappenplan

  • Kopieer bovenstaande code en open deze in de Arduino IDE
  • Klik op de Upload-knop om de code naar Arduino te uploaden
  • Open de Serial Monitor in de Arduino IDE
  • Open de Android app en koppel deze met de HC-05 Bluetooth-module (zoals in het vorige voorbeeld)
  • Na koppeling typt u “LED ON” of “LED OFF” in de Android app en druk op de “SEND” knop
Bluetooth Serial Monitor App
  • Arduino ontvangt de data en toont een reactie in de seriële monitor. Deze data wordt verzonden naar de Bluetooth app
  • Bekijk het resultaat in de Android app
Bluetooth Serial Monitor App
  • Bekijk het resultaat op de Serial Monitor van Arduino IDE
COM6
Send
Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me! LED ON LED is turned ON Arduino here, command me! Arduino here, command me! Arduino here, command me! LED OFF LED is turned OFF Arduino here, command me! Arduino here, command me!
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

U ziet dat de data op de Serial Monitor van Arduino IDE en in de Android app identiek zijn.

Arduino-code - Bedien LED met smartphone-app via Bluetooth

De onderstaande Arduino-voorbeeldcode accepteert twee commando’s (“ON” en “OFF”) van de Bluetooth Serial Monitor App om een ingebouwde LED aan of uit te zetten.

/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/arduino-bluetooth */ // NOTE: change the Serial to other Serial/Software Serial if you connects Bluetooth module to other pins #define LED_PIN 8 void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); // set the digital pin as output: } void loop() { if (Serial.available()) { // if there is data comming String command = Serial.readStringUntil('\n'); // read string until meet newline character if (command == "OFF") { digitalWrite(LED_PIN, LOW); // turn off LED Serial.println("LED is turned OFF"); // reports action to smartphone app } else if (command == "ON") { digitalWrite(LED_PIN, HIGH); // turn on LED Serial.println("LED is turned ON"); // reports action to smartphone app } } }

Meer gedetailleerde instructies vindt u in de handleiding Arduino bedient LED via Bluetooth/BLE.

Arduino-code - Bedien servomotor met smartphone-app via Bluetooth

De onderstaande Arduino-code ontvangt een hoekwaarde van de Bluetooth Serial Monitor App om de hoek van een servomotor te regelen.

/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/arduino-bluetooth */ // NOTE: change the Serial to other Serial/Software Serial if you connects Bluetooth module to other pins #include <Servo.h> Servo servo; // create servo object to control a servo int pos = 0; // variable to store the servo position void setup() { Serial.begin(9600); servo.attach(11); // attaches the servo on pin 11 to the servo object } void loop() { if (Serial.available()) { // if there is data comming int angle = Serial.parseInt(); if (angle >= 0 && angle <= 180) { servo.write(angle); // rotate servo Serial.print("Rotated servo to angle: ");// reports action to smartphone app Serial.println(angle); } else { Serial.print("Invalid angle: ");// reports invalid value to smartphone app Serial.println(angle); } } }

Meer gedetailleerde instructies vindt u in de handleiding Arduino bedient Servomotor via Bluetooth/BLE.

Als u de Bluetooth Serial Monitor app nuttig vindt, geef dan alstublieft een 5-sterren beoordeling in de Play Store. Dank u wel!

Video Tutorial

We overwegen het maken van videotutorials. Als u videotutorials belangrijk vindt, abonneer u dan op ons YouTube-kanaal om ons te motiveren de video's te maken.

Bekijk onze video-tutorial voor een visuele begeleiding bij dit project! De video biedt extra inzichten en laat stap voor stap het proces zien.

Functiereferenties

※ 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!