Data Transfer via SoftwareSerial (different Bautrates?)

Hello,

I am not a native english speaker and a complete beginner in regards to arduino, so please understand any weird phrasings.

I am trying to send data from an Arduino Nano Clone to an Arduino Uno Clone .
So the next step was to send the data using SoftwareSerial, but it didn't work as planned:

example Serial Monitor for Nano to Uno:

Transmitter:

-> Sent data: 23.34
-> 2334
-> 1E
-> 9

Receiver:

-> Received data: 7168
-> 71.68
-> 0
-> 1C
-> Received data: 57344
-> 573.44
-> 0
-> E0

When I use the same code with two Arduino Nano clones (same type), everything works as intended.

Which Nano am I using?

LGT-RF-Nano (using MiniCore Board Manager / Atmega328)

Which Uno am I using?

Seems to be the original one from Arduino itself.

Assumption: Problem narrowed down

Nano clone - works with a baud rate of 9600 in the code and 2400 in the serial monitor
Uno clone - works with a baud rate of 9600 in both the code and the serial monitor

I know that SoftwareSerial usually requires the same baud rate on both devices, but I do not know how to proceed from this point on...

Transmitter:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

double dataToSend = 23.34;

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

void loop() {
  // Break down the int into low and high bytes
  uint16_t dataToSend1 = dataToSend* 100;

  uint8_t highByte = (dataToSend1 >> 8) & 0xFF; // Extract high byte
  uint8_t lowByte = dataToSend1 & 0xFF;        // Extract low byte

  
  // Send the low and high bytes over SoftwareSerial
  //mySerial.write(&highByte, sizeof(highByte));
  //mySerial.write(&lowByte, sizeof(lowByte));
  mySerial.write(highByte);
  mySerial.write(lowByte);

  Serial.print("Sent data: ");
  Serial.println(dataToSend);
  Serial.println(dataToSend1);
  Serial.println(lowByte, HEX);
  Serial.println(highByte, HEX);
  
  delay(1000);
}

Receiver:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

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

void loop() {
  if (mySerial.available() >= 2) { // Ensure at least 2 bytes are available
    
    // Read low and high bytes
    uint8_t highByte = mySerial.read();
    while (!mySerial.available()) {}
    uint8_t lowByte = mySerial.read();

    // Combine bytes into an int
    uint16_t receivedData2 = ((uint16_t)highByte << 8) | lowByte;

    // Convert the integer back to the original float value
    double receivedData1 = receivedData2 / 100.0;
    
    Serial.print("Received data: ");
    Serial.println(receivedData2);
    Serial.println(receivedData1);
    Serial.println(lowByte, HEX);
    Serial.println(highByte, HEX);

    // delay(1000);
  }
}

If anything is missing, please tell me.

On the receiver, remove the delay from the loop.
I haven't checked the rest.

1 Like

Hi, thanks for the first response.
Removing the delay did not solve the problem.

As I mentioned, I think it is a problem regarding the baud-rate because it usually needs to be the same? - I am just guessing.

You need to install the board package for the LGT micro (runs at 32MHz)

Use it to complie the code for the Nano clone.

2 Likes

Your code shows it is the same. 9600 on master and slave.
The mySerial.write() call sends binary data.
If the data gets out of sync, you have no way to get back.
I would send text with a linefeed at the end.

1 Like

Once you complie the code for the LGT using the LGT package, the baud rate will be correct

2 Likes


I can not upload Code while using this board manager.

Change the upload speed to 115200

1 Like

You are literally my hero. Now it works.
Thank you very, very much.

The LGT8F328 is a great processor, it run at twice the speed of a regular 328, has a 12 bit ADC, 8 analog inputs, a DAC, more timers and a lot more.
Have fun!

1 Like

I moved your topic to an appropriate forum category @confusedbeginning .

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like