LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Sampling rate of data from Arduino not correct

Hi all,

 

I have several sensors connected to an Arduino Mega 2560 and want to read and save this data at a specific sampling rate via USB/Serial. The arduino program is written so that it will send out a sampling including a timestamp depending on an input. Right now, I have the Arduino spit out new lines of samples at 100 Hz in this format: Timestamp-->sample 1-->sample2-->sample3 /ln 

 

Now, I want LV to write this data to a spreadsheet. Unfortunately, there is always one sample missing @100Hz and only 99 samples/second are written to the spread sheet. Similar thing with 200Hz (195Hz are recorded). Only 25 Hz seem to work fine. 

 

The Arduino code is working just fine. I have tested the USB output with the serial monitor from the Arduino software and always get all the samples I am supposed to get.

 

Hopefully some of you have an idea what is going on. There might be some issues with the windows timing, but that is obviously just a guess. 

 

Cheers

0 Kudos
Message 1 of 7
(3,117 Views)

Anyone?

0 Kudos
Message 2 of 7
(3,067 Views)

Why are you opening and configuring the same VISA port twice?  Use the same purple VISA wire for both.

 

Why the 1 iteration while loop?  Why the extra sequence frames?

 

Use the attached VI.

 

Since you are writing to the file in the same loop you are reading the serial port, is it possible a file write is taking longer than you are expecting?

 

Is it possible you are overflowing your serial port buffer?  Are you getting any error messages?

 

128,000 bytes seems kind of large for a serial port buffer.  Did you have probelms when you were using the default buffer setting?  Why did you set it so large?

0 Kudos
Message 3 of 7
(3,058 Views)

Hi RavensFan,

 

well, I tried your .vi which I like it's simplicity 😉 

 

Anyways, i tried the really high buffer settings just for the sakes of it since I did have this problem with the default buffer setting.

 

After using yours,  there is still data points missing. If I am sending at rate of 100Hz, I still only get 99 samples saved in the spread sheet. Again, the arduino serial monitor shows all samples.

 

I am not getting any error messages either.

0 Kudos
Message 4 of 7
(3,041 Views)

Hi lookingforflow, 

 

How are you determining that LabVIEW is only reading 99 samples?  Could you post a screenshot?  

 

If the write to file function is taking longer than the serial read, which might cause a loss of data, you may want to use a producer/consumer architecture.  Essentially you would have two while loops: one to read the serial data, and one to write it to file.  The first loop (serial read) passes data to the second loop (write to file) via a queue.

 

Here is an example which uses a producer consumer architecture, but with DAQmx:  https://decibel.ni.com/content/docs/DOC-2431

The same thing can be done with a serial read (instead of DAQmx read).

0 Kudos
Message 5 of 7
(3,022 Views)

Hi WishKebab,

 

I am actually using the producer/consumer loops. I was checking the spreadsheet to see if the sampling rate is correct. Every now and then It just doesn't seem to "record" the values.

0 Kudos
Message 6 of 7
(3,017 Views)

There is no logical reason why it wouldn't record values.

 

I would put indicators on your various error wires just to make sure your VISA reads, or your File writes, aren't getting any errors.

I would put in a Bytes at Port property node and monitor that while your VI is running to make sure the buffer isn't filling up.

 

Beyond that, I suspect that your Arduino is actually working they way you think it is.  Perhaps you are gaining or losing tick counts on its clock.  Or you are misinterpreting the tick counts.  Or just not analyzing your log file correcly.

0 Kudos
Message 7 of 7
(3,013 Views)