pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
thefatmoop
 
Posts: 352
Joined: Tue Aug 19, 2008 4:36 pm

pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by thefatmoop »

I'm trying to measure inductance with a resonating LC circuit and turning the sinusoidal wave into a square wave with a comparator. I charge the cap and disconnect the 5v, then press down the inductor switch.
Circuit:
http://i54.tinypic.com/2lntzwz.jpg

I don't have an oscilloscope, but it should look like this (comparator on just a signal generator)
http://i55.tinypic.com/30dedma.jpg

Well pulsein(11, HIGH, 500) may return 39us. Now since it's just a resonating circuit, the duty of the square wave is 50%, so 2*39uS is the period. Calculations below show that this isn't correct.
Last edited by thefatmoop on Tue Jul 19, 2011 3:03 am, edited 5 times in total.

thefatmoop
 
Posts: 352
Joined: Tue Aug 19, 2008 4:36 pm

Re: Problem with pulseIn on resonating LC circuit

Post by thefatmoop »

Is the PulseIn() accurate when working with measurements 10-500uS? I checked with an arduino using a crystal and had the same readings.

Here's a picture with a larger inductor just to show that the circuit works.
http://i55.tinypic.com/fvwc2d.jpg
I initially thought that the pulseIn() value would be a tad small since the square wave is slightly shaped like a trapezoid. Just testing a few inductors, it seems that the inaccuracy is proportional to the pulseIn() reading, and not a added in constant due to the trapezoid signal.

Measured square wave frequency is about 1/20uS = 5.0khz.
Calculated sinusoidal frequeny 1/21.8 = 4.6khz
Arduino is reporting on the square wave 1/(2*35uS) = 2.86khz

Hauled out the scope from the 80s that RARELY works. So the measured could be off just because the scope hasn't been calibrated, but it's pretty good.
Last edited by thefatmoop on Tue Jul 19, 2011 3:35 am, edited 2 times in total.

thefatmoop
 
Posts: 352
Joined: Tue Aug 19, 2008 4:36 pm

Re: pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by thefatmoop »

It's the op amp's fault.

http://i52.tinypic.com/20kd5i8.jpg
this was with a very small inductor to show what is happening.


If anyone has any comments whether or not all op amps will be prone to this please explain. I'm just going to plot data and do a function fit to calculate the correction needed

thefatmoop
 
Posts: 352
Joined: Tue Aug 19, 2008 4:36 pm

Re: pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by thefatmoop »

post will be on reibot.org to walk through and measure inductance

User avatar
brucef
 
Posts: 215
Joined: Tue May 03, 2011 4:51 pm

Re: pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by brucef »

I'm definitely not an expert, but since nobody else has chimed in...

I wonder if the issue is that you're using an op amp where you perhaps should be using a comparator? Wikipedia has some commentary on the disadvantages of an op amp comparator that may be relevant here.

thefatmoop
 
Posts: 352
Joined: Tue Aug 19, 2008 4:36 pm

Re: pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by thefatmoop »

BruceF wrote:I'm definitely not an expert, but since nobody else has chimed in...

I wonder if the issue is that you're using an op amp where you perhaps should be using a comparator? Wikipedia has some commentary on the disadvantages of an op amp comparator that may be relevant here.

Yes you are correct! "A dedicated voltage comparator will generally be faster than a general-purpose operational amplifier pressed into service as a comparator. "

I'm using the op amp as a comparator, but it's not a comparator IC. The only experience that I have with true analog circuits, such as the op amp, is from my circuit analysis class...I'm not to keen on the subject.

i'll go back to radio$hack and buy a comparator chip

thefatmoop
 
Posts: 352
Joined: Tue Aug 19, 2008 4:36 pm

Re: pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by thefatmoop »

I bought an LM339 quad comparator. datasheet: http://www.national.com/ds/LM/LM339.pdf

Seems simple to wire up, but i'm having difficulties getting it to work. does gnd need to be a negative voltage? the LC circuit goes from about -10v to 10v.


How it's wired:
http://i55.tinypic.com/jfgjkl.jpg
and i'm using the 1st op amp.


edit: I do have a pull up resistor (3.3k) on the output of the comparator before the diode

thefatmoop
 
Posts: 352
Joined: Tue Aug 19, 2008 4:36 pm

Re: pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by thefatmoop »

nevermind got it working. I get the same results with the LM339. It does the job better when looking at it on the scope

User avatar
brucef
 
Posts: 215
Joined: Tue May 03, 2011 4:51 pm

Re: pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by brucef »

If I understand correctly, the output of the comparator looks good on the scope but you're not getting the measurements you expect in the Arduino?

thefatmoop
 
Posts: 352
Joined: Tue Aug 19, 2008 4:36 pm

Re: pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by thefatmoop »

I went ahead and calibrated my oscilloscope (big pain since it's about 30 lbs and is completely analog)

With the LM339, the arduino and oscilloscope agree perfectly. The math is a bit off, but I believe that my "120" marked inductor is really 120uH and not 12uH. The arduino is saying it's 121uH, so if it is 120 then that's a very good reading. If i manually read the frequency it agrees with what the arduino comes up with on the pulseIn()

I'm writing a guide which will eventually be available on http://reibot.org/2011/07/19/measuring-inductance/

User avatar
brucef
 
Posts: 215
Joined: Tue May 03, 2011 4:51 pm

Re: pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by brucef »

thefatmoop wrote: I'm writing a guide which will eventually be available on http://reibot.org/2011/07/19/measuring-inductance/
Awesome, I look forward to reading it!

thefatmoop
 
Posts: 352
Joined: Tue Aug 19, 2008 4:36 pm

Re: pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by thefatmoop »

Thank you adafruit for putting my post on your blog <3 Always fun to get ~600 hits above average

I'm shocked that multimeters don't feature inductance measurement when it's so easy to do. I guess i can see why if the goal was to measure .5uH-1H with 1% tolerance. Most multimeters less than 50$ don't use much of a micro controller.

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by adafruit »

its a great tutorial! people often ask about this very thing

apappano
 
Posts: 1
Joined: Mon Feb 04, 2013 9:47 pm

Re: pulseIn() inaccurate on resonating LC circuit -- Boarduino

Post by apappano »

Ok Gents,

I know this is a late visit since this post is quite old...
My girl took over a drive thru for her coffee shop. They have an induction loop detection system installed, but pulled out the controller box when they left.
I'd love to hook it up to ring the old gas station bell I bought her, it just has a normally open switch to kick it. Already set up a relay for that part.

So I built your LM339 circuit and have it all hooked up to an arduino... But i know I'm missing something from here.Probably a lot of somethings. Any guesses to whatto do next? :?

Any help/links/direction/divine intervention would be appreciated.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”