Ciao a tutti, eccomi di nuovo qui con un nuovo tutorial molto interessante che riguarda il modulo HC-06 Bluetooth. In questo articolo mostrerò come utilizzarlo e configurarlo con Arduino 🙂 .
Video
Lista dei componenti
Di seguito alcuni moduli HC-06 presenti su amazon:
Bluetooth HC-06
Il modulo bluetooth HC-06 è un modulo che permette di trasformare un segnale sul bus UART\USART, più comunemente conosciuto semplicemente come bus seriale, in un segnale wireless tramite Bluetooth, generalmente utilizzando il profilo SPP (Serial Port Profile).
Normalmente questo dispositivo viene utilizzato quando si vuole far comunicare un microcontrollore (come Arduino nel nostro caso) con il mondo esterno tramite un collegamento wireless (senza fili), dove il mondo esterno può essere un Computer, uno Smartphone, un Tablet o quant’altro la tecnologia possa fornire con una connessione Bluetooth.
Non vi scrivo tutte le caratteristiche tecniche, per questo consiglio di consultare il datasheet.
Come si può notare dall’immagine qui in alto, il modulo HC-06 Bluetooth si presenta con quattro pin, due per l’alimentazione (VCC, GND) e due per la comunicazione seriale con Arduino (RX, TX).
Collegamenti
HC-06 | ARDUINO |
VCC | +3.3V |
GND | GND |
TX | 2 |
RX | 3 |
Con il seguente sketch sarà possibile visualizzare sul monitor seriale di Arduino IDE le stringhe di caratteri inviate al modulo bluetooth tramite uno Smartphone o un PC. Sul web esistono tantissime App in grado di inviare stringhe tramite bluetooth, per Android consiglio questa App . Mentre per iOS, purtroppo, il modulo HC-06 non viene riconosciuto dato che gli iPhone supportano solo il protocollo Bluetooth Low Energy (BLE).
Sketch
// HC-06 with Arduino // Giuseppe Caccavale // www.giuseppecaccavale.it #include <SoftwareSerial.h> int rxPin = 3; int txPin = 2; SoftwareSerial bluetooth(rxPin, txPin); String message; //string that stores the incoming message void setup() { Serial.begin(9600); //set baud rate bluetooth.begin(9600); //set baud rate } void loop() { while (bluetooth.available()) { message += char(bluetooth.read()); } if (!bluetooth.available()) { if (message != "") { //if data is available Serial.println(message); //show the data message = ""; //clear the data } } delay(5000); //delay }
Accendiamo e spegniamo un led con il Bluetooth
Di seguito mostrerò come poter accendere e spegnere un LED attraverso l’HC-06. I collegamenti rimangono tali, bisogna solo aggiungere un LED sul pin 13.
Di seguito lo sketch da caricare su Arduino
Sketch
// HC-06 with Arduino and LED // Giuseppe Caccavale // www.giuseppecaccavale.it #include <SoftwareSerial.h> int rxPin = 3; int txPin = 2; SoftwareSerial bluetooth(rxPin, txPin); String message; //string that stores the incoming message const int Led = 13; void setup() { Serial.begin(9600); //set baud rate bluetooth.begin(9600); //set baud rate pinMode(Led, OUTPUT); } void loop() { while (bluetooth.available()) { message += char(bluetooth.read()); } if (!bluetooth.available()) { if (message != "") { //if data is available if (message == "H") { digitalWrite(Led, HIGH); Serial.println("Led ON"); //show the data delay(20); message = ""; //clear the data } else if (message == "L") { digitalWrite(Led, LOW); Serial.println("Led OFF"); //show the data delay(20); message = ""; //clear the data } } } }
Il funzionamento è semplice, quando il modulo bluetooth riceve il carattere H l’uscita 13 viene posta su HIGH, mentre se riceve il carattere L l’uscita 13 viene posta su LOW.
Come cambiare SSID e Pincode?
Se c’è la necessita di cambiare il SSID (nome device bluetooth) del bluetooth e il Pincode è sufficiente caricare lo sketch seguente su Arduino (utilizzando gli stessi collegamenti visti in precedenza) ed utilizzare il serial monitor di Arduino per configurare l’HC-06.
E’ possibile utilizzare i seguenti comandi:
- AT -> Se la comunicazione funziona il modulo risponde OK
- AT+VERSION -> Restituisce la versione del firmware
- AT+BAUDx -> Imposta il Baudrate, sostituire al posto di x il numero 1 per 1200 bps, 2=2400, 3=4800, 4=9600, 5=19200, 6=38400, 7=57600, 8=115200, 9=230400, A=460800, B=921600, C=1382400
- AT+NAMEstring -> Sostituire con string il nome che si vuole dare al modulo (massimo 20 caratteri)
- AT+PINxxxx -> Imposta il pincode del modulo bluetooth (es.1234)
// HC-06 with Arduino // Giuseppe Caccavale // www.giuseppecaccavale.it #include <SoftwareSerial.h> int rxPin = 3; int txPin = 2; SoftwareSerial bluetooth(rxPin, txPin); String message; //string that stores the incoming message void setup() { Serial.begin(9600); bluetooth.begin(9600); Serial.println("Lista dei comandi HC-06:\n"); Serial.println("AT Se la comunicazione funziona il modulo risponde OK"); Serial.println("AT+VERSION Restituisce la versione del firmware"); Serial.println("AT+BAUDx Imposta il Baudrate, al posto di x mettere 1 per 1200 bps, 2=2400, 3=4800, 4=9600, 5=19200, 6=38400, 7=57600, 8=115200, 9=230400, A=460800, B=921600, C=1382400"); Serial.println("AT+NAMEstring Al posto di string mettere il nome che vuoi dare al modulo (massimo 20 caratteri)"); Serial.println("AT+PINxxxx Imposta il pincode del modulo bluetooth (es.1234)"); } void loop() { if (bluetooth.available()) { Serial.write(bluetooth.read()); } if (Serial.available()) { bluetooth.write(Serial.read()); } }
Arduino Mega
Arduino Mega possiede 4 UARTs (Serial, Serial1, Serial2, Serial3) quindi utilizzando il modulo bluetooth con la Mega board si può fare a meno della libreria SoftwareSerial.h collegando il modulo HC-06 ad Arduino Mega nel seguente modo:
HC-06 | MEGA | |
VCC | -> | +3.3V |
GND | -> | GND |
TX | -> | 15 |
RX | -> | 14 |
Sketch per Arduino Mega
Questo sketch ha lo stesso funzionamento di quello ad inizio articolo.
// HC-06 with Arduino Mega // Giuseppe Caccavale // www.giuseppecaccavale.it String message; //string that stores the incoming message void setup() { Serial.begin(9600); //set baud rate Serial3.begin(9600); //set baud rate } void loop() { while (Serial3.available()) { message += char(Serial3.read()); } if (!Serial3.available()) { if (message != "") { //if data is available Serial.println(message); //show the data message = ""; //clear the data } } delay(5000); //delay }
Lo sketch seguente ha lo scopo di far accendere e spegnere un LED tramite bluetooth.
// HC-06 with Arduino MEGA and LED // Giuseppe Caccavale // www.giuseppecaccavale.it String message; //string that stores the incoming message const int Led = 13; void setup() { Serial.begin(9600); //set baud rate Serial3.begin(9600); //set baud rate pinMode(Led, OUTPUT); } void loop() { while (Serial3.available()) { message += char(Serial3.read()); } if (!Serial3.available()) { if (message != "") { //if data is available if (message == "H") { digitalWrite(Led, HIGH); Serial.println("Led ON"); //show the data delay(20); message = ""; //clear the data } else if (message == "L") { digitalWrite(Led, LOW); Serial.println("Led OFF"); //show the data delay(20); message = ""; //clear the data } } }
Mentre questo è lo sketch per il cambio del PINCODE e dell’SSID:
// HC-06 with Arduino MEGA // Giuseppe Caccavale // www.giuseppecaccavale.it String message; //string that stores the incoming message void setup() { Serial.begin(9600); Serial3.begin(9600); Serial.println("Lista dei comandi HC-06:\n"); Serial.println("AT Se la comunicazione funziona il modulo risponde OK"); Serial.println("AT+VERSION Restituisce la versione del firmware"); Serial.println("AT+BAUDx Imposta il Baudrate, al posto di x mettere 1 per 1200 bps, 2=2400, 3=4800, 4=9600, 5=19200, 6=38400, 7=57600, 8=115200, 9=230400, A=460800, B=921600, C=1382400"); Serial.println("AT+NAMEstring Al posto di string mettere il nome che vuoi dare al modulo (massimo 20 caratteri)"); Serial.println("AT+PINxxxx Imposta il pincode del modulo bluetooth (es.1234)"); } void loop() { if (Serial3.available()) { Serial.write(Serial3.read()); } if (Serial.available()) { Serial3.write(Serial.read()); } }