HC-05⸮串行不起作用

时间:2018-01-05 12:26:33

标签: c bluetooth serial-port ascii arduino-uno

我最近为我的arduino配备了HC-05蓝牙模块,但我无法从中发送或接收数据。我用一个代码来打开或关闭一个led,但是在我从我的电脑的串行监视器发送一个字符后,我得到了⸮。此外,模块不响应任何AT命令。 HC-05 Connection Arduino connection我以9600和38400波特率运行了串行,但没有任何改变。我也试过没有行结束和NL和CR。但是这个模块有问题吗?这是我的代码:

/*
Arduino Turn LED On/Off using Serial Commands
Created April 22, 2015
Hammad Tariq, Incubator (Pakistan)

It's a simple sketch which waits for a character on serial
and in case of a desirable character, it turns an LED on/off.

Possible string values:
a (to turn the LED on)
b (tor turn the LED off)
*/

char junk;
String inputString="";

void setup()                    // run once, when the sketch starts
{
 Serial.begin(9600);            // set the baud rate to 9600, same     should be of your Serial Monitor
 pinMode(13, OUTPUT);
}

void loop()
{
  if(Serial.available()){
  while(Serial.available())
    {
      char inChar = (char)Serial.read(); //read the input
      inputString += inChar;        //make a string of the characters     coming on serial
    }
    Serial.println(inputString);
    while (Serial.available() > 0)  
    { junk = Serial.read() ; }      // clear the serial buffer
    if(inputString == "a"){         //in case of 'a' turn the LED on
      digitalWrite(13, HIGH);  
    }else if(inputString == "b"){   //incase of 'b' turn the LED off
      digitalWrite(13, LOW);
    }
    inputString = "";
  }
}

2 个答案:

答案 0 :(得分:1)

我会一步一步走 - 连接 Arduino引脚蓝牙引脚

RX(引脚0)----> TX

TX(Pin 1)----> RX

5V ----> VCC

GND ----> GND

将LED负极连接到arduino的GND,将正极连接到引脚13,电阻值介于220Ω - 1KΩ之间。你完成了电路。 注意:不要将RX连接到RX和TX到蓝牙的TX到arduinoyou将不会收到数据,这里TX表示发送,RX表示接收。

/*
* This program lets you to control a LED on pin 13 of arduino using a bluetooth module
*/
char data = 0;            //Variable for storing received data
void setup()
{
    Serial.begin(9600);   //Sets the baud for serial data transmission                               
    pinMode(13, OUTPUT);  //Sets digital pin 13 as output pin
}
void loop()
{
   if(Serial.available() > 0) // Send data only when you receive data:
   {
      data = Serial.read();   //Read the incoming data & store into data

      Serial.print(data);     //Print Value inside data in Serial monitor

      Serial.print("\n");        

      if(data == '1') // Checks whether value of data is equal to 1

         digitalWrite(13, HIGH);   //If value is 1 then LED turns ON

      else if(data == '0')  //  Checks whether value of data is equal to 0

         digitalWrite(13, LOW);    //If value is 0 then LED turns OFF
   }
}

链接到连接:https://halckemy.s3.amazonaws.com/uploads/image_file/file/153200/hc-05-LED%20blink%20Circuit.png

注意:上传代码时,从Arduino移除蓝牙模块的TX和RX线,上传完成后再连接。

答案 1 :(得分:0)

    #include <SoftwareSerial.h>

SoftwareSerial hc(2, 3); // RX | TX

void setup()
{
  pinMode(4, OUTPUT);  
  digitalWrite(4, HIGH);
  Serial.begin(9600);
  Serial.println("Enter AT commands:");
  hc.begin(38400);  // HC-05 default speed in AT command more
}

void loop()
{

  // Keep reading from HC-05 and send to Arduino Serial Monitor
  if (hc.available())
    Serial.write(hc.read());

  // Keep reading from Arduino Serial Monitor and send to HC-05
  if (Serial.available())
    hc.write(Serial.read());
}

使用此代码在命令模式下测试蓝牙模块。在hc-05中有两种模式。一种是命令模式,另一种是数据模式。 按下蓝牙模块上的按钮几秒钟。然后led在此时缓慢切换模块处于命令模式,在此可以测试AT命令。 注意:以9600波特率打开串行监视器