Hi Sir,
What is the Time Taken for Serial.Write Function in arduino if i use 115200 baud rate sending Single byte of Data (8 bits) into the Serial port?
It depends on the size of the data written. At 115.200 baud, 8 bit data plus 1 start and 1 stop, divide by 10, so are roughly 11.520 bytes per second. If you send 1.152 bytes they'll take 1/10th of a second.
The time taken by Serial.write() in your sketch will depend on how much space is available in the Serial transmit buffer. write() only needs time to place the data in the buffer, then returns, unless the buffer is full, in which case it needs to wait for characters to be sent in order to free up space in the buffer.
Well Because the Serial.write() function uses a data buffer and is an actual hardware Serial port it "wont take" any time the reality is more complex:
Writing to the buffer takes roughly 1-2 cycle per byte depending on actual code but assuming its inline and just register modification shouldn't be more than 5 cycles.
Actual time for it to reach your computer is as @docdoc said.
If you want it to appear on the Serial port timing thats a whole different story including the latency of your OS and Arduino IDE processing etc.
Note: The writing to the buffer time is valid until you fill the buffer which by default should be 64 bytes so 64 characters or 32 integers of 16 longs. After the buffer fills you will be at the same speed as the Serial port baudrate thus timing of @docdoc will be affective and the code will execute as blocking
Hi sir, Thanks for the reply. I thought that the Baudrate = 1,15,200 give 1/1,15,200bps = 8.6 usec per bit.
8.6 usec x 10 bits (including Start and stop bit) = 86 usec to send the single byte into the Serial port using the RS-485. On otherside, it is not PC, it is another Microcontroller (Atmega2560). why it is 1/10th of the second? 10 msec for 1 byte? Kindly let us know. I have to send the 10 bytes in 1 millisec? How to achieve this?
I suggest you to avoid messing with single bit timings and microseconds, what is relevant for the topc is how many bytes per second you can send given the baud rate.
About the 1/10th, I explained why: on serial communications, if you have 1 start bit, 8 data bit, plus 1 stop bit equals to 10 bits per byte. So divide the baud rate by 10 and you'll get the maximum theoretical speed possible. Thus, at 115.200 baud you have up to 11.520 bytes per second, giving the 86 microseconds per byte if you want that measure.
If you know the size of the data to be sent, you can calculate the time needed. But remember this is the time for the other side to receive the data, neither the time Arduino processor needs to send it, nor the time the receiver needs to interpret it.
Looks like there may be some confusion caused by the period vs comma usage in the numbering systems of different countries.
Hi sir. Thanks for the reply. My baud rate is 1 lakh, that is 1,15,200. If i divide my baud rate 1,15,200 /10 (8 bit +Start bit + stop bit) =11,520 bytes per sec can be send using the arduino serial function. right?
11520 bytes/sec
1152 bytes/100 msec
115 bytes /10 msec
11.5 bytes /1 msec
1 bytes /100 usec
1 bit /10 usec approx
Can we achieve using arduino serial function? Kindly let us know.
That does not matter; the UART of the sender just pumps it out. Be aware that there is buffering. As a result your Serial.write() statements will fly till the buffer is full, Once the buffer is full, Serial.write() will slow down to the specified 86 microseconds per.
Buy a board for testing purposes; write a test sketch and test it. While buying, buy a cheap logic analyser (or an expensive one or an oscilloscope) so you can measure the results accurately. If a baudrate of 115200 does not cut it, increase it.
However, increasing baud rate might come with its own problems when communicating over longer distances. So you're currently only telling us part of the story.
Note:
Boards with native USB don't use a UART for communication with the PC. So Serial.write() will work at USB speeds. But that is not relevant if your Arduino is communicating with another Arduino.
Sorry, is it a question or a statement?
I mean, do you need to send 10 bytes each 1 ms (I don't know if you "have" to do that), or you're asking if 10 bytes can be sent in 10 milliseconds? Can you explain why you need to know that? What kind of data and communication you're setting up Arduino for? How you're connecting them (cable type, length...)? Which Arduino models we're talking about? You said the receiver is a Mega (right?), but you need to specify the sender also...
Are you using hardware serials (e.g. onboard UARTs) or SoftwareSerial?
Just to try and (lastly) make it clear, there are no more things to say: just divide the serial baud rate by 10 and you have your maximum speed you can achieve if the sender is able to collect and send the data, if the communication line is clean, and if the receiving party is able to receive and use the data.
If those conditions are met, and you set your serial to 115,200 baud, you can send up to 11,520 bytes per second. Period. Thus, if it meets your requirements you're ok.
If you need higher speed, some other considerations come in place, so we'll discuss about them if you need to get more than 115,200 baud.
Sir. I have to send 10 bytes within 1 millisec
What kind of data and communication you're setting up Arduino for? How you're connecting them (cable type, length...)?
RS-485 communication. 3 core shielded cable with less than 10 metres
Which Arduino models we're talking about? You said the receiver is a Mega (right?), but you need to specify the sender also...
Sender is also the Arduino Mega 2560 / Atmega328P
If you need higher speed, some other considerations come in place, so we'll discuss about them if you need to get more than 115,200 baud.
Arduino mega will support upto 115200 baudrate with 16 Mhz crystal.
Are you using hardware serials (e.g. onboard UARTs) or SoftwareSerial?
I am using the TX and RX Pin (UART) Port with RS-485 Driver. Not used in the USB Port Serial Port. I have to use Serial.Write function to send the data. if any arduino serial communication function will do faster than this function. Kindly recommend.
Hi sir, Thanks for the reply.
Link tells that the arduino serial library will support upto 115200 bps:
Attachment:
Searched in google "arduino mega baud rate speed"
It show the result as:
That's "SoftwareSerial", a library to emulate a serial over any other pin, not the hardware onboard UART chip. And as you said:
I am using the TX and RX Pin (UART) Port with RS-485 Driver
forget SoftwareSerial and believe on the table @sterretje has shown you.
That's "SoftwareSerial", a library to emulate a serial over any other pin, not the hardware onboard UART chip. And as you said:
I misunderstood that the softwareSerial library is needed for UART to communicate. Now I understood that the Serial.Write Arduino Function is no way related to this Software serial library. I am going to use the dedicated UART Pins. I understood that the arduino Mega with arduino serial function will Work upto 1 Mbps with 16 Mhz External Crystal. Can achieve/Transmit 10 bytes within 1 msec time frame. My required RS-485 cable length will be around less than 10 metres.
Yes. But both parties should be able to set such serial speed: are they both Mega?
An RS-485 with a good cable should be enough, even if I never used 1M baud on a Mega.
But be advised about the possible "physical" constraints, like the limitations in the Mega buffer and the receiver real reading/processing speed. If your requirement is to be able to send a 10 byte packet within 1 ms, I think 230400 baud could be a good compromise between speed and communication problems.
I don't know the project where you're working on, but if the data is somehow critical, you should also think about introducing some flow control (XON/XOFF) and/or data integrity check (Checksum or CRC).
The Mega has 4 UARTs. Don't use pins 0 and 1 but another pair; this allow you to use USB for debugging during development.
If you're still planning to use the 328PB, it has 2 UARTs so you can do the same.
If you're planning to use the 328P, you have no choice but to use the RX/TX pins for a baudrate of 115200 (see notes below); you can use SoftwareSerial plus a serial-to-USB converter (on e.g. pins 2 and 3) for debugging. Note that anything connected to the RX/TX pins if the 328P might interfere with the upload over USB.
Notes
- The above assumes that you upload over USB not using ICSP. If your using ICSP, you're free to do with RX/TX what you want.
- Note that SPI devices can interfere with upload using ICSP.
- Although the page that you linked states 115200 baud for SoftwareSerial, the general concensus is that 38400 baud is the maximum. If you decide to use SoftwareSerial in another project, test it before committing.
Yes. I will select both the end with same parties. Mostly i think it will be mega on both side.
I think 230400 baud could be a good compromise between speed and communication problems.
Will check the same to achieve it.
I don't know the project where you're working on, but if the data is somehow critical, you should also think about introducing some flow control (XON/XOFF) and/or data integrity check (Checksum or CRC).
Have to use Modbus arduino library with Checksum CRC in it.
The Mega has 4 UARTs. Don't use pins 0 and 1 but another pair; this allow you to use USB for debugging during development.
Will use other port other than Pin0 and Pin for Uart RS-485 communication
The above assumes that you upload over USB not using ICSP. If your using ICSP, you're free to do with RX/TX what you want.
I have a plan of upload the .hex file using the ICSP port only due to the Space constraint in one Board. For this, i have to go to ATmega2560.
Note that SPI devices can interfere with upload using ICSP.
Atmega328P have only 1 SPI Port. I have SPI interface Plan of 6 SPI Devices in one SPI port. For this also, I need a seperate ICSP Port(SPI Port) for Program .hex using ICSP Port. For this reason, i have to go to Atmega2560. Thanks for this information.
Although the page that you linked states 115200 baud for SoftwareSerial, the general concensus is that 38400 baud is the maximum. If you decide to use SoftwareSerial in another project, test it before committing.
Yes. I understood. I will try to use and start from 115200 baud rate to Transmit/ achieve 10 bytes within 1 millisec
Yes. So if you have SPI devices connected to it, I would advise to implement a mechanism to disconnect those so you can upload using ICSP.