Serial reception faster than 1ms ?

Hi,

I'm currently trying to measure the output voltage of my arduino UNO but i need to have data with less than 1 milisecond of interval between each measure.

I don't need a truly specific value, 100-200-250-500µs or whatever would be fine (less is better) as long as I know what is this time, with, let's say, 50µs of precision? Even a bit more would be fine.

My code is farly simple :

    int photocellPin = 0;
    int photocellReading;
    int D = 500;
    
    void setup(void) {
      Serial.begin(100000);
      
    }
     
    void loop(void) {
      photocellReading = analogRead(photocellPin); 
      Serial.print("\n");
      Serial.print(photocellReading);     
      delayMicroseconds(D);
    }

(as you can see it's a simple circuit with a photodiode, the photodiode is rated to have a 1-4µs of rise and fall time so it's not a problem)

However there is catch, i just came to realize that the code is actually taking time to run, i mean like serial.print is taking some µs to run but i don't know how much and if it's negligible or not.

I have read this post : http://forum.arduino.cc/index.php?topic=196328.0 According to Jenaflex it's taking 400-600µs which is significant....

Here is a graph of what are my results with 1ms of Delay (not DelayMicroseconds) https://media.discordapp.net/attachments/207953788026355712/425244268525912064/Rise-time.png?width=824&height=590 But apprently it's not exactly 1ms if Jenaflex is right (and i'm sure he is), even if i can't go sub-milisecond i need at least a 1ms precision...

I'm logging those data through putty in a .log file, the total time is around 30-40s. The size of the data is not a problem since i'm processing them through a script in R.

What would you recommend me to do to achieve a sub-milisecond precision to measure the voltage?

Thanks you in advance and sorry for my potentially bad english.

The time taken for analogRead is not negligible either - around 100us

Oh okay thanks you

So i should just forget the delay function.

So you can confirm that there is 100µs for analog read and 500µs for serial.print ?

Is serial.println() faster ?

Is serial.println() faster ?

Why would it be as, by definition, it has to output more bytes.

try Serial.begin(1000000); // 1Mb/sec = a byte in 10uSec

do you really need delayMicroseconds(D);

I don't think it has to do with baud speed but more about the time function are taking to execute themselves.

And nop, i don't really need delayMicroseconds as long as i know the time between each print.

UKHeliBob: Why would it be as, by definition, it has to output more bytes.

I don't know x)

Home
Buy
Software
Products
Edu
Resources
Community
Help

Show Cart

Arduino Forum

Using Arduino
Programming Questions
Preview - Re: Serial reception faster than 1ms ? ( Re: Serial reception faster than 1ms ? )
Alerts Unread Posts Updated Topics

Re: Serial reception faster than 1ms ?
Hello Rob,

just as a word of warning: not every serial connection can cope with 1Mbps…

@Xenio:
just for interest:
Doing the quick math, at 100kbps you will get 10000 bytes transmitted per second (8 Data + 1 Start + 1 Stop), i.e. you have 0.1 ms per byte transmitted.
Worst case you will be sending 6 bytes per turn (4 digit value + CR + newline). This will eat up 0.6 ms .
So you have 0.4 ms left for all the rest, where analogRead() eats up another 0.1 ms.
Then there are 300µs left - enough to run the program, but probably not too much margin for extra stuff.

If you would need exact timing of 1ms per run, consider using the “MsTimer2” library:
https://playground.arduino.cc/Main/MsTimer2
Usage is quite comprehensive. This will easily provide you with a 1ms interrupt tick which you can use to fire each run in constant periods.
If you were a beginner with Interrupts, make sure to use a single-byte variable only (bool, byte, char), to communicate with the timer interrupt service routine.

BR

stargar

Xenio: I don't know x)

Think about it.

How many bytes does Serial.print("XYZ");output ?

How many bytes does Serial.println("XYZ");output ?

@Stargar

Oh okay, so having less than 1ms delay between measures is quite compromised >< Well if i can at least have 1ms accuracy it would be fine.

For MSTimer2, if I set a timer of 1ms, will it be 1ms + time to run the program or 1ms ?

UKHeliBob:
Think about it.

How many bytes does
Serial.print("XYZ");output ?

How many bytes does
Serial.println("XYZ");output ?

I guess 3 and 3 + 1 + 1 … ><

Hi,
So I did some research and test and come to this :

Using Serial.write takes usually around 50µs against more than 100 for Serial.print
Using analogRead takes usually around 100µs
Converting an int to a byte takes basically no time (0-4µs)

So i’m going to use Serial.write and use those raw datas.

My outputs will stay between 0 and 1023 so can I assume that it will fit in 2 bytes?
Since for example 523 is 00000010 00001011 in binary ? Or am i wrong ?

So if it is the case then i’d have 200µs of transfer (100µs per byte at ~~100 000 bd)

However now I’d like some more informations about this MStimer2.

If I successfuly set a 1ms timer for my loop then will it be 1ms + time for processing or just 1ms? Assuming that the processing time is less than 1ms.

If this work like this I’ll use FlexiTimer2 which is working exactly like that but with a configurable resolution.

#include <FlexiTimer2.h>


    int photocellPin = 0;     
    int photocellReading;     
    byte lb;
    byte hb;
    
    void setup(void) {
      
      Serial.begin(115200);
      FlexiTimer2::set(1,1.0/1000, Mesure); 
      FlexiTimer2::start(); 
    }
     
    void loop(void) {
      
    }

    void Mesure() {
      photocellReading = analogRead(photocellPin);    
      lb=lowByte(photocellReading);
      hb=highByte(photocellReading);
      Serial.write(lb);
      Serial.write(hb);       
    }

Here is my final code
I have 0.1ms per byte ~
analogRead takes 0.1ms
lb and hb are almost instantaneous
I’m sending 2 bytes so 0.2ms

So the processing time is less than 0.4ms and I can set a timer of 0.5ms (here it’s 1ms, I was still testing).