Fastest Loop Time of Arduino UNO While Loop | Arduino Compatible Compiler for LabVIEW Discussions | Forum

Avatar
Please consider registering
guest
sp_LogInOut Log In sp_Registration Register
Register | Lost password?
Advanced Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Feed Topic RSS sp_TopicIcon
Fastest Loop Time of Arduino UNO While Loop
January 30, 2020
1:16 am
Avatar
Anthony L
Member
Members
Forum Posts: 6
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline

Hello Members:

I am testing an Arduino UNO board to read encoder digital signals at 5V TTL.  I was able to wire the encoder sensor and I provided external excitation of 5V to my encoder with common ground to Arduino UNO board. The encoder is 32 Pulses Per Revolution and is of the optical incremental type of encoder.  The count results read from Arduino UNO were OK for slow hand speed rotation but counts were missed when rotating faster. Eg by quickly rotating encoder by a quick hand push or strike about 10 counts were missed. This made me suspect the speed of Arduino UNO in running a loop.

Testing the Arduino UNO Loop Time

To do this I created an empty while loop and I only placed the Arduino microsecond timer functions to capture the minimum loop time. I also added Serial Write function so that Arduino UNO could report back a snapshot of what the loop time was. I compiled the Arduino UNO LabVIEW VI using Arduino Compatible Compiler for LabVIEW and downloaded that to the Arduino UNO. I used baud rate of 115200 and reported the time snap shot at 50msec intervals.

On the LabVIEW side I created a LabVIEW Serial Read VI using same Baud rate of 115200. The LabVIEW VI showed that the fastest loop time that the Arduino UNO could do was 64 microseconds. And this means 1/0.000064 loops per second or 15.625kHz.

Is this correct? The documented clock speed for Arduino UNO is 16MHz. I thought that the loop time would also be the same. Or am I missing something here?

 

NOTE: All LabVIEW VIs are inArduino-UNO-Minimum-Loop-Time-FASTEST.pngImage Enlarger LabVIEW 2014

Regards

Anthony Lukindo

January 31, 2020
12:50 am
Avatar
Steffan
Admin
Forum Posts: 408
Member Since:
March 12, 2015
sp_UserOfflineSmall Offline

16MHz defines the processor instruction cycle time, which is the execution time of 1 processor instruction, which is certainly not the same as loop time in a higher level language.  Every line of code will take many instructions.  In this case there are several layers between LabVIEW and processor instructions so you will have overhead in the conversions from LabVIEW to C and to assembly.  You also have a subVI, which translates to a function in Arduino, and will take multiple instruction cycles on the call and return.  You can read more info here and here.

In your example writing to the Serial port is slow as there is only a 1 byte hardware serial port buffer on the Uno. Execution time of Serial Write is roughly 10s/baudrate per character.  If you are writing 100 characters, that would take almost 10ms just to write.  More info here.  But doesn't look like you write every loop in that test code.

Regarding the original issue with the encoder, it depends how you implemented it.  If you use a digital edge interrupt, then you should not have an issue as long as the code in the interrupt is minimal.  If you were using Digital Read in the main loop, that could be an issue.  There is a lot of overhead in Digital Read used by the Arduino as it has to map software pin to physical port pin.  This takes about 5us (see here).  I would suggest using a digital interrupt and increment a counter in the interrupt handler.  You would also want to make sure you have either hardware or software debouncing.

January 31, 2020
7:24 am
Avatar
Anthony L
Member
Members
Forum Posts: 6
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline

Hello Steffan:

Thank you for your advise. I went ahead and removed the SubVIs and I already got a speed increase where loop time dropped from 216 to 124! This is (216 - 124) = 92 microseconds or 43% improvement which is quite impressive.

Regarding Interrupts:

My data acquisition loop has both Digital and Analog channel measurement needs. If I go with using interrupt technique for measuring counters from digital input pins will this make a difference or is there a way to use interrupts for measuring analog voltage was well?

In any case, is there LabVIEW equivalent code that can help me implement interrupts programming to cut down on latency time for reading digital input pins? And if applicable, is there LabVIEW interrupt code to read analog voltage channel?

Thanks for any further advice that you can provide.

Anthony

January 31, 2020
9:11 pm
Avatar
Steffan
Admin
Forum Posts: 408
Member Since:
March 12, 2015
sp_UserOfflineSmall Offline

Analog Read takes about 100us.  See here.  You can do it in an interrupt but it will affect timing of the encoder.  Typically you would do the analog read in the main loop and keep an interrupt for your encoder edge transitions.  There is an included example showing how to implement a digital interrupt.  If you need higher performance, you can also look at a higher end Arduino like the Due, which has an 80MHz processor.

Forum Timezone: UTC 0
Most Users Ever Online: 100
Currently Online:
2
Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
miche91: 33
scottj: 28
scadao: 23
Member Stats:
Guest Posters: 5
Members: 202
Moderators: 0
Admins: 3
Forum Stats:
Groups: 1
Forums: 2
Topics: 266
Posts: 1222
Newest Members:
pujacontrol
Administrators: geadmin: 22, filipealtoe: 96, Steffan: 356