1

I have a problem which I have been working on for some time. I have an Arduino Uno board and an HC-05 Bluetooth transceiver with TTL outputs.

The connections are as follows:

RX (HC_05)  --> TX (Arduino UNO)

TX (HC_05)  --> RX (Arduino UNO)

GND (HC-05) --> GND (Arduino UNO)

+5V (HC-05) --> +5V (Arduino UNO)

I have the following Arduino code:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX

void setup()
{
  Serial.begin(9600);
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
  pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  pinMode(10, INPUT);
  pinMode(11, OUTPUT);
  
  digitalWrite(9, HIGH);
  Serial.println("Enter AT commands:");
  BTSerial.println("Welcome to ARBA-Beat");
}


void loop()
{

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

I connect to the Bluetooth module through the Bluetooth Terminal Android app. Everything works fine (even the lights on the Bluetooth module). But when I send a character from the phone to Arduino, I get the following output:

Text sent to Bluetooth module - a

enter image description here

please help

thank you

3
  • You are calling BTSerial.read() twice in different functions which may mean that two values are being taken out of the queue. Try storing it in a variable before echoing to terminal/doing something with the value. Commented Sep 29, 2017 at 3:59
  • @MorrisonChang i tried whatever you said, now i am getting box shaped values Commented Sep 29, 2017 at 4:02
  • I would advise taking the value and transforming it into hex/binary and sending that to any debug/log session you are using to see if you are really get the value you send or have something else going on (extra characters/big-little endian problem/crlf issue). If you are following a tutorial - you may want to link to it and point out what step is failing. Commented Sep 29, 2017 at 4:08

1 Answer 1

0

I had the exact same issue. Have you tried running the HC-05 at 9600? Try the code below (with your pins). I used the code to switch a relay on pin 2, but you could use an LED if you want. I used the same Bluetooth app and it worked fine:

#include <SoftwareSerial.h>
int relay = 2; // Set pin for relay control

SoftwareSerial bleserial(8,9);

// setup the relay output and the bluetooth serial, and the serial monitor (if you want to print the outputs)
void setup() {                
  // set relay pin as output.
  pinMode(relay, OUTPUT); 
  // start bluetooth and serial monitor  
  bleserial.begin(9600);
  Serial.begin(9600);

}

void loop() {

  if(bleserial.available()){

    char char1 = bleserial.read();
    Serial.println(char1);
    // Set protocol that you want to turn on the light bulb, I chose 1 and 0 as on and off, respectively

    if(char1=='1'){
      Serial.println("ON");
      digitalWrite(relay,LOW);
    } else if (char1=='0'){
      digitalWrite(relay,HIGH);
    }
  }

}

If you want to see wiring etc. check out the entry on my blog:

https://engineersportal.com/blog/2017/11/15/bluetooth-home-automation-light-bulb-switch-using-an-hc-05-a-relay-and-arduino

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.