ATTiny85 Software Serial wrong ascii characters received

I am working on a project in which my ATTiny85 (slave) will receive single characters over bluetooth that correspond to a command. If a 1 is received, the led turns on. If a 0 is received, it turns off. Here is my slave code:


#include <SoftwareSerial.h>    

#define RxD 0
#define TxD 1

SoftwareSerial blueToothSerial(RxD,TxD);

int led = 4;
 
void setup() 
{ 
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();
  
  pinMode(led,OUTPUT);
} 
 
void loop() 
{ 
  int recvChar;
  while(1){
    if (blueToothSerial.available()){
      digitalWrite(3,HIGH);
      recvChar = blueToothSerial.read();
      if (recvChar == 113) {
        digitalWrite(led, HIGH);
      } else if (recvChar == 114) {
        digitalWrite(led, LOW);
      }
    } else digitalWrite(3,LOW);
  }
} 
 
void setupBlueToothConnection()
{
  blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=HC-05\r\n"); //set the bluetooth name as "HC-05"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  
  delay(2000); // This delay is required.
  
  blueToothSerial.print("bluetooth connected!\n");
  
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}

The strange thing is that when receiving bytes over bluetooth, the character encoding seems to be incorrect. When I receive a '0' its value is 112 instead of 48 (ASCII). When I receive a '1' its value is 113 instead of 49. Interestingly, when I load the exact same code onto an arduino nano, '0' is 48 and '1' is 49, as they should be. Why might this be? Please note that I have tried this with multiple ATTinys.

I am using the ATTiny85 with a 1mhz clock. If I can provide any additional information please let me know.

Most probably the baudrate is wrong.

Thank you for the reply. Can you guide me to a resource on choosing the optimal baudrate? I am unsure. I would only be sending one byte per second maximum. I am using the HC-06 bluetooth module.

I set the clock to 8mhz and now it works just fine. Thanks for the help, you put me on the right track @DrDiettrich

Fine :slight_smile: Otherwise increase the baudrate in the library 8 times for 1/8th (1MHz) clock.

BTW it should read MHz. The M stands for mega (10^6, million), while m stands for milli (10^-3, thousandth), and Hz stands for the frequency inventor Heinrich R. Hertz.