Using TX3 and RX3 ports

Hello ,

The code below does not work. What is the mistake

void setup() {
  // put your setup code here, to run once:
Serial3.begin(9600);

}

char rx_byte = 0;

void loop() {
  // put your main code here, to run repeatedly:
if(Serial3.available())
{
  rx_byte = Serial3.read();
  Serial3.write (rx_byte);
}
}

Instead of using Serial3 , if I use Serial it works. How can I enable the Serial3 ports ?

chutki:
The code below does not work. What is the mistake

Explain exactly what you mean by "does not work".

In the serial monitor, In the serial command box if I type anything , it should be echoed . It happens with Serial but echoing does not happen with SErial3

Do you have a USB to serial adapter connected to the RX3 and TX3 pins?

Did you select the COM port of that USB to serial adapter from the Tools > Port menu?

Oh thank you so much ! I did not know that I have to Open COM3. It now works thanks a lot. I have another question , how can I change the clock frequency in DUE. Its default is 4 mhz. I want it at 16mhz

pert:
Do you have a USB to serial adapter connected to the RX3 and TX3 pins?

Do you have a USB to serial adapter connected to the RX3 and TX3 pins?

Now I have connected DUE to a sensor. The sensor works with UART communication. So i have connected the tx-rx and rx-tx respectively. The sensor values are read on Serial port. For, serial3 port, I donot have a adapter instead I have connected reset and ground . I tried doing the same as serial port, but I cannot read anything from sensor on serial3 .

It's still not clear what you're trying to do. Please provide more details. I know you know what you're talking about but try to read it from the perspective from someone not familiar with your project, who can't see how you have things wired.

Sorry for the inconvenience. I have attached a picture of my wiring.

  1. My sensor and DUE are connected via UART as like in picture and I read out the values of sensor via UART and get it displayed on the serial monitor.
  2. Just like I connected with Serial port (Tx0 and Rx0) , I simply want to do the same as for Serial3 port like Tx3 and Rx3.
  3. I don not have USB - serial adapter , so I found this Use an Arduino UNO as a USB-to-Serial converter. - Project Guidance - Arduino Forum , I followed the post #1

OK, so you're actually just using the Due as a USB to serial adapter. For that purpose you should connect sensor RX to Due's Rx0 and sensor Tx to Due's Tx0. I know that seems wrong at a first glance but the pin labels are relative to the SAM microcontroller pins but you're not using the microcontroller, you're using the ATmega16U2 USB-serial chip, which is connected to 0 and 1 opposite to the labels on the pins (because it's connected to the SAM microcontroller RX-TX, TX-RX). So when you make the RX-RX, TX-TX connections between the due and the sensor you're actually doing Rx-Tx, Tx-Rx with the ATmega16U2.

chutki:
I simply want to do the same as for Serial3 port like Tx3 and Rx3.

Is that because you have two sensors? You won't be able to do the "Due as USB-serial adapter trick with Serial3 because only Serial is connected to the ATmega16U2. For Serial3 you will need to do a standard Rx-Tx, Tx-Rx connection with the SAM microcontroller, don't hold it in reset, and upload a sketch that transfers data between Serial3 and Serial.

I wanted to know how to use the Serial3 port , there is only one sensor. So, the reset and ground connection should be removed and serial 3 to serial program is required. Ok fine. Thanks fr the clarity.

pert:
OK, so you're actually just using the Due as a USB to serial adapter. For that purpose you should connect sensor RX to Due's Rx0 and sensor Tx to Due's Tx0. I know that seems wrong at a first glance but the pin labels are relative to the SAM microcontroller pins but you're not using the microcontroller, you're using the ATmega16U2 USB-serial chip, which is connected to 0 and 1 opposite to the labels on the pins (because it's connected to the SAM microcontroller RX-TX, TX-RX). So when you make the RX-RX, TX-TX connections between the due and the sensor you're actually doing Rx-Tx, Tx-Rx with the ATmega16U2.

If suppose I want to use the Serial0 port as USB to serial adapter(I would also send some variables from the serial terminal window , that is transmission and reception to be performed on the serial terminal) , in the sketch will this be enough ?

