Serial comunication between different arduinos

Hi my name is Diego, it's my first time here so i hope i can explain my problem in a good way.
I had problems with the serial comunication between an arduino nano and an arduino uno. I need to send some numbers from the nano to the uno, i wrote the code using serial.write() on tx and serial.read() on rx, in a couple different ways using string or just a single number but it doesn't work. Then i decided to switch tx/rx board so now i'm sending data from the uno to the nano, and it works!!! The problem is that in the final project i need to put on the uno a display shield that unfortunately uses a lot of pin, and on the nano a temperature/humidity sensor and an rf24 receiver module that also uses a lot of pin. I could just buy an arduino mega and solve all my problems but i already have one and i can't make the rf24 module working with it (maybe mine mega is defective). So my question is: why does the comunication from the uno to the nano works when the same code doesn't work if from the nano to the uno? Is there any solution?

Thank for your time and have a great day!

I would suggest to study Serial Input Basics to handle this

post your code using code tags </> ?
the rf24 should work on the Mega - remember that PINs 50, 51, 52, and 53 are used as SPI pins
if you could use Mega it would remove all the overhead and complexity of the serial communications

This can't have a reasonable explanation because the Uno and Nano are virtually the same board and therefore shouldn't be difference between Uno-> Nano and Nano -> Uno communication cases.
So you definitely make the error either in your connections or in your code.

Please post your code and a wiring diagram.

Here is the tx code

#include<dht.h>
#define dataPin 5;
dht dht11;
int temp,hum,temp2,hum2;
int gps= 111,sat= 22,vento= 333;
char a[20];

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

void loop() {
  
  dht11.read11(5);
  //if(dht11.humidity>0)
  {
  
  temp2= 12;
  hum2= 65;
    temp = dht11.temperature;
    hum = dht11.humidity;
    
    sprintf(a,"%d%d",hum2,temp2);
    
    Serial.write(a);
    
    //Serial.println(a);
   
    
    delay(1500);
  }
}

And here is the receiver code



char a[20];
int i,valu,temp,hum,temp2;
int gps,sat, vento;


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

void loop() 
{
  if (Serial.available()>0) 
  {
    i=0;
    while(0 <Serial.available())
    {
      a[i] = Serial.read();
      i++;
    }
  }
String s=String(a);
Serial.println(a);


  delay(1500);
}

hope u guys can understand were is the mistake, because i have tried a lot of things and it still work only if the nano is receiving data :frowning:

It still could be a wiring issue but you didn't show your wiring diagram. However, I see other issues:

  1. You have provided no way to indicate the begin or end of the message you are transmitting. Unless your transmitter and receiver are perfectly synchronized you are likely to receive only fragments of messages.
  1. See below. How do you know where one value ends and the other begins? You should put a space, tab, or comma between the values.
  1. See below. If you detect that one or more bytes are available but they all haven't been transmitted then you will read a partial message. The next time through loop you will read the rest of the message but overwrite the first part of the message. This is related to not knowing where the message begins and ends.

You should add some type of delimiter to indicate the beginning and/or end of the message and code in the receiver to look for the delimiter(s). You should remove the delay from the receiver so that you don't miss any characters.

Ok thank you, i'll implement those features to improve the comunication.
Btw i think i found the issue: i had some of the rf24 pins still connected to the arduino nano, i already had disconnected the 3.3v and the ground but evidently that wasnt't enough. Now with olny the dht11 sensor connected to the nano it is able to send data to the uno.
But now i wonder how sould i connect the rf24 preventing serial comunication issue. The rf24 on my setup use this pin: D7 -> CE, D8->CSN, D11->MOSI, D12->MISO, D13->SCK, and vcc to 3.3v and gnd to gnd
I'm using this libraries for the rf24:

#include <SPI.h> 
#include <Wire.h>
#include "RF24.h" 

Is there a way to get data from the rf24 and on the same board send them via serial to another arduino?
If not i'll order a new arduino mega hoping that the rf24 will work on it.
Thanks to everybody for your help, really appreciated.

on the Mega PINs 50, 51, 52, and 53 are used as SPI pins

So you power with 3.3V but digital signals are 5V? Is the module OK with that?

I don't know why it is like this. The datasheet says that powering it with 3.3v this is the correct way, if u give 5v to power the rf24 will burn.

Is it possible (in the nano in my case) that because the rf24 is using the SPI comunication the serial output via tx /rx is not working?

Post a link to your module

Sould be this one, the name is "Pixnor NRF24L01+"

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.