LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Arduino analog read pin to frequency

Solved!
Go to solution

Take a second look at the type of structures shown in the code I attached previously

 

The loop that contains the data aquisition is a for loop and only runs a set number of times.  The larger loop around that is a while loop, so that once the aquisition and analysis are completed the aquisition starts again.

 

Case structures run once, so as you have implemented it it will not start again.

 

I have included the live accelerometer reading as you coded it in the snippet below. Look carefully at the structures!

 

arduino_test_snippet2.png

Craig H. | CLA CTA CLED | Applications Engineer | NI Employee 2012-2023
Message 11 of 33
(4,691 Views)

Thanks Craig.

 

 

It's up and running the loop simultaneously unaffecting the rest of my program.

 

However, I am a bit surprised with the time it takes to capture say 128 data points of voltage in 15.6 seconds, or roughly 8 samples per second. That's only a sampling rate of 8 Hz!!

 

The baud rate for the arduino has been set to 115200, and the delay in the for loop I had for 1 ms, which I removed but didnt speed up the code. If the accelerometer I am using has a sampling frequency of about 50Hz, why is there such a huge bottleneck in the program execution?

0 Kudos
Message 12 of 33
(4,680 Views)

There are actually much better functions for polling information from the arduino. The reason it is running so slowly is because the read pin function is not built for any sort of continuous aquisition, your code running slowly demonstrates this.

 

Take a look at the Continuous Sampling Example that can be found in the Arduino -> Examples Pallete. It uses a Continuous Samples vi found in Arduino -> Low Level -> Analog Sampling palette. This vi returns an array and it should replace the for loop you have been building.

Craig H. | CLA CTA CLED | Applications Engineer | NI Employee 2012-2023
0 Kudos
Message 13 of 33
(4,678 Views)

Hey Craig,

 

Thanks for the quick response. I did try the continuous sampling Vi before but noticed that its really good for controlling just one pin at a time, as opposed to simultaneously continuously sampling 3 pins on the arduino. I was reading up about this and I guess its just the way the program and arduino can interact and can't be circumvented (https://decibel.ni.com/content/message/38026).

 

Do you suggest purchasing another arduino just to control one of the pins, essentially I am only interested in the Z-axis of the accelerometer. Do you think this would alleviate the issue?

 

Thanks for your continued support!

0 Kudos
Message 14 of 33
(4,675 Views)

You could also try using the Analog Read Port Function and pulling elements out of the array. It's possible that that would perform better than polling each pin as it may be getting the readings across the ports at once.

 

Otherwise it does sound like there could be a device limitation. Using another Arduino and dedicating one of them to read the one channel that is most important may be a good option. I'll look a little more into this later!

Craig H. | CLA CTA CLED | Applications Engineer | NI Employee 2012-2023
0 Kudos
Message 15 of 33
(4,671 Views)

Hi Craig,

 

Running the Continuous sample for one pin made a huge difference, it's much snappier now. The FFT is also starting to make more sense and is way faster. On the downside, can't simultaneously read or write other pins now. =(

 

You mentioned pulling elements out of the array, how does one go about this?

0 Kudos
Message 16 of 33
(4,669 Views)

Hi.

 

So I've mad some head way, but I am stuck trying to figure out how to display the X value of the maximum Y value for my FFT waveform graph. I looked at some other forums for ideas and implemented it into my code and it correctly shows me the maximum amplitude, but not the corresponding correct frequency.

 

Attached my code, could someone please give me some feedback?

 

Thank you!!

0 Kudos
Message 17 of 33
(4,655 Views)

I went ahead and changed a few things in your VI and have attached it below (we don't normally do this, but this is interesting!). Note the different FFT function and how the outputs are being calculated.

 

You will probably need to figure out how to filter out the low frequency fourier offset so that it doesn't appear as the largest part of the fourier transform.

 

As for manipulating arrays, take a look at the array pallete found in the programming pallete.

Z axis only2.png

Craig H. | CLA CTA CLED | Applications Engineer | NI Employee 2012-2023
Message 18 of 33
(4,630 Views)

Hey Craig,

 

Thanks for taking time to redo my code!

 

Unforuntately the FFT results make absolutely no sense, since it is peaking at max around 135 Hz, yet the spec sheets claim close to 240 Hz. I was able to remove the DC offset by applying a median filter, which seems to do the trick.

 

I took some components of your modified code and implemented in my original complex FFT. Doing it this way seems to give me more realistic results, with values close to 300 Hz when I peak to maximum voltage on my accelerometer. I am not sure what the issue is, and why the normal FFT analysis is not working...

 

Attached my most up-to-date code which shows the maximum frequency, alas it is not in decimals but integers (something to do with the complex FFT which I am normalizing to polar form?)

 

Thanks!

0 Kudos
Message 19 of 33
(4,628 Views)

I actually was able to test your code with the small modifications I made on an Arduino Uno with a 10Hz sinusoid input. The FFT was showing the correct frequency for the 10Hz input.

 

There could be a lot of things going on here:

What accelerometer are you using?

What sampling rate do you have set for your arduino?

How many samples is the program getting from the arduino for each window?

What is the accelerometer attached to (e.g. what type of vibration is it measuring)?

 

I would primarily be concerned about aliasing in the sample you have coming in. If you are expecting to see a peak at 300Hz you need to be sampling at >600Hz to accurately capture the input waveform.

Craig H. | CLA CTA CLED | Applications Engineer | NI Employee 2012-2023
0 Kudos
Message 20 of 33
(4,622 Views)