Serial between two arduino UNO

Hi i am trying to comunicate two arduinos using Serial pins. I can see the bytes im sending when I reset the arduino, but sudenly the message received is always the same has nothing to do with the bytes I am sending.
The conections are just rx on arduino 1 to tx on arduino 2, and tx arduino 1 to rx on arduino 2, and a cable to ground of both.

This is the code for the sender:

const byte nitro[] = {0x01,0x03, 0x00, 0x1e, 0x00, 0x01, 0xb5, 0xcc};

void setup() {
  Serial.begin(9600);
  Serial.println("serial has started");
  delay(3000);
}

void loop() {
  Serial.println("values ti be written : ");
  for(int i=0; i<sizeof(nitro);i++){
    Serial.print(nitro[i],HEX);
  }
  Serial.println("");
  Serial.write(nitro,sizeof(nitro));
  Serial.println("");
 delay(1000); 
}

This is the code for the receiver:

byte message[8];
byte start[4]={0x01,0x03, 0x00, 0x1e};
byte m_buffer[4];
void setup() {
  Serial.begin(9600);
}

void loop() {

 if(Serial.available()>0){
 Serial.readBytes(message,8);
 }
 if(message[0]==start[0]){
  Serial.println("NUEVO MENSAJE:");
  for(int i=0;i<sizeof(message);i++){
    Serial.println(message[i],HEX);
  }
 }
 else{
  Serial.println("No se enconntro mensaje");
  for(int i=0;i<sizeof(message);i++){
    Serial.print(message[i],HEX);
    message[i]=0;
  }
  Serial.println("");
 }
 delay(1000);
}

And this is the message that Im receiving:
616C756573207476

If you want ASCII, look up the difference between “types” byte and char.

The serial input basics tutorial may be of interest.

Did you want ?

  for (int i = 0; i < sizeof(nitro); i++)
  {
    //Serial.print(nitro[i],HEX);   
    Serial.write(nitro[i]);
  }

@arturovelasquez

It was difficult for me to reproduce the fault of your sketch of post #1 with UART connection between two Arduinos. It is not recommended to use the UART Port for data exchange as these ports remain engaged with PC/IDE/SM.

I have connected UNO and NANO using Software UART (SUART) Port as per Fig-1 and then have uploaded your sketches (with slight modifications) to receive message from Sender correctly.
uartUnoNano-1
Figure-1:

Sender Sketch:

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3); //SRX = 2, STX = 3

const byte nitro[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xb5, 0xcc};

void setup() {
  Serial.begin(9600);
  SUART.begin(9600);
  Serial.println("serial has started");
  delay(3000);
}

void loop() {
  Serial.println("values ti be written : ");
  for (int i = 0; i < sizeof(nitro); i++) 
  {
    Serial.print(nitro[i], HEX);
  }
  Serial.println("");
  
  SUART.write(nitro, sizeof(nitro));
  delay(1000);
}

Receiver Sketch:

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3); //SRX = 2, STX = 3

byte message[8];
byte start[4] = {0x01, 0x03, 0x00, 0x1e};

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

void loop()
{
  if (SUART.available() > 0)
  {
    SUART.readBytes(message, 8);
    if (message[0] == start[0])
    {
      Serial.println("NUEVO MENSAJE:");
      for (int i = 0; i < sizeof(message); i++)
      {
        Serial.print(message[i], HEX);
      }
      Serial.println();
    }
    else
    {
      Serial.println("No se enconntro mensaje");
    }
  }
}

Receiver Output:

NUEVO MENSAJE:
1301E01B5CC
1 Like

Isn’t the serial on UNO is the same serial you connect to the monitor?

That worked perfectly. I still don't understand why it dosent work well with pins o and 1.
I thought those were the ones desingned for this type of transmission. The project I am trying to build includes a NPK sensor that needs conversion from RS485 to TTL. I thought that connecting the conversion circuit to the UNO's tx and rx pins was the correct thing to do, but since I could not get any signal I simplified to just verify I could read and writte correctly using serial comunication. For future reference: should I avoid using pins 0 and 1 for comunication?

Hope that the following diagram (Fig-1) will help to visualize the stuff that are connected with Hardware UART Port of the ATmega328P MCU of the UNO Board.


Figure-1:

If you are making UNO-based project, then leave the UART Port for down loading/debugging the sketches.

1 Like

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