millis() function to send .txt file characters one by one

Hello everyone,
I've been trying to send a txt file to my arduino one by one for some time and no luck!
my txt file contains a few characters like AJGIVJKFJHR...... and I need these characters to be sent to arduino UNO one by one periodically (for example every 0.5 second ).
However,there seems to be a overflow problem when I use delay command. A friend suggested that I use millis() function but having some trouble when it comes to writing the right codes.

does anyone know how to do that ?

I would really appreciate any thought.

We can't see your program so it is impossible to know what the problem is.

Have a look at how millis() is used to manage timing without blocking in Several things at a time.


You can start by showing your attempt.

It's my understanding that your text file lives on a pc; if you want to send a character from that file from the PC ever so often to the Arduino, you will need to write an application for the PC that reads the file and e.g. sends one character e.g. every 500 milliseconds. After that the Arduino code should be straight forward (check if data is available, read the byte, take action). Nothing difficult with delays or millis() as far as I understand the problem.

First, thank you guys for the help!

Well,i'm trying to send a .txt file that contains a few characters to arduino Uno.

Here is the sample code :

void setup()
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
void loop()
  while (Serial.available())
    char command =;
    if (command == 'A')
      digitalWrite(13, HIGH);
      digitalWrite(13, LOW);

And I have a text file (D:\example.txt) that contains the following in it :


And i'm using Tera Term program to send the txt file to arduino (TeraTerm>File>sendFile)

Part of the data copies just fine, and I can tell that around 3 of the 'A's have transmitted fine based on the number of times the LED blinks. But half the data failed to be received. I've tried to send smaller batches, and play with the COM port settings, but I haven't found a way to ensure all the data gets received by the Arduino.
A friend suggested that the problem is with the (delay) command and that it's overflowing the buffer.
There were suggestions to use millis() command but I'm having problems figuring out the codes.

any thoughts are appreciated !

You should not have any delay()s, or any other form of "wait" in the receiving program. It must be listening all the time. Any timing should be put in the transmitting program.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. They should not miss any of the data.


At 38k4, one byte takes about 250 microseconds to be transferred. In the time that you switch your led on and off (275 milliseconds), over a 1000 bytes will have been transferred (if your file is that big).

However, the Serial class has a software receive buffer of 64 (or 63) bytes; once that buffer is full (because you did not read fast enough), further received data is thrown away.

Robin’s solution will only work if the file is small so you don’t have to allocate a big buffer (to store all received data) in the small memory of your Arduino.

It might be better to stop sending during the time that your code is taking action on the received data (regardless if that uses delay or a millis() based approach). The solution for a terminal program lays in the software handshake Xon/Xoff. Once you have received a byte, you send a Xoff to Teraterm to tell it to stop transmitting. Once Serial.available indicates that there are no more bytes and all your actions have been performed, you send a Xon to Teraterm to tell it to continue transmitting. Repeat.

There is an example code for the Arduino side of things here. Please don’t ask why it was implemented as it is as I have forgotten.

You need to configure Teraterm to use Xon/Xoff for this to work.

RealTerm will allow you to specify a delay between each character and each line of a text file when it sends.

The Arduino program with the big delay()s won't process fast data but this is all about testing isn't it? The real data will never come that fast to send two "A" within 250 milliseconds?

I found the solution , thank you guys really , if it was not for your help I would still be frustrated .
For those who might have the same problem in the future the answer lies in the TeraTerm itself ( or any similar program )
what u have to do is to open the TeraTerm>setup>serialport>transmit delay
this way u can send each character or each line in your desired timing. Thus, avoiding any kind of problem with the memory of the arduino and overflow and alike!