pulseIn() inaccurate on resonating LC circuit -- Boarduino

by thefatmoop on Mon Jul 18, 2011 7:07 pm

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 2:03 am, edited 5 times in total.
User avatar
thefatmoop
 
Posts: 329
Joined: Tue Aug 19, 2008 3:36 pm

Re: Problem with pulseIn on resonating LC circuit

by thefatmoop on Tue Jul 19, 2011 1:29 am

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 2:35 am, edited 2 times in total.
User avatar
thefatmoop
 
Posts: 329
Joined: Tue Aug 19, 2008 3:36 pm

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

by thefatmoop on Tue Jul 19, 2011 11:35 am

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
User avatar
thefatmoop
 
Posts: 329
Joined: Tue Aug 19, 2008 3:36 pm

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

by thefatmoop on Tue Jul 19, 2011 11:37 am

post will be on reibot.org to walk through and measure inductance
User avatar
thefatmoop
 
Posts: 329
Joined: Tue Aug 19, 2008 3:36 pm

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

by BruceF on Tue Jul 19, 2011 11:43 am

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.
- Bruce
BruceF
 
Posts: 212
Joined: Tue May 03, 2011 3:51 pm

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

by thefatmoop on Tue Jul 19, 2011 11:48 am

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
User avatar
thefatmoop
 
Posts: 329
Joined: Tue Aug 19, 2008 3:36 pm

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

by thefatmoop on Tue Jul 19, 2011 2:39 pm

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
User avatar
thefatmoop
 
Posts: 329
Joined: Tue Aug 19, 2008 3:36 pm

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

by thefatmoop on Tue Jul 19, 2011 3:19 pm

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
thefatmoop
 
Posts: 329
Joined: Tue Aug 19, 2008 3:36 pm

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

by BruceF on Tue Jul 19, 2011 5:44 pm

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?
- Bruce
BruceF
 
Posts: 212
Joined: Tue May 03, 2011 3:51 pm

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

by thefatmoop on Tue Jul 19, 2011 6:40 pm

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
thefatmoop
 
Posts: 329
Joined: Tue Aug 19, 2008 3:36 pm

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

by BruceF on Tue Jul 19, 2011 7:18 pm

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!
- Bruce
BruceF
 
Posts: 212
Joined: Tue May 03, 2011 3:51 pm

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

by thefatmoop on Thu Jul 21, 2011 12:13 am

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.
User avatar
thefatmoop
 
Posts: 329
Joined: Tue Aug 19, 2008 3:36 pm

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

by adafruit on Thu Jul 21, 2011 6:56 pm

its a great tutorial! people often ask about this very thing
User avatar
adafruit
 
Posts: 11642
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

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

by apappano on Mon Feb 04, 2013 8:53 pm

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.
apappano
 
Posts: 1
Joined: Mon Feb 04, 2013 8:47 pm