1

I plug in BT rx to digital 11, BT tx to digital 10 and then 5v and ground from the HC-05 to the arduino. I then upload this code to the HC-05 that is set as a master.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  Serial.begin(57600);
  mySerial.begin(9600);
}

void loop() { // run over and over
  Serial.println(mySerial.read());
  delay(2000);
}

I then upload this code to the arduino with the HC-05 that is set as a slave:

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


void setup() {
  // put your setup code here, to run once:
  mySerial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  mySerial.write("100");
  delay(2000);
}

The issue is that I never receive "100" on the serial monitor, I only ever receive 49 and occasionally 48. When I changed it from "100" to "10" same thing happened. Any help would be appreciated.

  • UPDATE: I changed "100" to "hello" and now I'm getting values like 104, 101, 111, 108. I read something that the bytes may be getting mixed up because of how long or short a delay is. – Gxs1619 May 22 '18 at 19:06
  • 2
    49 is the decimal ASCII value of string 1 .... 48 is the decimal ASCII value of string 0 .... 104=h, 101=e, 108=l` etc. etc. ........... asciichart.com/ascii_decimal.html – jsotola May 22 '18 at 19:07
  • Guessing I can't send strings over bluetooth then – Gxs1619 May 22 '18 at 19:09
  • 1
    you already are, the data being transmitted is always a binary value which has a decimal equivalent and an ASCII equivalent ..... just convert the ASCII values to string .... lot of places on web show you how – jsotola May 22 '18 at 19:10
1

SoftwareSerial.read() returns a single character as an integer. When you print an integer the print() functions convert it to a human readable format.

You want to covert (cast) the output of read() to the char type. Either:

void loop() {
  char c;
  c = mySerial.read(); // store the result in a temporary variable c
  Serial.println(c);
  delay(2000);
}

or

void loop() {
  Serial.println((char)mySerial.read()); // cast the type to char befor printing.
  delay(2000);
}    

However since only one character at a time is being read your output will be one character per line (assuming no data loss due to the delay):

1
0
0
1

"You are getting 49 and 48 occasionally"?

I would expect you to get 49, 48, 48, 49, 48, 48 ...

49 is '1' in ASCII and guess what 48 means :)

This is because you are writing and reading bytes not characters. There is a very subtle difference.

change you receiver to this and it should make more sense.

void loop() { // run over and over
  const char letter = (char)mySerial.read(); // Turn the byte into an ASCII letter.
  Serial.println(letter);
  delay(2000);
}

It may all still go wrong, if you read <32 or >126 but ...

Here is an ASCII table to help: ASCII Table

-1

You should change the TX and the RX pins on the arduino board and pin it on to another pin other than the TXD and RXD as the are hardware pins....

  • The OP says they are using 10 and 11. – Code Gorilla May 24 '18 at 12:47

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.