pulseIn() inaccurate on resonating LC circuit -- Boarduino Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

pulseIn() inaccurate on resonating LC circuit -- Boarduino

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: 350
Joined: Tue Aug 19, 2008 4:36 pm

Re: Problem with pulseIn on resonating LC circuit

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: 350
Joined: Tue Aug 19, 2008 4:36 pm

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

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: 350
Joined: Tue Aug 19, 2008 4:36 pm

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

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

thefatmoop

Posts: 350
Joined: Tue Aug 19, 2008 4:36 pm

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

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: 215
Joined: Tue May 03, 2011 4:51 pm

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

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: 350
Joined: Tue Aug 19, 2008 4:36 pm

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

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: 350
Joined: Tue Aug 19, 2008 4:36 pm

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

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

thefatmoop

Posts: 350
Joined: Tue Aug 19, 2008 4:36 pm

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

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: 215
Joined: Tue May 03, 2011 4:51 pm

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

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/

thefatmoop

Posts: 350
Joined: Tue Aug 19, 2008 4:36 pm

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

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: 215
Joined: Tue May 03, 2011 4:51 pm

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

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.

thefatmoop

Posts: 350
Joined: Tue Aug 19, 2008 4:36 pm

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

Posts: 12131
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

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

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 9:47 pm

Please be positive and constructive with your questions and comments.