I am working on a serial communication project. In this project, I would like to use the serial pins ( TX, RX, GND) of FTDI basic breakout board to send serial data to my Leonardo board. And I use the serial monitor on Leonardo board to check if the board receives the serial data from my FTDI.
Connections between FTDI and Leonardo are:
TX to RX1 (pin 0)
RX to TX1( pin 1)
GND to GND
Somehow, the serial monitor doesnt receive any serial data from FTDI. Both FTDI and Leonardo board connect to different COM ports of my laptop. I dont know where the problem is . I have used the loop-back method on FTDI to verify that TX and RX of FTDI are working fine. The TX LED in FTDI is flashing properly every time I send out a data from FTDI. Attached is the code that I use to read the serial data sending in to Leonardo.
const int led = 13;
int matlabData;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
while(!Serial);
}
void loop(){
if(Serial1.available()>0){
matlabData = Serial1.read();
Serial.println(matlabData);
}
}
Serial1 is not available on the leonardo, since there is only one serial port on that board And even if there was a Serial1, you would have to do Serial1.begin.
This worked for me. My FTDI is connected to RX0/TX0 of an Arduino Mega, FTDI on COM5, Arduino on COM4. Sending a message from the FTDI will be output on both COM4 and COM5.
The Arduino Leonardo board uses Serial1 to communicate via TTL (5V) serial on pins 0 (RX) and 1 (TX). Serial is reserved for USB CDC communication. For more information, refer to the Leonardo
I use Arduino Pro Micro's quite a lot, which are basically same as the Leonardo (i.e they both use the AtMega32U4) and it definately has serial1 because Serial is a connection via the chips built in connection to USB.
You are trying to receive an integer with serial. I thought that serial received is all ASCII, which is why there is a function called
ATOI (Ascii to Integer)
Where did you get this code ? Did you write it yourself ?
You can print an integer but receiving it is another story (AFAIK).
I could be wrong. Just sayin'......
Thank you all so much for the support. I get it work. I do need to put Serial1.begin() in void setup().
One more question is that I have a number (for example 1 ) in my terminal program (or matlab program )to send out from FTDI breakout and however the serial monitor shows 49 on the Leonardo board. I suspect that the leonardo takes this number as a string ( or a symbol). How I can do in my code to decode 49 (ASCIITable based) to turn it into 1 if I dont use the simple math. Thanks a lot again for all your support. Feel good to get back to here from time to time.
Serial is handled directly by the USB there are no pins connected to Serial
Which is why the Leonardo and Pro Micro etc are quite useful as they give you an extra Hardware Serial that you can use without needing to use the larger Mega board
Serial is handled directly by the USB there are no pins connected to Serial
Which is why the Leonardo and Pro Micro etc are quite useful as they give you an extra Hardware Serial that you can use without needing to use the larger Mega board
That's all right. Thanks a lot Guix for your follow up.
One more question is that I have a number (for example 1 ) in my terminal program (or matlab program )to send out from FTDI breakout and however the serial monitor shows 49 on the Leonardo board. I suspect that the leonardo takes this number as a string ( or a symbol). How I can do in my code to decode 49 (ASCIITable based) to turn it into 1 if I dont use the simple math. Thanks a lot again for all your support. Feel good to get back to here from time to time.
int matlabData;
Are you sure that code is correct ?
You are trying to receive an integer with serial. I thought that serial received is all ASCII, which is why there is a function called
ATOI (Ascii to Integer)
Where did you get this code ? Did you write it yourself ?
You can print an integer but receiving it is another story (AFAIK).
I could be wrong. Just sayin'......
@raschemmel. Thanks a lot for helping along the way.
The code is right.My code is writen based on an example from Arduino website. (forget where I saw that example now. Sorry.) And you are right as well. From what I undertand, the data type of TX is treated as string, the serial monitor of Leonardo will display the ASCII based number for the string. That's why it displayed 49 when I send out the value of 1 from matlab program. If I make the type of data in matlab as Char, the serial monitor will display the value of 1 as 1. But I will try your suggestion as well. Thanks again.
Thanks, Guix. I end up making Char as the type of data in my matlab code (which generate serial data for TX). And then I use int as the data type in my arduino program.
One more question is that if I want to send out the data as an array (for example [0 10]), do I need to build up the array back in my arduino program after serial communication. Because I believe that the serial data is transmitted as bytes. Then 0 would be received by arduino first and 10 would be received second.