Arduino Bluetooth Module

I have a problem I am using Arduino Uno to Communicate with the Bluetooth module version HC - 05. It is connecting to the phone but not receiving anything else but -1 which I guess is Standard. I looked up all the questions but not getting anything. Here is my code:

#include <SoftwareSerial.h>
SoftwareSerial bt(2, 3);
int LED = 2;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  bt.begin(9600);
  digitalWrite(LED, LOW);
}

String data;

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() > 0) Serial.println(bt.read());
    if(bt.read() == 1){
      digitalWrite(LED, HIGH);
      Serial.println("LED IS ON");
    }
    else if(bt.read() == 0){
      digitalWrite(LED, LOW);
      Serial.println("LED IS OFF");
    }
}

Then my attachments are …
On HC - 05 module :

  • RXD - Digital Pin 2
  • TXD - Digital Pin 3

I think you will be receiving more complex data than just a '0' or a '1' , The HC-05 can be either a slave or a master (i have only an HC-06 myself which can only be a master i think) anyway i suggest you switch it to AT mode and play with that a bit Check this you can either send the commands from your sketch or use a Serial pass-through (or even connect it to the hwSerial directly and use the Arduino as a USB -> Serial)

What is the logic of checking if Serial has anything available and then reading bt (SoftwareSerial) without checking if that has anything available?

A return of -1 from a read means nothing has been received for you to read. So perhaps you should check whatever is trying to send data to the Arduino.

Steve

Hello PR7.

A few thoughts.

When software serial is declared, the first pin is the one that receives data and the second is the one that transmits data. I remember this by the alphabetical order: R comes before T.

The BT pin that transmits data (labelled TX) needs to be connected to the Arduino pin that receives data (your pin 2).

The BT pin that receives data (labelled RX) needs to be connected to the Arduino pin that transmits data (your pin 3).

The labels TX & RX on the BT board relate to the wired connection to the Arduino board, not the wireless connection to the phone.

Remember that reading a byte from the serial port removes that byte from the receiving buffer. It can't be read a second time.

What data is the BT sending?

A BT serial terminal app communicating with the Arduino IDE monitor is a simple way to test the setup.

John.

Why are you even trying to use Pin 2 for both Bluetooth SoftwareSerial and LED?!?

I think you have wired Bluetooth the wrong way round. It should be RxRx, and get that stupid LED out of the game. If you insist on using a LED, you might consider that on pin 13. I respectfully suggest you studiously ignore reply #1. The last thing you want to do is fiddlearse around with AT commands.

You might find the following background notes useful.

Ignore this has been fixed and also thanks this has been fixed :smiley: . Sorry for all your trouble

1. Check that the connection between UNO and HC-05 agrees with Fig-1. You need voltage divider as the RX-pin of BT is not 5V tolerant.
hc5-1y.png
Figure-1:

2. Upload the following sketch and check that whatever character you send from your Android Phone, it appears on the Serial Monitor.

#include <SoftwareSerial.h>
SoftwareSerial bt(3, 2);  //SRX-pin of UNO = DPin-3; STX-pin of UNO = Dpin-2

void setup() 
{
  Serial.begin(9600);
  bt.begin(9600);
 }

void loop() 
{
  byte n = bt.available();
  if (n != 0)
  {
    char x = bt.read();
    Serial.print(x);
  }
}

hc5-1y.png

GolamMostafa:
1. Check that the connection between UNO and HC-05 agrees with Fig-1. You need voltage divider as the RX-pin of BT is not 5V tolerant.

That schematic doesn't look right to me. Shouldn't the low end of the voltage divider be GND and not 5V/Vcc? The way it is shown the module's RX pin will be 5V when TX is HIGH and 1.6V when TX is LOW.

johnwasser:
That schematic doesn't look right to me. Shouldn't the low end of the voltage divider be GND and not 5V/Vcc? The way it is shown the module's RX pin will be 5V when TX is HIGH and 1.6V when TX is LOW.

You are right Chieftain. The diagram is corrected in Post#7. Thanks.

Now I have a problem that whenever I attach a motor driver I get readings of different values which is supposed to be 0, 1, 2, 3 is now coming 49, 50, 51, 52 what should I do?

It works normally without the motor driver. Thanks for your help.....

It would appear that this has nothing to do with Bluetooth, so start a new thread.

Nick_Pyner:
It would appear that this has nothing to do with Bluetooth

I too believe so but do you think it might be a problem with the Motor Driver?

Quite possibly. It certainly has nothing to do with Bluetooth, hence my comment.

Ok

So what should I do now... Like is it a problem with the Arduino or the wiring?