I wonder if one of you could help me think of a way to plan some code for reading frequency and detecting a change, i'm using an Arduino Due and the frequency change will come in the form of a change in indutance of an LC oscillator.
It's basically going to be a metal detector for a traffic light system. At the moment I'm thinking like this:
define certain_change;
define change_of_freq;
define (other things too);
read analogin() for 'xset' amount of seconds
take average_frequency
store average_frequency
read analogin for 'xrun' at certain intervals
'xrun - xset' = change_of_freq
if change_of_freq = certain_change then run_certain_function_to_make_decisions
(etc,)
Once I can read a change of frequency it's all about the decisions, failsafe checks and timings isn't it.
The idea is basic but I don't think it's as easy as I think, or is it?
I'm still finding it hard to get my head around the reading of frequency and how often I should take it with the speed of the processors, i.e interrupts?
A frequency can be measured directly (cycle time), or from a voltage representing the frequency (RC filter and rectifier). I'd try pulseIn() for getting the cycle time, and find out whether the resolution allows to distinguish relevant changes.
For digital inputs oscillating at less than 100khz, using an interrupt shouldn't overwhelm the Due. You could write a very simple function that only records input rising (or falling) count and time in microseconds.
Then in your main loop, just poll your readings (current and previous) at whatever rate you require. There'll be enough information available to calculate pulse period, frequency, number of pulses in measurement, measurement duration, change in frequency, change in duration, etc.
First, you would need to condition the signal so that it will not damage the Due (0-3.3V).
Hi thanks for the reply. I forgot to mention that it will be an AnalogIn because the change in frequency is due to a change of inductance from a colpitts oscialltor.
The frequency will be quite high, but the counting only needs to be about 0.01/s. I just don't know the theory of sampling analog signals. I'm guessing I count the highest value, and do it again and measure the time between them?
ia there a function to detect the peak of analog signals? I would have assumed the peak would be a value of 1023 if i need to condition a 5v signal to peaked between 0v and 3.3v. Is it not possible to set the aref to be the max value of the analog output i.e 5v, or is that too much for the Due too?
Or will i need to program this manually by comparing the last one and checking if its higher?
how do I know how often it reads each value?
The ADC is quite slow, you'll have a problem with high input frequencies. Better add an comparator (sine to rectangle converter) and read the digital signal (1 pulse per period). Then you can also use an timer to count the pulses in hardware, faster than with interrupts or polling the signal.
Back in the days we did something like this with a (4046) PLL.
Crystal oscillator with divider train into one input of the PLL.
Free-running oscillator with divider into the second input of the PLL.
PLL's error output to a varicap of the free-running oscillator.
The PLL's error signal holds the information of a detuned free-running oscillator.
Curious how this is going to be solved only in software.
Leo..