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.
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.
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.
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.