Easily measuring inductance with Arduino

bidirectional analog to digital - using LM741 as comparator.

So you need to make or measure an inductor, but you don’t have an oscilloscope or signal generator? Measuring inductance with a handful of cheap common parts is certainly possible. I’ve verified this method is accurate with a scope from 80uH to 30,000uH, but it should work for inductors a bit smaller or much larger. There are some contingencies to keep in mind when it comes to measuring inductors — more on this in “Other Notes:

There are three components that you’ll probably have to buy, but they can be picked up at your local Radio$hack: LM399 and two 1uF non polar capacitors – look at the schematic. If you don’t want to shop at radio$hack, there is a list of products at the end that should work.

No Arduino?

There is 1 digital output and 1 digital input, so this will work with most micro controllers. The output works better with a high current and uses ~33mA at 5V. The only thing left is to measure the rising edge to falling edge time on a square wave. You may want to look at the code if you’re unsure about how to enter the equations, you too can measure inductance with a microcontroller!

LM741, LM339 comparison and a picture showing bell like behavior.

A short lesson on the theory:

An inductor in parallel with a capacitor is called an LC circuit, and it will electronically ring like a bell. Well regardless of the frequency or how hard a bell is struck, it will ring at it’s resonating frequency. We will electronically strike the LC bell, wait a bit to let things resonate, then take a measurement. There is some internal resistance so this is really an RLC circuit, and I’ll talk about this more in the math.

Now micro controllers are terrible at analyzing analog signals. The ATMEGA328 ADC is capable of sampling analog signals at 9600hz or .1ms, which is fast but no where near what this project requires. Let’s go ahead and use a chip specially designed for turning real world signals into basic digital signals: The LM339 comparator which switches faster than a normal LM741 op amp, but there will be a schematic for the LM741 too.

As soon as the voltage on the LC circuit becomes positive, the LM339 will be floating, which can be pulled high with a pull up resistor. When the voltage on the LC circuit becomes negative, the LM339 will pull its output to ground. I’ve noticed that the LM339 has a high capacitance on it’s output, which is why I used a low resistance pull up.

Math:

LC equations

Since our wave is a true sinusoidal wave, it spends equal time above zero volts and below zero volts. This means that the comparator will turn it into a square wave with a duty of 50%, and pulseIn(pin, HIGH, 5000); will measure the time in microseconds elapsed from rising edge to falling edge. This measurement can then be doubled to get the period and the inverse of the period is the frequency. Since the circuit is resonating, this frequency is the resonating frequency.

To the left are the equations where f is the resonating frequency, c is capacitance, and L is inductance. Solving for inductance will result in the last equation

Since this is an RLC circuit due to internal resistance, it won’t change any characteristics of the resonating frequency. The RLC will still resonate, but the amplitude will die out. With a low resistance the RLC will tend to latch onto the exact resonating frequency quicker. For you EE’s think of the frequency response of an RLC with low resistance versus high resistance.

Parts that should work:

review the circuit before buying anything. All resistors are 1/4 watt, but anything will work.

LM339

Using LM339 (works better at high frequency)

The Circuit:

Pick whichever circuit is better for you, but the one using the LM339 is better. Both the capacitors are 1uf metalized film, but anything that is non polar will work. It will need to be very close to 2 uF though. You can not use a capacitor that marks which connection is ground. One thing you may notice is that the LM741 is geared for analog computing. This means that it requires a negative voltage on it’s V- pin. If you don’t have a power supply that offers this, use two AA batteries to go 3v below ground as shown. The LM339 doesn’t need this and there is no problem inputting a negative voltage. Remember that the LC circuit will vary above and below ground. Here’s a picture of the breadboard.

Using the common LM741 op amp. D2 is a 1N4001 too.

Code:

Code for Arduino – With large inductors, you may need to increase the timeout on pulseIn() from 5000 to 10000. If you’re having issues with very small inductors – under 200uH – increase the delayMicroseconds() right before pulseIn() to a larger value ~500uS.

Other Notes:

Not accurate enough? If you look at the equation and you’ll see that the capacitor’s tolerance is key. Expect your results to be accurate within ~10% with a 10% tolerance capacitor. What does this mean? Let’s say you’re using a 10% tolerance capacitor, and the Arduino spits out that the inductor is 1000uH. Well this means that the inductor is in between 900uH and 1100uH. Think of a bell curve if you’ve taken a statistics class – most capacitors with 10% tolerance will be under 10%. (pdf)

