Arduino与蓝牙hc-06的连接

时间:2014-01-27 13:17:47

标签: bluetooth arduino

Hello people

我有点问题。我需要将Arduino Mini Pro与HC-06蓝牙连接起来。

输入/输出: Arduino HC-06

GND ------------------ G(GND)

VCC --------------------- V

D0(TX)----------------- 2

D1(RX)----------------- 3

我试过这段代码:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup()
{
Serial.begin(9600);
Serial.println("Goodnight moon!");
//mySerial.begin(115200);    //if you change the baud and want to re-run this sketch, make sure this baud rate matches the new rate.
mySerial.begin(38400);
//mySerial.begin(9600);


delay(1000);
mySerial.print("AT");
delay(1000);
mySerial.print("AT+VERSION");
//delay(1000);
//mySerial.print("AT+PIN1234"); // Set pin to 1234  was 1342
//delay(1000);
//mySerial.print("AT+NAMEJY-MCU-HC06"); // Set the name to JY-MCU-HC06
delay(1000);
//mySerial.print("AT+BAUD8"); // Set baudrate to 115200
//mySerial.print("AT+BAUD4"); // Set baudrate to 9600
//mySerial.print("AT+BAUD7"); // Set baudrate to 57600
delay(1000);
}

void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}

但是,我不能得到确定。你能帮助我吗? 谢谢。

1 个答案:

答案 0 :(得分:3)

正如mpflaga所说,调试这些模块的最佳和最简单的方法是直接与您的计算机连接,并通过终端程序(hyperterm等)进行连接。

话虽这么说,我猜这个模块可能运行在9600波特,而不是38400。 此外,我认为您的代码可以更好地工作:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

char myChar;

void setup()
{
    Serial.begin(9600);

    //mySerial.begin(38400);
    mySerial.begin(9600);

    mySerial.print("AT");
    delay(1000);
}

void loop() // run over and over
{
    while ( mySerial.available() )
    { 
        myChar = mySerial.read();
        Serial.print(myChar);
    }

    while ( Serial.available() )
    { 
        myChar = Serial.read();
        mySerial.print(myChar);
    }
}