Products > Embedded Computing

Strange values while I'm measuring frequency with arduino

<< < (4/6) > >>


--- Quote from: dariodario on April 22, 2019, 10:41:35 pm ---Kleinstain: At the moment no other routine are run in my code, only the gammon frequency routine. So only one (visible to me) interrupt is generated. I instead was thinking at the fact that the Serial library could get some delay that disturb the capturing time code. I could copy the frequency values in a long array and then print it in a text file at the end of the program, avoiding the Serial library load. What do you thinks.

--- End quote ---

Kleinstein is exactly correct, that code has a nasty race condition since the elapsed time consists of a hardware counter which is read on a rising edge interrupt combined with a timer overflow counter that gets updated in a separate overflow interrupt.  Fixing this will need a solution very much like reading a 16 bit timer value on the old PICs that didn't have latches, you read timer and overflow flags in a loop until the most significant part (the overflow) hasn't changed during a single loop.

Something like this should work, but note I have not tested this code.  This will add a small amount of jitter to the timer reads but it's unavoidable if the race condition is to be fixed.

--- Code: ---void isr ()
    unsigned int counter;
    unsigned char overflow;

    do {
        overflow = TIFR1 & bit (TOV1); /* Read the overflow interrupt flag */
        counter = TCNT1;               /* Read the 16 bit hardware timer */
    } while( overflow != (TIFR1 & bit (TOV1)) );    /* Read the overflow interrupt flag again and keep going if it doesn't match */

    /* If an overflow has occurred we handle it here
    if( overflow )
        TIFR1 = bit (TOV1);  /* Clear the overflow interrupt flag to prevent double counting */
        overflowCount++;     /* Increment the overflow counter */
    /* Rest of rising edge interrupt code here. */

--- End code ---


--- Quote from: dariodario on April 23, 2019, 09:32:06 pm ---I tried and it is better than an android app you can find on the app store because the output is about 0.5V RMS compared with 0.1-0.2V RMS from my smartphone.

I used the sine wave signal to drive the LM393 input and I found that

1. The C4 is still necessary to the output to be correctly measured. Why?  |O
2. The Spike are still present to the output, so they are not depending on the ac generator I'm using. See below.

About the point 2 I found that the spike measurement was generated by something related with the Gammon routine. I changed the routine with a less precise (max allowed frequency is 4kHz) and the problem was almost solved. So now I need to found another library or modify the Gammon one (I don't know how).

 p.s. While I made this post other answer arrived. So sorry to be not updated.


--- End quote ---

please post the code which you are using to measure the frequency. Also try the 3 resistors + diode schmitt trigger which I posted and let me know if it converts sine to square wave like intended.

Actually you need to cut the pulse on the rising edge as well if your op amp is not powered from 20v. so you need 2 schottky diodes. let me know if you want an updated schematic.

I tried the mikerj code with the PC sine generator and the things go better, but still at the (relatively) high frequency there are some  incorrect measures. The big spikes are disappeared now, I attached some examples. I think the PC generator is quite stable although could have some %error, but not variable with the time.

I also tried a code that measure the rising edge for 1 sec window, so directly calculate the frequency. This code work almost well with a 0.2% error but it has the downside that I need to wait 1 second to receive the measures, that is a bit too long for my application. Cutting the window measurement to 500ms downgrade the error % as expected. I would need an update measure each 0.2-0.3 second.

OM 2220: I cannot send the square wave directly to arduino pin because it expects for 0-5 volt transition (0-about 3V), while the PC generate a 0.5 RMS voltage. I want try your circuit, but I would ask you if the IC I have to use is the (half) LM393 or an op-amp like the TL081 I have. The LM393 has an open collector output.

Thanks to all for the help.

p.s. I'm seriously think to buy a RIGOL 2 channel scope or the similar Siglent.

here is the circuit you must use with your sensor input to convert from 40v peak to peak sine, to 5v peak square wave (these are op amps, not comparators, but you should get similar results non the less):

if you want to use the online square wave generator, use this circuit (again an op amp, but it can be a comparator as well):

the two resistors near the op amp are selected so the voltage between them is less than the maximum of the input signal. in this case maximum output is 5v, so the voltage across the 10k resistor is 5*(10/110) which is about 0.45v and we know the input has a maximum peak of 0.7v (0.5v rms = 1.4v peak to peak, so it goes from +0.7v to -0.7v). just select those values carefully if you change the input voltage.


[0] Message Index

[#] Next page

[*] Previous page

There was an error while thanking
Go to full version