Hi
I am trying to generate a excitation frequency sweep to excite a resonant coil and measure the resonant frequency.
First step: generate frequency sweep
I am trying to generate a frequency sweep (50% duty cycle square wave) with range 500Hz - 5000Hz.
For starters I need one period of each frequency component (maybe 1.5-2 periods of each freq component : will be determined later upon the tests to find desired sweep approach to get best response).
The sweep should step through the frequency in steps of 1Hz(sequential 500,501,502...4999,5000 Hz) or say 5Hz steps(a variable value) so the sweep will be 500,505,510,.....4990,4995,5000 Hz.
What would be the best way to do this?
I've looked at bit banging digital pins,using tone functions, using the PWM freq generation.
I think PWM is probably the way to go.
I've got a 16Mhz arduino nano , below is the code I've managed to scrounge from looking at several replies/similar posts in this wonderful forum:
void freq_sweep()
{
int half_cycle count = 2 // for 1 freq period fOCnx=fclk/2*N*TOP
//Apparently the TOP value needs to be TOP/2 because of dual slope particularity ??
// so i calculated the increment for 1 frequency increments, the TOP deccreases by 64 (63.75)
TCCR1A=0;//reset the register
TCCR1B=0;//reset the register
TCNT1=0;
TCCR1A=0b01010011;//COM1A0,COM1B0 are 1, COM1A1, COM1B1 are 0
//also WGM11, WGM10 are 1
TCCR1B=0b00010001;//WGM13 is 1 and WGM12 is 0 with no prescaler CS10 is 1
for (int i= 32000;i>=3200;i=i-64) // TOP value needs to be decremented by 64 for each freq period increment of 1Hz
{
OCR1A=i;// compare value used to create a frequency sweep
}
// turn off the freq sweep when done
TCCR1A &= ~(1 << COM1A0);
}
Does this code make sense ? Is this the right approach? Alternative better approach?
Is this an acceptable way to create a frequency sweep? How is frequency sweep usually generated?
Is there a way to create a complimentary waveform of the above in another pin?
Once the sweep is sorted I will be working at doing the frequency measurement (will need to amplify the resonant freq response using op-amps (as it will be very weak ac signal in mV and do a pulse count of say 100 pulses and get the time taken as resultant freq). But its for later.
If you can find two potentiometers, I would use them for coarse and fine frequency. frequency = 100 + analogRead(A0) * 500 + analogRead(A1);
That will give you a range from 100 Hz to 6238 Hz which will easily cover your desired range. The coarse knob will jump by 500's and the fine knob will step by 1 Hz.
You can use tone(pin, frequency); to play the tone and display on Serial Monitor ever couple of seconds.
I am trying to do a continuous sweep . each sweep will be completed within few hundred milliseconds. it will go through square waves between 500-5000Hz.
something like this
I thought of using PWM instead of tone library because I thought I can more efficiently control the sweep nature.
after the sweep is completed I need to wait around a hundred ms to measure the resonant freq.
I am assuming these intermittent breaks are due to the prescaler being changed. Or do you think its an issue caused by my logic analyzer ans should not appear??
Is there a way to get smoother transitions?
Also is it possible to get the same complimentary wave in another pin 11? If so how would we go about doing it?