Arduino UNO + HC05 => weird data transfer

Hey,
recently I startet working with the Arduino UNO. So far so good.
After 1 Day of getting the Arduino with the HC05 to receive AT-Commands I have a problem.

Setup:

  • Arduino UNO
  • HC 05

Wired:
HC05 GND => UNO GND
HC05 VCC => UNO Digital Pin 10
HC05 TXD => UNO Digital Pin 11
HC05 RXD => Breadboard => 1k Resistor => UNO Digital Pin 12
Breadboard => 2k Resistor => UNO GND

Why the VCC On UNO?
Because its easier to get to the AT-Mode. Just press Reset Arduino Button and HC05 Button instead of plugging out/in wires.

Code:

#include <SoftwareSerial.h>

// Define Pins
#define HC_Power 10 
#define HC_RX 12
#define HC_TX 11

// Define BT-Serial
SoftwareSerial btSerial(HC_TX, HC_RX); // TX | RX => von HC05 (From Arduino its other way round)

void setup() {
  // Start Debugging to Computer via USB-Serial
    Serial.begin(9600);
    Serial.println("Los geht's");
    Serial.println("To get into AT-Mode restart Arduino while Pressing HC05 Button.");

  // Start Bluetooth Connection
    btSerial.begin(38400);  

  // Pin Stuff
    pinMode(13,OUTPUT);

  // Activate Power for HC05
    pinMode(HC_Power, OUTPUT);
    delay(500);
    digitalWrite(HC_Power, HIGH);
    Serial.println("Bluetooth Module activated!");
}

int data = 0;

void loop() { // run over and over
  // Read Data from HC05
  if (btSerial.available()) {
    data = btSerial.read();
    Serial.println(data);
  
    if(data == '1'){
      digitalWrite(13,HIGH);
      btSerial.println("LED ON");
    }
    else if (data == '0'){      
      digitalWrite(13,LOW);
      btSerial.println("LED OFF");
    }
} 

  // Send Data to HC05
  if (Serial.available()) {
    btSerial.write(Serial.read());
  }
}

What works:
I can send AT-Commands to the Uno via USB from Serial Monitor. I can rename the HC05 and stuff like that.
I can connect to the HC05 and send Data via Bluetooth Terminal Android App.

What doesnt work?
Shortly: The Data that I Send.

Sending Data from Serial Monitor to Android is weird. Sending any letter there displays an y with 2 dots above it in the Android App.
Sending Data from the Android App to the uno doesnt activate the LED or give the correct output. Sending an 0 gives me a few lines (0, 128, 120, 0, 128, 120, ), sending a 1 gives me following (120, 128, 120, 0, 128, 120). I think it has something to do with the Sending/Receiving, Bytes and that stuff. But every Tutorial i find looks like: Send a 0, check if data equals Char of 0 and then do stuff. So Why doesnt my Code work?

Thanks for answering
Dear
RPGFabi

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

You cannot supply power to the HC-05 directly from a digital Arduino pin. The pin cannot reliably supply the required current.

Ok, then I supply it via Transistor.

Can that be the reason for the weird Behavior?

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

What do you get when you power the HC05 with a transistor. Post the new code using a transistor along with the communication results.

Did you set the communication baud rate (as opposed to AT mode baud rate) of the HC05 to 38400?

What app is sending data to the HC05?

Why change code? Im just adding the transistor?

Baud rate Hc05 38400
Baudrate IDE 9600

Tried 2 Apps
1 for Bluetooth Transmission
1 specialy for HC05

May be, because the supply voltage for the HC-05 isn't stable.
To check this, it would be easiest to supply the HC05 directly with 5V.

Hi, @rpgfabi
Welcome to the forum.

Can you please post a schematic of your project?
A hand drawn circuit would be fine.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Adding that later/tomorrow, currently cooking.

Hand drawn is currently only the part for the thing that the project is planned for, not the communication.

Thats how its gonna look tomorrow. Implemented n channel Transistor.
Currently Pin 10 goes to Vcc and Hc05 gnd to uno gnD

Sorry for the wrong orientation, idk how to change that from my Phone

1. Follow Arduino convention and make connection between UNo and HC05 as per following diagram of Fig-1.

hc5x
Figure-1:

2. The default buad rate of HC05 is 9600. Do not change the Bd using AT Command. If you have changed it to some other value, then bring it back to 9600 as SUART (Software UART Port) does not work well above 9600 Bd.

3. Upload the following sketch in UNO (your one with some adjustments)

#include <SoftwareSerial.h>

SoftwareSerial btSerial(11, 12); //SRX=DPin-11 of UNO connects with TX of HC05; 

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

void loop()
{
  if (btSerial.available())
  {
    char data1 = btSerial.read();
    Serial.println(data1);
    if(data1 == '1')
    {
        digitalWrite(13, HIGH);    //L is ON
        delay(1000);
        digitalWrite(13, LOW);
        delay(1000);
    }
  }

  if (Serial.available())
  {
    char data2 = Serial.read();
    btSerial.println(data2);
  }
}

4. Pair BT and Android Phone.
5. Open BT Terminal at the Android Phone. Select LF as the terminating charcater.
6. Select "No line ending" for the "Line ending tab" of Serial Monitor (Fig-2). Enter A in the InputBox of the Serial Monitor and then click on the Send button.


Figure-2:

7. Check that A has appered on the BT Terminal of the Android Phone.
8. From the BT Terminal of Android Phone send B (in ASCII Mode).
9. Check that B has appeared on the Serial Monitor of UNO.
10. Send 1 from BT terminal of Android. Check that L of UNO has blinked once.

thought i read that 05 default bdrate is 38400 and 9600 is for 06

I purchased so many HC05s fro my pupils and they all worked well at default Bd = 9600.

For HC-05, 38400 is the required - not default - rate for configuration.
For HC-06 it is 9600.
Both can run OK @ 38400 under software serial, but running any faster is a bad idea. This is an Arduino problem, not Bluetooth.

I just tested the new circuit. Can't run it on 9600 => Had that problem before

Now I tested running AT Commands, but I somehow get no text back, only weird numbers. It should be OK to get back

OK, so here is the correc working thingy:

To work with AT Commands I need a Baud-Rate of 38400, Both NL & CR in Serial Monitor and press the HC05 Button while pluging 5V in.

The default Baudrate was indeeed 9600. To connect to it, I set it in Code to 9600, connected the Uno like @GolamMostafa said and now it works correctly.

Why are there so many not clear or false Tutorials out there?

Thank you all for your help.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.