SoftwareSerial with Nano works with Arduino IDE but not with PlatformIO:

I am using the standard example for SoftwareSerial with an Arduino Nano and a HC-05 Bluetooth Module

My problem is that everything works fine with Arduino IDE but not with PlatformIO:

I can send data from the PC Monitor via Nano to the mobile phone.
But when I send data from the mobile phone, I get rubbish on the PC Monitor.

Are there any settings I am missing?
Are there any restrictions in PlatformIO I am not aware of?

My Hardware:
Arduino Nano
AZ-Delivery HC-05 (TX to D12, RX to D11 via resistors)

My sketch:

#include <Arduino.h>
#include <SoftwareSerial.h>

const int TX_HC05RXD = 11;
const int RX_HC05TXD = 12;

SoftwareSerial btSerial(RX_HC05TXD, TX_HC05RXD);

void setup() {
	Serial.begin(38400);
	btSerial.begin(38400);

	while (!Serial)
	{
		; // wait for serial port to connect. Needed for native USB
	}

	Serial.println(F("BT Test"));
	btSerial.println(F("BT Test"));

}

void loop2() {
  if (btSerial.available())
    Serial.write(btSerial.read());
  if (Serial.available())
    btSerial.write(Serial.read());
}

void loop3 () {

	if (btSerial.overflow())
    Serial.println(F("ERROR - Bluetooth overflow!"));

  while (btSerial.available()) { // BT > PC
    Serial.println(F("receive start"));
    String temp = btSerial.readString();
    Serial.println((String)temp);
    Serial.println("receive end");
  }

  while (Serial.available()) {   // PC > BT
    String temp = Serial.readString();
    btSerial.println((String)temp);
  }

}

void loop() {
	loop3();
}

My config:

[env:nanoatmega328]
platform = atmelavr
board = nanoatmega328
framework = arduino
monitor_speed = 38400
lib_deps = featherfly/SoftwareSerial@^1.0

My output on PC Monitor:

BT Test
receive start
f����XZ�Sդ�  <- That should be "from phone". I can see that with ArduinoIDE
receive end

My output on “Serial Bluetooth Terminal” on Phone:

BT Test
from phone
from PC

The (String) cast is redundant but okay.

No. That library is a fork of https://github.com/PaulStoffregen/SoftwareSerial . But in the Arduino IDE, you’re very likely using the built in software serial, which is a different implementation that can very well behave differently. So, no 1:1 comparison can be done between PIO and the Arduino IDE if you use different libraries…

Solution:

  1. Remove lib_deps = ... from your platformio.ini
  2. Delete the .pio folder of your project
  3. Build + Upload again
1 Like

Yes!
Thank you. That solved the problem!