Arduino bestuurt LED via Bluetooth

In deze handleiding gaan we leren hoe u een Arduino programmeert om een LED te besturen via Bluetooth of BLE.

Deze tutorial geeft instructies voor beide modules.

We gebruiken de Bluetooth Serial Monitor App op uw smartphone om commando’s naar de Arduino te sturen. De commando’s zijn:

Arduino LED Bluetooth

Hardware Benodigd

1×Arduino UNO of Genuino UNO
1×USB 2.0 kabel type A/B
1×HC-05 Bluetooth-module
1×(Alternatief) HM-10 BLE-module
1×LED Kit
1×LED (red)
1×220 ohm weerstand
1×Breadboard (experimenteerprint)
1×Jumper wires
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 LED en Bluetooth Module

Als u nog niet bekend bent met LED en Bluetooth Module (pinout, hoe het werkt, hoe te programmeren ...), leer er dan meer over in de volgende tutorials:

Bedradingsschema

  • Wilt u de LED via Bluetooth aansturen? Gebruik dan de HC-05 Bluetooth-module met het onderstaande bedradingsschema.
Arduino LED Bluetooth Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • Wilt u de LED via BLE aansturen? Gebruik dan de HM-10 BLE-module met het onderstaande bedradingsschema.
Arduino LED BLE Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code - LED bestuurt via Bluetooth/BLE

De onderstaande code werkt voor zowel de HC-05 Bluetooth-module als de HM-10 BLE-module.

/* * 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-controls-led-via-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 } } }

Snelle Stappen

  • Installeer de Bluetooth Serial Monitor App op uw smartphone
  • Kopieer de bovenstaande code, open deze in de Arduino IDE en upload de code naar de Arduino
  • Klik op de Upload-knop in de Arduino IDE om de code naar de Arduino te uploaden. Mocht het uploaden niet lukken, probeer dan de TX- en RX-pinnen van de Bluetooth-module tijdelijk los te koppelen, upload de code en sluit daarna de RX/TX-pinnen weer aan.
  • Open de Bluetooth Serial Monitor App op uw smartphone
  • Selecteer Classic Bluetooth of BLE, afhankelijk van de module die u gebruikt
Bluetooth Serial Monitor App
  • Koppel de Bluetooth App met de HC-05 Bluetooth-module of de HM-10 BLE-module
Bluetooth Serial Monitor pairing
  • Typ “ON” of “OFF” en klik op de Send-knop
Bluetooth Serial Monitor App
  • Kijk naar de status van de LED op het Arduino-bord. De LED zal respectievelijk AAN of UIT zijn.
  • U ziet ook de status van de LED in de Bluetooth App
  • Bekijk het resultaat in de Android App.
Bluetooth Serial Monitor App

U vraagt zich wellicht af hoe Arduino een compleet commando kan ontvangen? Bijvoorbeeld, wanneer we het commando “OFF” sturen, hoe weet Arduino dan dat het commando “O”, “OF” of “OFF” is?

⇒ Bij het sturen van een commando voegt de Bluetooth App een newline-teken ('\n') toe door de optie “newline” te selecteren in de App. De Arduino leest de gegevens tot het newline-teken wordt bereikt. Het newline-teken fungeert als scheiding tussen commando’s.

Als de Bluetooth Serial Monitor App nuttig voor u is, geef dan alstublieft een 5-sterrenbeoordeling op Play Store. Hartelijk dank!

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.

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!