Search code examples
bluetootharduinosoftware-serial

Receiving multiple chars at once with Software Serial


I have a Arduino Uno R3 and a Bluetooth Mate. When linking the Mate to the Arduino Hardware Serial (pin 0,1) I can send multiple characters at once from my connected device but when I try to make the same thing with Software Serial (using pin 4,2 for example) I only get the first character and the rest of the chars are messed up.

My code:

#include <SoftwareSerial.h>  

int bluetoothTx = 4;  
int bluetoothRx = 2;  

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() 
{
  Serial.begin(115200);  
  bluetooth.begin(115200);  
}

void loop()
{
  if(bluetooth.available())
  {
    Serial.print((char)bluetooth.read());  
  }
}

For example if I send this chars: abcd from my android device I get this in the serial monitor: a±,ö

This code that uses Hardware Serial (I link my bluetooth to pins 0 and 1) works just fine:

void setup()
{
  Serial.begin(115200);  
}

void loop()
{
  if(Serial.available())
  {
    Serial.print((char)Serial.read());  
  }
}

I even tried changing the baud rate but it didn't helped

If I send the chars one by one it works fine but I would like to be able to send them as a string.


Solution

  • As pointed out by @hyperflexed in the comments this is a baudrate related issue. I had to take the baudrate as low as 9600 to make it work.

    This is the code that worked:

    #include "SoftwareSerial.h";
    int bluetoothTx = 4;
    int bluetoothRx = 2;
    
    SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
    
    void setup()
    {
      Serial.begin(9600);
      delay(500);
      bluetooth.begin(115200);
      delay(500);
      bluetooth.print("$$$");
      delay(500);
      bluetooth.println("U,9600,N");
      delay(500);
      bluetooth.begin(9600);
    }
    
    void loop()
    {
      if(bluetooth.available()) {
        char toSend = (char)bluetooth.read();
        Serial.print(toSend);
      }
    
      if(Serial.available()) {
        char toSend = (char)Serial.read();
        bluetooth.print(toSend);
      }
    }
    

    For changing the baudrate I had to put some big delays to make sure the commands are executed otherwise it won't work.