Serial output speeds different in PlatformIO (sendonlysoftwareserial)

I am using PlatformIO to program and debug an ATTiny85 script, connecting using an usbASP programmer to upload and a USB to serial board (CP2102 USB-UART) to read serial output from the script. SendOnlySoftware serial is used to send output to pin PB3.

As no output was detected I loaded the simple SendOnlySoftware example script to the Arduino IDE (2.3.2), which worked as expected. The output is seen when the monitor’s baud rate matches that specified in the code.

Upon loading the same script into PlatformIO, it didn’t work. After some detective work, I found that I had to specify the serial rate in the script to be 1/2 that specified for the serial monitor in the platformio.ini file.

The same behaviour is seen whether monitoring via serial monitor in the IDE or using cat /dev/ttyUSB0 in a terminal window. The serial port seems to be running at twice the rate of the specified output from SendOnlySoftwareSerial.

While this works, it seems odd, so I was wondering if anyone has an explanation and a better solution.

The working script and ini files are below. Notice that the value passed to begin() is 1/2 monitor_speed.

main.cpp

#include <Arduino.h>
#include <SendOnlySoftwareSerial.h>

SendOnlySoftwareSerial mySerial(PB3);  // Tx pin

void setup()
{
  mySerial.begin(BAUD/2);
}

int i;

void loop()
{
  mySerial.print ("test: ");
  mySerial.println (i++);
  delay (100);
}

PlatformIO.ini

[env]
monitor_speed = 19200
platform = atmelavr
framework = arduino
upload_flags = -e
upload_protocol = usbasp
debug_tool = simavr
build_flags = -D BAUD=${env.monitor_speed}


[env:attiny85]
board = attiny85
board_build.mcu = attiny85

Then some clock setting is wrong. WIth your current config, the standard value of

applies so PlatformIO and the Arduino framework think the chip runs at 8MHz. Different fuse settings or crystal oscillator though might have the chip running at 16MHz, at twice the speed, thus producing the serial signal at twice the speed, causing you to have to compensate for it by dividing by two.

What is the clock speed setting in the Arduino IDE? (Arduino IDE → Tools). Add

board_build.f_cpu = 16000000L

if needed to the platformio.ini.

The clock setting in the Arduino IDE is 16Mhz.

Compiling with the board_build.f_cpu line fails with an error Custom tuning is not supported in the current version of ATTinyCore.

Setting the chip’s clock speed to 8 MHz, fixed the problem.

Thanks.

Then this sounds like

Can you revert back to 16MHz with these suggestions?

Thanks for the link. Setting build_flags = -DCLOCK_SOURCE=6 let me set the chip’s clock to 16Mhz. And all works as expected