come codici ho usato in modalità AT il seguente:
/**********************************************
* Load this sketch to communicate with *
* hc-06 or hc-05 modules in AT mode *
* *
* EsperienzeElettroniche 2020 *
**********************************************/
#include <SoftwareSerial.h>
/* Pins used for software serial connection */
#define rxPin 7
#define txPin 3
/* Try other values if it doesn't work */
#define baudrate 38400
SoftwareSerial hc06(rxPin, txPin);
void setup() {
Serial.begin(baudrate);
hc06.begin(baudrate);
}
void loop() {
if(hc06.available()){
Serial.write(hc06.read());
}
if(Serial.available()){
hc06.write(Serial.read());
}
}
invece per comunicare con altro dispositivo bluetooth il seguente:
/**********************************************
* Control an LED using bluetooth *
* serial commands. *
* 1 turns LED on, 0 turns it off *
* *
* EsperienzeElettroniche 2020 *
**********************************************/
#include <SoftwareSerial.h>
/* Pins used for software serial connection */
#define rxPin 7
#define txPin 3
/* Set serial connection speed */
#define baudrate 57600
#define ledPin 8
SoftwareSerial hc06(rxPin, txPin);
int counter=0;
char data;
void setup() {
hc06.begin(baudrate);
hc06.println("********************************");
hc06.println(" Bluetooth led controller");
hc06.println("Esperienze Elettroniche 2022");
hc06.println("********************************");
pinMode(ledPin, OUTPUT);
}
void loop() {
while(!hc06.available());
data=hc06.read();
if(data == '1'){
digitalWrite(ledPin, 1);
hc06.println("Led on");
}
if(data == '0'){
digitalWrite(ledPin, 0);
hc06.println("Led off");
}
}
Compila tutto correttamente.
Il mio circuito è come il seguente solo che invece del collegamento TX di hc-05 e D2 è fatto con TX di hc-05 e D7 e il 5V è preso direttamente da arduino: