How to have reliable, fast and continous communication between two arduinos using serial

Hi!

I am making a new drone once again and one of my conclusions of my previous drone was that it was a little bit to slow to handle PID, radio and everything else, so my plan this time is to have two arduinos, one that recives the data from my remote, and one that does all the logic and gets the data from the IMU.

I have tried this method of data transfer from my reciver to the main arduino:

float toSendDataArray[5]; // Change the size and data types as needed

void setup() {
    toSendDataArray[0] = 1000.0; // Use floating-point values instead of strings
    toSendDataArray[1] = 0.0;
    toSendDataArray[2] = 0.0;
    toSendDataArray[3] = 0.0;
    toSendDataArray[4] = 1.0;
    Serial.begin(115200);
}

void loop() {
    Serial.write((uint8_t*)toSendDataArray, sizeof(toSendDataArray));
    delay(10);
}

And this on the main arduino:

if (Serial.available() >= sizeof(recivedDataArray)) {
        Serial.readBytes((char*)recivedDataArray, sizeof(recivedDataArray));
        previousMessageMillis = millis();

        // Print the received data
        for (byte i = 0; i < sizeof(recivedDataArray) / sizeof(float); i++) {
            Serial.print(recivedDataArray[i], 2); // Print each float value with 2 decimal places
            Serial.print(" ");
        }
        Serial.println(); // Print a newline for formatting
    }

But the result of the transfer is the following:

0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 
766.00 0.00 0.00 0.00 1.00 
0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00 

I dont know why and how I only get the written data sometimes, is there a missmatch/desync between the readbytes and the serial write maybe?

Thanks in advance!
Best regards Max

A classic beginner error. 2 Arduino will make your problems worse because of the extra complexity and overheads of getting them to communicate.

If you can't write your code more efficiently, use a single, faster Arduino.

Two UNOs?

Yes. You send the 4 byte floats as binary and prints out as characters. Study how different data types are represented.

I am using two arduino pro mini 5v

Isnt it even more weired that it works every now and then then?

What do you recommend as a faster arduino? Now I am using two pro mini 5v? What alternativs are there that arent massivly big. I have a Mega and some Unos, are those the only options?

You have the expected result, because the data stream has no means of synchronization. See the Serial Input Basics tutorial on this form.

Plus, there are problems in the code you forgot to post.

There are so many to choose from! Check out the Arduino web shop. Check out the Adafruit web shop.

Check out the PJRC website for Teensy products as well.
https://www.pjrc.com/teensy/

2 Likes

It doesn't. Know what data type You send and read the same type.

Could an option be to send everything as chars and then convert it to a float?

A question to the proffesionals, so I have always had a hard time with the NRF24 communication, it doesnt seem super reliable, on the other hand, is HTTP an option? To have some kind of SIM card and use a webserver on the arduino to have event based controlling of the drone? What are your thoughts?

Hello lordmax2

For a long time I have been following the topic with the use of this type of wireless module and the associated software functions.

For wireless projects I recommend the use of the HC12 module.

What are the advantages:

  1. no external functions from a library are needed.
  2. the development of a communication protocol is in your hands
  3. the necessary development steps can be programmed and tested without using the wireless module
  4. the radio module can be easily configured for the project requirements
  5. both transmitter and receiver are contained in one radio module.

hth

Have a nice day and enjoy coding in C++.

1 Like

You said you were going for speed. This would be slower would it not? There would be more bytes to transmit.

What happened to wanting it to be fast? None of this sounds fast at all.

This is true. Making the system more complicated will make it slower, not faster. Perhaps you should let someone help you to improve that other code before you go and split it to two MCUs and make it worse.

That would be easier to handle and debug. Remember to use markers telling "end of float" after each float.

What needs to be fast is the main arduino controlling the ESCs and reading from the IMU.

My code from my previous attempt is: GitHub - Hickori/ArduinoDrone

The drone worked "okay", but the radio lost connection every now and then. The stabilization often worked but my hypotheses is that when things started to take a lot of time it started wobbling every now and then. But can ofcourse also be my PID that is not calibrated perfecly.

What have you done to test that hypothesis?

You know what they call it when you start developing off of an untested hypothesis don't you? It's called a waste of time. You could be making the problem worse or not affecting it at all because you don't know what the problem is. Save yourself some time and frustration and make sure that this is even your problem before you go and do a bunch of work that may be a waste.

Okay I will give it a try again with 1 arduino, thanks!

No, much too slow.

+1 for the HC12 suggestion from @paulpaulson . Excellent range and simple to use.