HC-05 bluetooth module on Arduino + Debugging

2019-08-07 02:16发布

问题:

I'm kind of stuck here. I have a HC-05 Bluetooth module - from ebay- and i'm testing it to make certain it works. I have uploaded the following sketch to the chip:

//////////////////////////////////////////////////////////////////////////////////

// REMIXED BY: TECHBITAR (HAZIM BITAR)
// LICENSE: PUBLIC DOMAIN
// DATE: MAY 2, 2012
// CONTACT: techbitar at gmail dot com

char INBYTE;
int  LED = 13; // LED on pin 13

void setup() {
  Serial.begin(9600); 
  pinMode(LED, OUTPUT);
}

void loop() {
  Serial.println("Press 1 to turn Arduino pin 13 LED ON or 0 to turn it OFF:");
  while (!Serial.available());   // stay here so long as COM port is empty   
  INBYTE = Serial.read();        // read next available byte
  if( INBYTE == '0' ) digitalWrite(LED, LOW);  // if it's a 0 (zero) tun LED off
  if( INBYTE == '1' ) digitalWrite(LED, HIGH); // if it's a 1 (one) turn LED on
  delay(50);
}

I have used three different bluetooth terminal on android as well as the TeraTerm terminal on WIN7 to test if the blutooth module successfully communicates. For whatever reason, all 4 receive the line Press 1 to turn Arduino pin 13 LED ON or 0 to turn it OFF:but when I enter 1 in the terminals and send it to the module, the LED does not turn on.

Note: The LED works I'm affirmative I've wired everything appropriately My RX and TX serial lines must work otherwise I would not be able to upload the sketch.

Is it possible that the HC-05 is not working and is unable to receive information? What can I do to test otherwise?

Thanks alot!

回答1:

Thanks @SunGa for the loop-back idea. It worked, so I can confirm the Bluetooth module is in good health. Reading through other forums to try to understand why pin 0 could not receive the serial signal sent by the Bluetooth module I discovered that this was a common issue. It so turns out that since RX and TX are hard wired to the USB to TTL converter chip on the board, sometimes the state of Pin 0 and Pin 1 get "stuck" and they can't be used appropriately ( which is unfortunate! and poor engineering imo).

I was able to successfully send and receive data by making use of the SoftwareSerial library and remapping Pins 10 and 11 to RX and TX.

That said, If an AtMega chip was used as a stand alone microcontroller (e.g. DIYduino) then Pins 0 and 1 could be used to communicate to the Bluetooth module!

Hope this can help others who face the same issue



回答2:

I am not at all familiar with Arduino. But can suggest you to configure loop-back connections at HC05 pins and test only communication between Android (or PC) terminal and HC05. This will figure out whether HC05 is working or not.



回答3:

I have encountered the same problem and found an awesome solution. The solution exists as the library called SoftwareSerial.h". This library does really do wonders, as it helps in the most important things, "Debugging" the app that you are using, may it be android app or ios app.

I have used the following setup/apparatus:

  1. Adruino module
  2. LightBlue Explorer App(ios 10)
  3. HC-05 Bluetooth Module(BLE)

Please note: Connect ur Bluetooth rx pin to adriuno pin 9 and Bluetooth tx pin to adruino pin 10.

In this way you can use see the adruino board serial interface for debugging purpose.

#include <SoftwareSerial.h>
int state = 0;
SoftwareSerial Bluetooth(10,9);
int data;

void setup() {
  // put your setup code here, to run once:
  pinMode(4,OUTPUT);
  Serial.begin(9600);
  Bluetooth.begin(9600);
}

void loop() {
  if(Bluetooth.available() > 0)
  {
    data = Bluetooth.read();
    if(data == '1')
    {
      digitalWrite(4,HIGH);
      Serial.println("LED Turned ON");
    }
    else{
      digitalWrite(4,LOW);
      Serial.println("LED Turned OFF");
    }
    Serial.println(data);
  }
  delay(1000);
}

For more information, please refer Bluetooth Module Debugging