代码之家  ›  专栏  ›  技术社区  ›  antisycop

串行HC-05不工作

  •  0
  • antisycop  · 技术社区  · 6 年前

    我最近为我的arduino买了一个HC-05蓝牙模块,但我无法从中发送或接收数据。我使用了一个代码来打开或关闭led,但在我从PC的串行监视器发送了一个字符后,我得到了¸®。此外,模块不响应任何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 回复  |  直到 6 年前
        1
  •  2
  •   anime    6 年前

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

    RX(引脚0)–俬–俬->德克萨斯州

    TX(引脚1)->接收

    5V->VCC公司

    GND->接地

    将LED负极连接至arduino的GND,正极连接至针脚13,电阻值介于220Ω–1KΩ之间。你的电路也完成了。 注意:不要将蓝牙的RX连接到RX,TX连接到TX到Arduino您将不会收到任何数据,这里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线,上传完成后,连接它们。

        2
  •  1
  •   vallabh    6 年前
        #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波特率打开串行监视器