Arduino Nano - LED Besturen via Bluetooth

Deze tutorial legt uit hoe u de Arduino Nano programmeert om een LED te besturen via Bluetooth of BLE.

Deze tutorial bevat instructies voor beide modules.

We gebruiken de Bluetooth Serial Monitor App op een smartphone om commando’s naar de Arduino Nano te sturen.

Deze commando’s zijn onder andere:

Arduino Nano LED Bluetooth

Hardware Benodigdheden

1×Official Arduino Nano
1×Alternatief: DIYables ATMEGA328P Nano Development Board
1×USB A naar Mini-B USB kabel
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 (verbindingsdraden)
1×(Aanbevolen) Schroefklem Uitbreidingsboard voor Arduino Nano
1×(Aanbevolen) Breakout Uitbreidingsboard voor Arduino Nano
1×(Aanbevolen) Stromsplitter voor Arduino Nano

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 niet bekend bent met de LED en Bluetooth Module (pinout, werking, programmeren, etc.), kunnen de volgende tutorials u verder helpen:

Aansluitschema

  • Indien u de LED via Bluetooth wilt aansturen, gebruikt u de HC-05 Bluetooth module volgens het onderstaande aansluitdiagram.
Arduino Nano LED Bluetooth wiring diagram

This image is created using Fritzing. Click to enlarge image

  • Wilt u de LED bedienen via BLE, dan sluit u de HM-10 BLE module aan volgens het volgende schema.
Arduino Nano LED BLE wiring diagram

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - LED besturen via Bluetooth/BLE

De code werkt voor zowel de HC-05 Bluetooth module als de HM-10 BLE module. Het is voor beide toepasbaar.

/* * Deze Arduino Nano code is ontwikkeld door newbiely.nl * Deze Arduino Nano code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino-nano/arduino-nano-control-led-via-bluetooth */ #include <SoftwareSerial.h> #define SOFT_RX 4 // The Arduino Nano pin connected to the TX of the bluetooth module #define SOFT_TX 5 // The Arduino Nano pin connected to the RX of the bluetooth module #define LED_PIN 12 // The Arduino Nano pin connected to the LED SoftwareSerial bluetooth(SOFT_RX, SOFT_TX); void setup() { Serial.begin(9600); bluetooth.begin(9600); pinMode(LED_PIN, OUTPUT); // set the digital pin as output: } void loop() { if (bluetooth.available()) { // if there is data comming String command = bluetooth.readStringUntil('\n'); // read string until meet newline character if (command == "LED OFF") { digitalWrite(LED_PIN, LOW); // turn off LED bluetooth.println("LED is turned OFF"); // reports action to smartphone app } else if (command == "LED ON") { digitalWrite(LED_PIN, HIGH); // turn on LED bluetooth.println("LED is turned ON"); // reports action to smartphone app } } }

Snelle Stappen

  • Download de Bluetooth Serial Monitor App op uw smartphone.
  • Open de code in de Arduino IDE en klik op de Upload-knop om deze naar de Arduino Nano te uploaden. Mocht u problemen ondervinden bij het uploaden, ontkoppel dan eerst de TX- en RX-pinnen van de Bluetooth module, upload de code, en sluit ze daarna weer aan.
  • Start de Bluetooth Serial Monitor App op uw telefoon en kies Classic Bluetooth of BLE, afhankelijk van welke module u gebruikt.
Bluetooth Serial Monitor App
  • Maak verbinding met de HC-05 Bluetooth module of HM-10 BLE module via de app.
Bluetooth Serial Monitor pairing
  • Typ “LED ON of “LED OFF in en druk op de Send-knop.
Bluetooth Serial Monitor App
  • Controleer de status van de LED op de Arduino Nano. Deze zal aan of uit zijn.
  • Daarnaast kunt u de status van de LED ook in de Bluetooth App zien.
  • Controleer ten slotte het resultaat op de Android App.
Bluetooth Serial Monitor App

U vraagt zich wellicht af hoe de Arduino Nano een volledig commando interpreteert. Bijvoorbeeld, wanneer u “OFF” stuurt, hoe weet de Arduino of het commando “O”, “OF” of “OFF” is?

Bij het verzenden voegt de Bluetooth App een newline-karakter ('\n') toe door de optie “newline” in de app te kiezen. De Arduino Nano leest data totdat het newline-karakter wordt bereikt. Dit newline-karakter werkt als scheidingsteken tussen commando’s.

Als u de Bluetooth Serial Monitor app nuttig vindt, waardeert u het misschien om een 5-sterren beoordeling achter te laten in de Play Store. Wij zijn u dankbaar voor uw steun. 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.

Functie Referenties

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