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);
}
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.
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);
}
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.