void setup() {
  // initialize serial ports
  Serial.begin(38400);
    // USB serial port 0
}

char read_byte = 0 ;        // stores received byte


void loop() {

   //testread
  // check for data byte on USB serial port
  if(Serial.available()) {
    // get byte from USB serial port
    read_byte = Serial.read();
    // send byte to serial port
    Serial.write(read_byte); 
    delay(100); 
    
  }

The problem I face is the values that I read fom the sensors are overwrittten. I am pretty confused if I am using the SAM controller or not. The SAM at default works in 4mhz and the sensor is at 16mhz. If I am using the DUE as a USB to serial adapter only , why is that the output on the serial port gets overwritten ? So the SAM's clock frequency affects the ATmega16U2 USB-serial chip and hence SAM must be configured as same as the sensor. Am I right ?

Connect the sensor Tx-Rx3, Rx-Tx3 and then upload the following code

void setup() {
  // initialize serial ports
  Serial.begin(38400);  // USB serial port 0
  Serial3.begin(38400);
}

char read_byte = 0 ;        // stores received byte


void loop() {
   //testread
  // check for data byte on USB serial port
  if(Serial3.available()) {
    // get byte from USB serial port
    read_byte = Serial3.read();
    // send byte to serial port
    Serial.write(read_byte);
    delay(100);
  }   
}

I tired the code with two DUEs and it worked . When I connected the sensor , it did not work. Is there any method to connect the other serial ports(1,2,3) to USB ? It simply works only with Serial0 port.

In My Arduino IDE folder , I cannot find 'sam' library. how can I access the low level registers for the DUE ?

What is the baud rate of the sensor. I just set it to 38400 because that's what you had in your other code but in the original post I see you had it set to 9600. You need to change this line to match the baud rate of the sensor's serial communication:

Serial3.begin(38400);

This line should match the baud rate setting in menu in the bottom right corner of Serial Monitor

Serial.begin(38400);  // USB serial port 0

Maybe it would be helpful if you told us what this sensor is.

void setup() {
  // initialize serial ports
  Serial.begin(38400);
  Serial3.begin(38400);    // USB serial port 0
}

char rx_byte = 0 ;        // stores received byte


void loop() {

   //testread
  // check for data byte on USB serial port
  if(Serial3.available()) {
    // get byte from USB serial port
    rx_byte = Serial3.read();
    // send byte to serial port
    Serial.write(rx_byte); 
    delay(100); 
    
  }
  1. I connected tx1-rx and rx1-tx. My device is not a sensor but atmega controller. I expected that UART communication between any two devices will be the same and that is why I was asking about clocking of the DUE. My Atmega is working with 16 mhz (I mentioned this earlier as my sensor's working frequency)
  2. I did not mention it earlier because I thougt it would be confusing for another person . I apologise.
  3. I used 38400 , the code with 9600 is for two DUEs. The above code is the code I use.

Can you post the code running on the ATmega controller?

chutki:
how can I change the clock frequency in DUE. Its default is 4 mhz. I want it at 16mhz

I don't know much about the Due, but I'm pretty sure it runs at 84 MHz, not 4 MHz (reference: Arduino Due — Arduino Official Store). Why do you want to change it to 16 MHz?

I am sory the code for atmega is a combination of very large files and I cannot post it. I want to change to 16mhz because atmega works at 16mhz and DUE is not 16.To establish a UART communication between them, I want both of them to work at same frequency.

What I am actually tryin to do is : atmega board cannot be directly conncted to PC , so I am using the DUE to connect it via USB. So connecting them via UART.

expectation.png:


NOOOOOO! We've already been through this.

There are two ways to go about this:

  1. Due as USB-serial adapter:
  • Hold the main microcontroller on the Due in reset.
  • Connect the sensor to the Due RX-RX0, TX-TX0
  1. Due as a serial pass through:
  • Upload the code I posted previously to the Due.
  • Connect the sensor to the Due RX-TX3, TX-RX3

Either way, the diagram you posted above will never work.

Thank you soooo much. After holding it in reset, I had connected rx-tx (I never realised it)and that is why it never worked. Really thankful to you.