If you require a very accurate measurement for a system running at a very high frequency, then this method is definitely not for you due to parasitic capacitance, which isn’t taken into account. This method uses low current to measure inductance, so saturation characteristics will be unavailable (measurements will be taken in an unsaturated state.) This won’t be an issue for most people.

There is this wonderful thing called permeability. Filling an inductor with certain materials changes the inductance without changing the coils. This is similar to mutual inductance in transformers. Ever notice how high frequency transformers are made with nearly non conductive ferrite, and 60hz transformers are made with an iron/steel?

Another method that doesn't work well with Arduino.

You could make a metal detector. Inductors that don’t have closed fields — not magnetically isolated — will change their inductance when something with a different permeability than air is near.

If you have access to fast sampling rates, you can use the method on the right too, but it will require a p type mosfet to really pump some current into the inductor and R1 less than an ohm or so, but greater than the equivalent series resistance of the inductor. This method will probably run into saturation issues if the sample isn’t taken quickly, but if you’re smart about it you should be able to get information about the saturation characteristics.

And there you have it! This is the most difficult part to build on a diy LCR meter.

About these ads

About Moser
Electrical Engineering student who loves to bike!

63 Responses to Easily measuring inductance with Arduino

  1. Pingback: Easily measuring inductance with Arduino « adafruit industries blog

  2. Pingback: Electronics-Lab.com Blog » Blog Archive » Easily measuring inductance with Arduino

  3. uber dude says:

    Looks like there is a bug in your program:

    the line
    pulse = pulseIn(9,HIGH,5000);//returns 0 if timeout

    Should be

    pulse = pulseIn(11,HIGH,5000);//returns 0 if timeout

    since you define pin 11 as the input pin.
    Thanks for the nifty idea, this will be really helpful for me.

    • Moser says:

      Fixed.
      Yes Thank you for pointing that out I probably changed that when I went from the LM741 to LM339. puseIn() will change the pin to digital input anyway, which is probably why I didn’t catch it.

  4. uber says:

    Built the circuit today and it works great! Just a tip on improving the accuracy – most digital multimeters have a capacitance measurement. Just measure your caps and use that. Not sure what is now driving the accuracy now, DMM tolerance? timing of the LM339? Either way, it is more accurate by a couple orders of magnitude.

    Also, not that the code needs much optimization based on its simplicity, but I moved the capacitance definition to the void setup() function so the little microcontroller isn’t constantly redefining it.

    • Moser says:

      Good to hear it! you can use different capacitance, and a larger capacitance will be able to measure smaller inductors. The main drawback is that in order to get energy into smaller inductors is that it requires much more current. Could be done with a p type mosfet and the mosfet ESR wouldn’t change resonation.

      Yes if you have a DMM that can measure capacitance, then that will be the best thing to use for capacitance. My multimeter was about 10$ lol and doesnt.

      • Rob says:

        Thank you for your article and code.

        What would the schematic look like with the p-channel mosfet included?

        I’m also interested in measuring smaller inductances << 20 uH – down to 1uH or less.

      • Moser says:

        using the P channel mosfet: Vin on the image above would be connected to the drain on the p channel mosfet
        p mosfet schematic

        I’ve thought of a few ways how to measure very small inductances thinking in terms of the frequency domain rather than a resonanting circuit. This would allow a circuit measure at different frequencies (like 100kHz, which i think is the standard for measuring inductors.)

        I may get to it this summer, and I will definitely post results and a guide!

  5. WestfW says:

    Can you use the internal comparator present on the AVR?

    • Moser says:

      The main thing to keep in mind is that the LC circuit may go 10-15 volts above and below ground, so this would be somewhat of a concern for the micro since they’re picky about voltage. Did some searching around and it looks like a no, but definitely send an update if you get it working!

      http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1163394545

      and it’s on section 22.1 of the atmega328 datasheet

      • Nash Hole says:

        i’d start an attempt like this with a pin change interrupt and wheatstone bridge to scale up the signal… adjust the resistance of R2 or R3 to pull the signal within range… might have to use a fast switching zener to block out noise: negative end of the zener connected to the zener, current limiting R between zener and digital pin with resistance large enough to keep the current below whatever will fry the zener — if a digital output is used as the excitation voltage, and the zener can handle 40mA, then the R can be ignored. i don’t know how zeners affect oscillating circuits, though.

      • baz says:

        I successfully done it. You only need 10-20k resistor connected before arduino comparator positive input. It is required to drop high voltage spikes through internal clamping diodes, which are exists inside any atmega chip. So, it is safe enough.
        The only one tricky thing, is to write precise procedure to count time between comparator change it’s state. I’ve done it using inline assembler with interrupts disabled. The results are great. I checked accuracy using some marked inductors, and it’s about 2-5%.
        Good luck!

  6. Pingback: ArduinoProjects.INFO » Easily measuring inductance with Arduino

  7. Pingback: Using an Arduino to measure inductance - Hack a Day

  8. Pingback: Using an Arduino to measure inductance

  9. Pingback: Using an Arduino to measure inductance | The Depot of Talk

  10. Njay says:

    Hey very neat idea :)! As for trying to remove the comparator, my suggestion is to feed the oscillation through a capacitor to the center of a Vcc resister divider by 2, then a current limiting resistor and to the AVR’s comparator.

    Feeding the signal coming from the capacitor into the resistor divider will make the center of the resistor divider oscillate with center point equal to Vcc/2.

    All AVR pins are protected by diodes going from GND to the pin and from the pin to VCC, which will clamp the voltage to within GND – 0.5V to Vcc + 0.5V, as long as you provide a series external resistor that doesn’t let go more than 1mA when clamping.

  11. bedesigns says:

    Very nice Idea and project ;) keep it up ;)
    Just got one small question, could it be used to measure values below 20uH? or it be out of scale?

    • Moser says:

      Make the capacitance larger which would decrease the frequency. In order to charge up the system you’ll probably need to provide more current, so try using a p type mosfet. remember U=1/2L*i^2 for an inductor.

      Possibly use a frequency to voltage converter like the LM2917? Sounds inaccurate though

      http://www.national.com/mpf/LM/LM2917.html

  12. Njay says:

    I have a challenge for you guys :):

    1) Generate a sinusoidal wave, Vs peak voltage, as perfect as possible, with frequency (f) 1 to a few KHz

    2) Apply that wave to an unknown capacitor (Cx) in series with a low value resistor (Rt) (allow at least a few 10ths of mA, a few 100ths is better)

    3) Sample voltage at the input (sinus wave) and at the resistor, to find peak values, time-stamped; the resistor measurement is to measure current (It) and timestamps are to measure phase difference (a). Current peak will come before voltage peak.

    4) Apply formulas to measure capacitor value and ESR, using complex impedance in Ohm’s Law: Z = V / I, where V = Vs + 0.j and I = It x cos(a) + It x j.sin(a); ESR = It x cos(a) – Rt (that is, real part of impedance minus Rt); capacitor reactance, Xc = It x j.sin(a) = 1 / (2 x Pi x f x Cx) => Cx = 1 / (2 x Pi x f x It x j.sin(a))

    5) If successful, do the same to measure inductance

  13. Pingback: Made my Inductor » Transportation Scholar

  14. Khan says:

    Dear Moser,

    I have made it according to your instruction, and it is working!!

    Thanks a lot!!!

  15. rht says:

    hey i’ve tried making ur circuit….but the inductance and frequency is varing ,in the serial monitor……..am new with
    electronics……what am i supposed to do???please guid me…..am stuck….am using the same lm339…

    • Moser says:

      It’s best to test the circuit with an inductor which you know the inductance. Have you tried this? Are you using electrolytic capacitors on the resonating part?

  16. rht says:

    yes i am using electrolytic cap…..

  17. oh says:

    Thanks for the article. One question, though: The datasheet of LM339 says that the input voltage range in either differential input ports (+ and -) must be -0.3…36V. However, to me it seems that the LC oscillations can cause the + input to go below -0.3V?

    • Moser says:

      You’re right, but i didn’t have problems and no one complained which is interesting. Put two resistors (1k should do) in series on the + differential input. In between those two resistors put a diode with the cathode connected to ground (striped end). Use a 1N4148 but that would let it go down to -.6v, a schottky diode would keep it closer to -.3

      • john212 says:

        Help me out, I am confused. If the 1N4148 diode is connected with the cathode connected to ground, wouldn’t that send the positive signal you want to use as the input to ground?

      • Moser says:

        Youre only interested in the positive portion since the positive and negative are the same

  18. Pingback: Tube_Exploratie3 | TIII

  19. Dave says:

    Can you measure inductance using a 555 timer in place of the LM339? It seems reasonable as the 555 would trigger high/low on the 1/3 and 2/3 voltage, measuring the same length of time. I tried wiring something up, but didn’t manage to get it to work.

    • Moser says:

      Surprisingly i’ve never really done anything with the 555. If you can get the 555 to act as a comparator then yeah you can use it. Make it simple and just use a comparator chip since it will activate as soon as the signal goes onto the positive voltage ranges.

  20. diego says:

    Did you choose the LM339 for some particular (functional) reasons, or if I want to build a (phisically) small circuit, do you think that also a LM311 can do the job?

  21. A. Pappano says:

    Duplicating my post from Adafruit here just in case…

    Looking for a little guidance…

    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.

    • Moser says:

      That is awesome! One of my dreams in life is to own or partially own a coffee shop – even if I don’t make a profit! Anyway were you able to measure an inductor with a known inductance? It might not work for the drive through inductor and my thoughts are that: There isn’t enough current going through the drive through inductor to make a magnetic field large enough to reach out to the car’s metal. If it can’t reach out that far, then the car’s metal won’t affect the inductance. I’m not sure, there are lots of ways to measure a ‘change’ in inductance though that aren’t too difficult. You could stimulate the circuit more aggresively with a mosfet and use a larger (non electrolytic) capacitor – and then you’d probably have to add some protection circuitry to cut off the tips of the resonating sinusoid (you don’t care about those anyway, just 0volt intersection). If this is gibberish then look around for one of the drive through sensor boxes :P

      http://vehicle-counters.com/TC-BL44.htm

      you could also get a weather proof ultrasonic sensor and have it pointed at where the car will be.

      http://www.adafruit.com/products/1137

      sparkfun sells one too

      • A. Pappano says:

        I actually have a zero cross intersection detection circuit for the home roaster I’m going to run off an Arduino… Might just be the thing. I’m setting up my oscope this weekend to take some tests and see what I get. Funny you mentioned the prox sensor… I was thinking about that earlier this morning.

        I’ll try the MOSFET amp and the ZCD and see if that works. If you can recommend a good amp circuit link, that’d help. Here’s the ZCD I’m going to use:

        http://goo.gl/oYkBV

        Cheers!

  22. raul says:

    I tried with two 0.1uF caps and with a 1mH inductance and it worked like a charm :) Thanks for sharing!

    A happy reader :)

    • raul says:

      Actually I realized a single power supply and it still works. I’m using a lm324 so in the negative lobe the opamp ouputs zero and in the positive it outputs 5v.

  23. Pingback: Kaip pasidaryti henriometrą | Darau, blė

  24. raul says:

    Hi I improved the measurement of the resonance frequency, I hope you like it!

    http://codinglab.blogspot.be/

    Thanks for your great article!

  25. Andrew says:

    Very well written article! I tried it with an lm471 I had lying around and it won’t output anything until I disconnect or reconnect the 3 volt as batteries- it outputs something different each time though depending on how fast I disconnect and reconnect?? Any ideas?

  26. Mayan says:

    Is it possible to measure capacitance/capacitor (changing code) with this circuit?

    • Moser says:

      Yeah but there are much easier ways how to do this. Modify the code from my post: “measuring resistance with a digital i/o…” also most multi meters can measure capacitance.

  27. Mayan says:

    Yes. That is simple and good for resistance or capacitance :).

    I made this inductance meter earlier, and works well. I think this can also measure capacitance without changing any hardware design. Can you please take a look here – http://www.microsyl.com/index.php/2010/03/29/inductor-capacitor-meter-lcmeter/ ?

    Best regards.

    • Mayan says:

      Sorry, my mistake. Tested, and this does not work for capacitance. Anyway measuring capacitance is not much important.

  28. Pingback: Naminis Arduino shield’as | Darau, blė

  29. Jdub1581 says:

    Awesome project! I finally got it working (I think) .. A while ago I gought a basic component pack at RadioShack and it came with a few inductors : 33mh,10mh, 1mh, 470uh etc.. They are all marked, however when I put them into the circuit they seem to only show 1/2 of the marked value… for example the 470uh measures at 227uh… I used your code verbatim, so I am wondering If I may have something wrong in the circuit, or would you think the inductors are marked incorrectly(though I doubt it)?

    • Moser says:

      if you didn’t adjust anything in the code, then i’d say your capacitors are 2uF each and two of those in parallel will be 4uF which would end up dividing your inductance by two. Check your caps and make sure they sum up to agree with the code.

      • Jdub1581 says:

        I bought the the 1uf metalfilm caps as you describe above from radioshack… which is what was confusing me… im fairly new to electronics… guess ill load a cap meter code and see what they say… Thanks for the reply!

  30. dpczic says:

    Like the project — got it working fine with one problem. It is overly sensitive to temperature. I have wound coil in a sled to drag across the ground. It’s tethered to an Arduino with GPS so that variations in soil can be logged. I left it in the same place for several minutes to check repeatability and discovered that when the electronics were in direct sunlight the reading increased. When they were moved into the shade, the reading gradually fell.

    I brought it back into the house and tried letting a soldering iron hover near the 339 and capacitors. The reading increased. The 339 and caps are close together on the board so I can’t tell which is to blame (could be both for all I know). Any ideas on how to correct the problem? Here’s a link to the graph that alerted me to the problem: https://www.dropbox.com/sh/1awv4zu8jpa9g8v/-U5tIVL-W1

    • Moser says:

      I wouldn’t think the comparator would be affected that much, I’d expect the capacitors to be the main culprit. If you’re using a non crystal resonator or internal oscillator it could be a time error too. Anyway an easy solution would be to thermally stabilize the electronics.

      Now to get to the meat of the issue:
      This device isn’t going to produce a very wide magnetic field. The coil design is a part of that factor. More importantly the circuit pumps very little current into your coil, so it won’t sense permeability changes far enough away (although maybe you’re having good results other than the temperature instability)

      One solution is to add some support circuitry to pump more current in. Another would be to design a system based off how a true lcr meter works – feed in a sine wave and measure the peaks and phase shift when the current passes through an LR divider. Maybe you could even take the ad5933 and add some circuitry to increase current.

      Anyway this December or January I’ll have time to write up how a true lcr meter works and post my schematics with videos and whatnot

      • dpczic says:

        Is there a known circuit that can compensate for temp or another type of capacitor that can be used?

        I was trying to be brief earlier, but I’m putting .2 amps from an 18V source through the coil controlled by a P2N2222A transistor.

      • Moser says:

        Using the same inductor that you have on your sled, measure the inductance and temperature at room temperature, in your fridge, and in your freezer. Make sure to put the entire circuit (except the batteries) in each thermal environment and leave it for an hour to make sure the temperature evens out. Then graph those in excel and make an inductance versus temperature graph (inductance y, temp x) and do a ‘find trendline’ to make a function to compensate for temperature.

  31. hyperstition says:

    Thanks a bunch! This is truly handy and I will be making a quick little shield out of this for future use. I’ve only compared the results with one inductor so far but it seems to be ~ same value (to within tolerance of capacitors) :)

  32. Pingback: Videoton DC 2050E folytatás 2. | Blogom

  33. Justin says:

    So I made this circuit, and I’ve tried using both my arduino Uno and Micro but with both everytime I open the serial window it displays “hello” message but never anything else… I used all the components listed for the LM339 configuration… I used the example code, and messed around with delay time… Nothing. Help please?…

  34. Aleza says:

    Quick question: The measurement (of L and Freq) will to 100% dependant on the 2x 1uF Capacitors right?

    I am planning to measure L and freq for RFID antennas, but, then I will have to port that inductor with the capacitors right?

    • Moser says:

      yeah if you’re using the 125KHz or w/e antennas you’ll just need to enter in the right capacitor values and ensure that the resonance will be a frequency within the capabilities of pulsein(). if you’re using the 13.5MHz rfid then this won’t likely work for you.

      • Aleza says:

        I wanted to test 125khz antennas and 134.2khz antennas.
        The question was more in the lines of: lets say I am making an 125khz antenna, I wind the coil, I tested with this circuit and done, its tunned to 125khz.

        Then when I use this antenna, I will need to use the same C (2uF) right?

      • Moser says:

        Yeah as long as the pulsein() is within the range of the arduinos capability

      • Aleza says:

        Tested, made the circuit on a protoboard and worked perfectly, even made a 100 measurements and saw mean and deviation…. Really acurate!
        Too bad when I tried to make a pcb, I must have made something wrong… Its not working probably my fault. I can share the eagle files if you want to add them to your site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s