LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a square wave with a dynamic frequency

Sounds like you are on the right track.  I would start with the two Voltage - Continuous examples.  The Voltage - Continuous Input.vi for reading your voltage and the Voltage - Continuous Output for generating a square wave.  Then you need a way to detect changes in the voltage input from loop iteration to iteration like a shift register comparing values.  When you detect a change, you update the analog output (square wave) frequency based on the new voltage reading.

 

Post the code you're working with so we can take a look...

 

I'll make a quick example but I only have a USB-6002 DAQ here right now so I can't do anything real fancy.

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019
0 Kudos
Message 11 of 23
(956 Views)

I don't have time to check the analog in and I don't know how you want to convert voltage to frequency but this should give you some idea what I was talking about.  I have a control for simulating the changing voltage level.  You will need to add your conversion code in the disabled diagram structure and enable it.

 

You will also need a fancier way to determine when the voltage input changes enough to warrant an update in frequency.  Right now it would change every loop due to noise in the Analog input so you'll want some sort of hysteresis programmed in.  Even changing the datatype of the frequency variable from a float to an integer would do for a start.

 

Play around and see what you can come up with.  Post your VI (not a picture) if you get stuck on anything.

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019
Message 12 of 23
(953 Views)

FYI, some good tutorials for getting started with LabVIEW and DAQmx.

 

LabVIEW Tutorial (ni.com)

 

Getting Started with NI-DAQmx - NI

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019
0 Kudos
Message 13 of 23
(948 Views)

FWIW, I recall that commonplace MIO boards such as your M-series can *also* allow you to change the AO sample rate on the fly as another method for changing the output waveform's frequency.   You would use a DAQmx Timing property node to set SampleClock.Rate.   (Mod below added to shipping example and tested on a simulated device).

 

                     Kevin_Price_3-1680623189959.png

 

 

-Kevin P

 

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 14 of 23
(942 Views)

If you are using a counter output, then this is pretty easy.  I wrote a frequency sweeping code that uses a counter and posted it here  this can change the frequency and duty cycle without having to stop the output.  If you are using an analog out, you can get a bit crafty.  Years ago I made a continuous AO output with a waveform that just repeats.  While the task is running you can replace the buffer with new data.  So As long as the number of samples are the same you can change the data without stopping the task.  I describe some of the details here.  But I don't have the code anymore.  At the time I got away with having my frequency step by 100hz increments, which helped in keeping the buffer sizes the same, so some rates would repeat more than others.  I've never tried changing the clock rate but that sounds much easier as long as you only have one AO at a time.  I think I had N channels N samples at the time.

0 Kudos
Message 15 of 23
(932 Views)

Thank you this has been very helpful. 

 

I am still a little confused on the disabled diagram structure, and how I should be connecting the varying frequency. 

 

The input voltage will be varying from 1-5V. Is there a way I can have the frequency change double as the voltage increases by one? This would give me a good starting point.

 

I have attached your code that I have been using/testing and modified slightly. Let me know of any suggestions. 

0 Kudos
Message 16 of 23
(911 Views)

Also, is there a way to have the width of the pulse change? Right now it is 1/2 of the period. If possible I would like to have the pulse no more than 5 mV while still being able to adjust the period.

 

0 Kudos
Message 17 of 23
(894 Views)

I'm using LabVIEW 2019 so I can't open your code.  I updated my version to better show how simple math functions (e.g.: Multiply) can scale your voltage input into a frequency.  You could use a formula node or any other method as well.  The pulse width is controlled by the Duty Cycle input on the square wave generator.  It works in terms of a percentage of the wave that is high so 50% is a symmetrical square wave and 10% is more of a pulse wave.  It you want a consistent 5mSec pulse you will have to recalculate the percentage to give you that based on your signal's frequency.  Then change it on-the-fly just like we are doing with the frequency.  I added a control for it so you can see how it affects the signal.

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019
0 Kudos
Message 18 of 23
(881 Views)

I made just a couple mods to the code posted by NIquist, mainly to carry through phase information (which in turn led to writing into the buffer *every* iteration).  Then I moved the AI task to be above the AO task to limit wire crossings.  And a couple other fairly trivial changes.  It isn't a final version, there's still stuff for you to do:

 

1. the correct math to convert AI voltage to desired square wave frequency

2. calcs to then determine the correct duty cycle when you want a fixed pulse width across all frequencies

3. possible further calcs to modify phase appropriately.  (For example, as frequency changes, not changing the phase angle might result in an immediate square wave transition, which you may want to avoid)

4. possibly some work to reduce latency time between changes on the GUI and changes in the real-life signal.  Getting down to 1/2 second or so is pretty manageable and robust.  Under 50 msec requires more detailed work and increases your risk of buffer underflow errors.   I recommend you don't start messing with this unless you *need* to and even then, not until after you take care of the other stuff.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 19 of 23
(859 Views)

Thank you. I have been working through the program and I think it is almost there. I'm currently getting the error:

Error-200479 occurred at DAQmx Start Task.vi: 7220001

Possible reason(s):

Specified operation cannot be performed while the task is running.

 

Do you have any insight on what may be causing this error? I have checked all the while loops and they seem to be ok. I have attached my program below. 

 

 

0 Kudos
Message 20 of 23
(823 Views)