Dick Cappels' project pages http://www.projects.cappels.org

Return to HOME (more projects)
A Pretty Good LC Meter Based on the AVR Microcontroller
Calculates and displays L and C from oscillation frequency using reference components.

No relays, no range switching, a minimum of controls. And it is pretty accurate too!
Note: After reading this article,
check out the improved, modified An Even Better LC Meter...



The 2 line x 16 character LCD shows the calculated inductance and the oscillation
frequency. The frequency might be of interest because inductors with cores can appear to
vary in inductance with changing test conditions.


Files for this project available for downloading:

To make the complete project you will need at a minimum both of the hex files below. You can also download the source files if you wish to customize them.

The hex file containing code for the frequency meter - U3- ATTINY2313 or AT90S2313 2313LCmeter_070217A.hex
WINAVR C Compiler Source file 2313LCmeter_070217A.c
The WINAVR Project Folder 2313LCmeter_070217A_project.zip

The hex file containing code for the LCD driver chip - U4- ATTINY2313 or AT90S2313  LCDbuttons040904C.hex 
The assembly source code for the LCD driver chip  LDCbutons040904C.asm
The assembly source code for the LCD driver chip updated for newer assmblers LDCbutons040904C_updated.asm
The include file needed by the LCD driver chip   2x16lcd.inc

Introduction


There must be half a dozen similar LC meters on the web. Why didn't I just download one of them and use it?  I suppose part of the answer is that I wanted to do some of it myself. As nice as these other projects are, I wanted to make an LC meter that others could copy and get working without very much trouble. That meant that "exotic" components need to be kept to a minimum, and to me, that means no relays or hard-to-find special switches. It also means that the complete design needs to be spelled out in sufficient detail to allow anybody with basic assembly skills to put it together and make it work.

This instrument requires two precision components: A precision capacitor and a precision inductor. You only need to start with one precision component, either the reference capacitor or the reference inductor, and using this meter, you can select or adjust the other precision component.

In my case, I used a pretty high accuracy BK Precision inductance/capacitance meter and sorted through piles of inductors and capacitors to find those that had the lowest error. I then used those parts, a 1 millihenry inductor and a 0.01 microfarad capacitor, in this meter.

The basis of this project is several similar projects on the world wide web and some magazine articles before the world wide web was a common means of information interchange. Unfortunately, I am not able to determine the originator(s) of the concept, but I suspect that it is as old as radio. Another project on my web site, LC Determination by  Resonant Frequency Measurement, measures the resonant frequency of an L/C circuit, but the hardware stops at the frequency measurement. It does not proceed to calculate the unknown inductance or capacitance.

To convert from frequency to inductance or capacitance requires a pocket calculator or spreadsheet, and is fine if only taking a few measurements per day. Beyond that, the manual labor seems a bit much, and the time to complete a measurement and calculation becomes burdensome. As I found myself spending more time winding transformers in my Thailand lab for various power supplies that I longed for a meter that would read out directly in inductance and capacitance, such as I had in my lab in Arizona.

The only difficulty in getting the instrument to display inductance and capacitance directly was in organizing the assembly language code, and probably more importantly, properly scaling the operations. I had looked into it, and it seems to be too much trouble. It would take about as much time and energy to add this feature to the LC meter as it would to learn to use a C compiler. So, that's why this project is written in C.

Please excuse the fact that in some ways the source code looks more like assembly than C. This will improve with time.

It was a true pleasure to type:

inductance = (((numerator/(628*frequency))*(numerator/(628*frequency)))/capacitance);

and have the calculation performed and the result returned with high precision. It is a small difference from organizing assembly language code to do the same, but it is such small things that changes the way we do things.  Similarly, debugging was easier in C than in assembly language because I needed to look at and manipulate large numbers during debugging.

Basic Theory of Operation

An LC oscillator oscillates at the resonant frequency of a parallel LC resonant circuit. When measuring an inductor, a precision capacitor is switched in to the circuit. When measuring a capacitor, a precision inductor is switched into the circuit.

If the Q of the resonant circuit is about 10 or greater, the measurement error contributed by this factor will be less than 1%. Q is the comparison of the losses in the circuit, often the result of resistance in the circuit, with the reactance of the inductor and capacitor. The Q of good quality capacitors is usually not a problem - just keep away from ceramic and electrolytic capacitors and you shouldn't have a problem. The Q of the inductor is the one to watch out for. The first inductor that I used had a Q of 3 when measuring a 0.47 uf capacitor, and that made the error nearly 20%!

Q of the inductor is of greatest concern at the lowest operating frequency. In this application, this corresponds to the situation in which the highest value capacitor is being measured.  A 1 mH inductor resonates with a 1 uf capacitor at 5.035 kHz. The reactance of the inductor is 6.28 x 5.035 kHz x 1mH = 31.6 Ohms. In order to keep the error contributed by the Q of the coil to less than 1% of the measured value, the Q must be 10, so the resistance of the resonant circuit must be less than 1/10 of the reactance, or 3.16 Ohms. You can check the resistance of inductors you are considering for use as L2 with an Ohmmeter to find its resistance.

Before measuring, the circuit's offset reactance needs to be measured and removed, using a zero set procedure. Turn the meter on for a few seconds, to allow enough time for the analog circuitry and the readings to settle, then if measuring an inductor, short the Lx/Cx terminals together, or if measuring  a capacitor, leave the terminals open. Press the ZERO SET button and hold it down for a couple of measurement cycles. The LED blinks one time each measurement cycle. Release the ZERO SET button, and after the current measurement cycle is completed, the meter should read zero.

A convenient alternative means of shorting the Lx/Cx terminals during ZERO SET for an inductance measurement is to press and hold the SHORT button during the ZERO SET procedure.


Specifications

• Inductance from 0.1 uH to 50 H with 0.1 uH resolution.  Note: High resistance in inductors contributes to  measurement error.
• Capacitance from 1 pf to 1 uf with 1 pf resolution.  (Note: Resistance of the reference inductor limits accuracy with large capacitance values).
• The pilot light blinks once during the transmission of the results from each measurement.
• Resonant frequency is displayed in Hz.
• Measurement interval is approximately 1.1 seconds.

The actual range of the firmware extends to 300 Microfarads and 300 Henries, but with the component values in the present oscillator circuit, the oscillator is not capable of operating that slow. For values above about 1 microfarads and 50 Henries, I suggest increasing the values of C1, C2, C3, and L2, and increasing the frequency measurement time from one second to 10 seconds or more.

Accuracy is, for the most part, dependent upon the accuracy of the reference inductor, the reference capacitor, and the 4 MHz clock oscillator for U3. Resolution is not the same as accuracy. Note that you could measure a 25 Henry inductor and display will show it in 100 nanohenry increments (as an example: 25074164.6). The accuracy of the measurement could probably only be on the order of 1%, and take note of the frequency: at 25 Henrys, the oscillator only runs at 318 Hz and is sampled for only one second, so the granularity of the measurement, that is the smallest change in the measured value that can be detected, is found by taking larger of the difference in frequencies 1 Hz above and 1 Hz below 318 Hz, compared to 318 Hz.

Inductance corresponding to  317 Hz = 25.232 H   +248 mH
Inductance corresponding to  318 Hz = 25.074 H
Inductance corresponding to  319 Hz = 24.917 H     -157 mH

The resistance of the inductor will affect the inductance measurement slightly. I have some pc mounted power supply chokes with very poor Q - about 100 oHms for the 10 millihenry inductor. Its measurement seems to be off about 5%. Capacitors tend to have much higher Q than inductors, and unless you are making your own using low Q dielectric, it would not be worth worrying about how the Q of capacitors affects the measurement.

This meter assumes a lumped inductor and a lumped capacitor coupled directly against each other. If there is a lot of capacitance distributed over the inductance, such as in a delay line or transmission line, the resulting measurement will not be meaningful in terms of inductance of capacitance. This means that this is no the appropriate tool to measure the inductance of that 75 meter diameter lowfer loop antenna behind your house.

On the subject of long test leads, I have noticed that my meter is susceptible to interference from my computer when I tried to use the meter right next to the computer. This is not a great surprise since the computer has two intentional transmitters operating (Bluetooth and WI-Fi), and of course some lesser incidental noise sources. Even if the meter were to be shielded, the test terminals, and any test leads connected to them can pick up noise.

Circuit and Assembly Overview



The circuit is composed of a comparitor-based oscillator (U1), a microcontroller that measures the frequency and calculates the inductance or capacitance from the frequency (U3), and a microcontroller that takes the output from the first controller to drive a Liquid Crystal Display (U4).


Physically, the meter breaks down into these subassemblies:

The 2  line X 16 character liquid crystal display,
the Oscillator board,
the microcontroller board, and
the front panel wiring harness that ties it all together.


Everything but the battery is mounted on the top of the case. The oscillator is permanently mounted onto the front panel by virtue of its being soldered onto the banana plugs.  Copper was cut away to form an isolated pad for the ungrounded banana plug. The wiring harness solders onto the oscillator board, the buttons, the L/C switch, and the battery clip. The microcontroller board at the top, plugs directly into the LCD, which is covered by the board in this picture.



The 2  line X 16 character liquid crystal display

The calculated inductance and inductance is displayed on a Truly MTC-C162DPLY-2N-LCD, an inexpensive two line LCD display. Alternatively, an Hitachi HD44780 controller-based display can be used, as described on my web page, A serial interface for the Truly MTC-C162DPLY-2N 2 line X 16
char LCD display.  The output of U3 pin 3 is serial ASCII. U4 serves to simulate a dumb terminal and display the data on the 2 line X 16 character Liquid Crystal
Display

A 10k pot on the microcontroller board adjusts the drive on the LCD to give the best contrast for your chosen vertical viewing angle.

The Oscillator Circuit

Being the only analog circuit in the project, the oscillator circuit was fun to finalize and fun to build.            


The circuit was is the same comparitor-based oscillator that is the mainstay of many if not most of the microcontroller based LC meter projects on the web. The oscillation frequency is the resonant frequency of the parallel LC combination.  No relays are used to switch between inductance and capacitance measurement modes. A double pole double throw slide switch is used to select between measuring inductors and capacitors. When measuring inductors, S1B connects C1, a 0.0100 uf reference capacitor to the oscillator's tank circuit.  When measuring capacitance, S1B connects a L2, 1.00 millihenry inductor into the circuit. S1A grounds an input to mircocontroller U3 to let is know when the switch is in the capacitance mode, and U3 formats the output display accordingly.

The intent was to cover my range of interest,  nanohenries through hundreds of millihenries, and picofarads through nearly a microfarad, using only the two reference components noted above, and no band switching. For this, the oscillator would have to operate over a wide range, and that meant using a comparitor that is faster than the LM393 from the previous project. I found several LM319 comparitors, which are plenty fast. The '2313 can only measure frequencies up to 2 MHz when using a 4 MHz clock, and the oscillator's range is from several kHz to over 1.3 MHz in my particular instrument.

What I like about this arrangement, is that high value inductors are tested at a frequency at which I am most interested in their performance - that of many KHz or tens of KHz, while lower inductance inductors are tested at RF or near-RF frequencies.

The 5 volt power supply is filtered by a L//C filter to minimize ripple voltage from the microcontroller and display module. Such ripple could cause the oscillator to "cog" on the ripple frequencies, harmonics or the ripple frequencies, or subharmonics of the ripple frequencies, thus possibly preventing some inductance and capacitance values from being measured properly.  Similarly, it was to reduce the susceptibility of the oscillator board to radiation and ground loops from the microcontrollers and the LCD display module, as well as to simplify construction, that the oscillator board was built on a separate assembly that was located at the opposite end of the case from the microcontroller board and LCD display module.

I wanted the oscillator to be able to oscillate with any inductor or capacitor attached so that small inductors and capacitors would register on the display. That meant making sure that there is  a minimum shunt capacitance and a minimum series inductance capacitance. There is plenty of stray capacitance in the circuit. The basic oscillator has nearly 10 pf of stray capacitance. When it was mounted in the case and connected to the banana plugs, the stray capacitance came up to 14 picofarads. The stray inductance was fairly low, so a 2 uH inductor was added in series with the ungrounded  Lx/Cx terminal. In the case of this particular oscillator, 1 uH would have been enough, but my conform ally coated 1 uH inductors kept going open circuit whenever I tried to solder them with short leads, so I grabbed a suitable ferrite bead and put two turns on it, knowing that this would make a fairly rugged inductor.

L1 can be any value high enough to assure that the oscillator oscillates when in the inductance measuring mode and the Lx/Cx terminals shorted, but it should not be much larger than necessary so as to not cause performance problems.





The circuit was built "ugly bug" style on a piece of copper clad board, with an isolated pad for the ungrounded Lx/Cx terminal.  The pad was isolated by cutting away some copper with a hobby knife. The board was mounted by soldering it to the banana plugs that make up the Lx/Cx terminals on the front panel. All of the components are supported by their leads, which are soldered point-to-point to the other components. U1, the LM319, is in a socket because its noninverting input is exposed to ESD hazards though the ungrounded Lx/Cx pin. The socket is mounted by bending the grounded leads at right angles to form feet, which are soldered to the groundplane, while the other leads are bent upward to prevent them from touching the groundplane.

In the photograph above, you can clearly see the 2 uH inductor in series with the ground ungrounded Lx/Cx terminal. The red wire from the ungrounded Lx/Cx terminal connects the terminal to the SHORT button.

The reference capacitor (green) and reference inductor (blue with brown and black dots) can be seen on the far side of the board, with one end of each soldered to C2 and the other end soldered to S1, the view of which is obscured by the circuit board.

 


In order for the SHORT button to work correctly as a shunt for zeroing the series inductance, the inductance seen at the oscillator's input when the button is pressed needs to be the same as the inductance when a short is placed across the terminals. This was accomplished by trimming the length of the leads from the
SHORT button until I get the same (unzeroed) inductance reading from the SHORT button being pressed as I get when measuring a piece of wire connecting the banana plugs together. The loop in the bare metal wire from the ground lead of the SHORT button can be seen clearly in the photograph above.

The microcontroller board



There isn't much in the way of circuitry on the mircocontroller board. Most of the wiring on the back ties the LCD display connector (top) and the wiring harness connector (lower right) to the microcontrollers. There are two microcontrollers. The lower one in the photo above is U3 and the one above it is U4. U3 measures the frequency of the oscillator circuit, calculates the frequency and inductance or capacitance, and sends it as serial data our of pin 3. 

The 7805 voltage regulator reduces and regulates the output of a 9 volt transistor radio battery. From the approximately 45 milliamps drawn by the breadboard, I expect to get about 10 hours of continuous readings from a good quality alkaline battery. That's about 360,000 measurements.

U4 is the interface between the 2 line x 16 character LCD, and it appears as a dumb terminal to the serial data.

In the case of using AT90S2313 microcontrollers, the 4 MHz crystal is used to control the frequency of the on-chip clock oscillator on U3, and the output of U3's oscillator is used to drive the clock input on U4.

When trying ATTINY2313 as U3, I found that the voltage swing on AT90S2313 U4 was not sufficient to clock U4.  Then, turning to the ATTINY2313 datasheet, I found that one must set the ATTINY2313's internal fuses to accept an external clock. The only potential problem in doing this is that after programming that fuse setting, the controller cannot be programmed again, unless it is supplied with an external clock. I chose not to bother with trying this because of the extra work to supply and external clock while reprogramming the chip. This can be a real pain if you are not set up for it, particularly if you have to program the chip a number of times. If you understand the ATTINY2313 well enough to set up U4 to be clocked by U3, you don't need my instructions to do so. If you need my instructions to set up U4 to be clocked by U3, then you are unlikely to be able to recover from a mistake in programming U4. In your case, and many others, separate oscillators for the two ATTINY2313's is a good solution.

 I do not suggest tying to get by calibrating U4's on-chip RC oscillator to run at 4 MHz, I did run the LCD controller with the internal 4 MHz RC clock just so I could  prove to myself that ATTINY2313's work properly in this circuit, but sometimes, as expected, the UART in the LCD controller had difficulty synching to the incoming stream from U3.  It is much better to use the extra 4 MHz crystal, as suggested by the schematic diagram.





As an alternative to using an on-board LCD, you could drive an external serial LCD or a dumb terminal, or a computer running a dumb terminal program. The output of U3 driving and inverting serial line buffer is shown above.  Most likely your serial device is RS-232 or RS-422, but often a simple inverter that drives the signal between +5V and Ground, as shown in the picture above,  is sufficient.

Firmware

The frequency measurement and inductance and capacitance calculation firmware for U3 was written and compiled with WINAVR (avr-gcc 3.4.6), which can be downloaded free of charge from http://sourceforge.net/

The dumb terminal emulation firmware for U4 had been written in Atmels AVR Studio for an earlier project. I looked at rewriting the LCD code into assembler. With my C programming capabilities, it would have taken several days or even weeks to get all of the code ported and working. and I decided not to make a project of it, so the dumb terminal firmware is part of this project - just a borrowed module from an earlier one.

Much of the firmware had to do with measuring the frequency of the oscillator circuit. In this system, the number of pules from the oscillator circuit are counted by TIMER1 for a period of 1.114 seconds. Since frequencies exceed 1.3 Mhz, the 16 bits in counter 1 was not enough to handle the highest frequencies. To handle this, TIMER1 interrupt was set up so that a global variable was incremented each time TIMER1 overflowed. At the end of the 1.114 second measurement period, the contents of the overflow variable is multiplied by 2^16 (= 65,5536) and then added to the 16 bit contents of TIMER1. The result is then divided by 1.114 seconds to yield the number of oscillator cycles counted per second.

From there it is a fairly simple matter to calculate the inductance or capacitance based on the value of the reference capacitor or reference inductor. The same computation is used for both inductance and capacitance. A little more code takes care of switching the display between L and C, and the ZERO SET function when the ZERO button is pressed.

The flash memory space used for program memory filled up quickly. I wanted to have auto zero for capacitance measurements since this could be done with "no hands" as it is not necessary to short anything in order to zero the capacitance measurement, but I could not get it to work just right because there was not enough memory left to make it work just right. As a result the meter only zeros the reading when the ZERO button is pressed.

Putting the decimal point just to the left of the right-most digit in in the inductance display resulted in some very assembly language-looking code. It was a struggle to fit this into the memory. I can see the value in saved code space by coding in assembly language. A few double precision math routines, and poof! There goes the program memory.

I thank my C Guru and mentor, Marshall Schaffer, for preparing me for this task and for his assistance in removing some persistent compiler generated warnings.

Note: The assembly language file for the LCD driver chip has a bug in it that prevents is from being assembled on later versions of AVRStudio. This bug was found by Allun Bennett was has been corrected in the downloadable file named LDCbutons040904C_updated.asm. Please see the list of downloads at the top of this page.

The wiring harness and completed assembly




A wiring harness was built according to the schematic show elsewhere on this page. there is a connector to allow the mircocontroller board to be unplugged from the harness. Similarly, the mircocontroller board plugs into the back to the LCD, thus it is possible to completely remove the mircocontroller board from the instrument. This came in very handy during the the first several days after initial assembly.

Measurement of Inductors
To measure an inductor:
1. Turn on the instrument and wait for the readings to settle.
2. Short the Lx/Cx terminals together by either directly shorting the terminals or by pressing and holding the SHORT button.
3. Press and hold the ZERO button for two or more measurement cycles. The LED blinks one time each measurement cycle.
4. Release the ZERO button and read the inductance value and the measured frequency from the LCD.
5. Turn off the instrument.

Note:    Inductors with exceedingly low Q  (or high resistance) will not be measured accurately.
             Inductors with cores may exhibit different values dependent upon the measurement frequency and power level.
             Exceedingly long test leads can affect measurement accuracy.


Measurement of Capacitors
To measure a  capacitor:
1. Turn on the instrument and wait for the readings to settle.
2. Leave the Lx/Cx terminals open (do not press the SHORT button).
3. Press and hold the ZERO button for two or more measurement cycles. The LED blinks one time each measurement cycle.
4. Release the ZERO button and read the capacitance value and the measured frequency from the LCD.
5. Turn off the instrument.

Note: Exceedingly long test leads will can affect measurement accuracy.


Find updates at www.projects.cappels.org




HOME (More Projects)
 
Contents ©2007 Richard Cappels All Rights Reserved. Find updates at www.projects.cappels.org

First posted in February, 2007. Updated November, 2007.

You can send  email to me at projects(at)cappels.org. Replace "(at)" with "@" before mailing.

Use of information presented on this page is for personal, nonprofit educational and noncommercial use only. This material (including object files) is copyrighted by Richard Cappels and may not be republished or used directly for commercial purposes. For commercial license,
click here.


  Liability Disclaimer and intellectual property notice
(Summary: No warranties, use these pages at your own risk. You may use the information provided here for personal and educational purposes but you may not republish or use this information for any commercial purpose without explicit permission.) I neither express nor imply any warranty for the quality, fitness for any particular purpose or  user, or freedom from patents or other restrictions on the rights of use of any software, firmware, hardware, design, service,information, or advice provided, mentioned,or made reference to in these pages. By utilizing or relying on software, firmware, hardware, design, service,information, or advice provided, mentioned, or made reference to in these pages, the user takes responsibility to assume all risk and associated with said activity and hold Richard Cappels harmless in the event of any loss or expense associated with said activity. The contents of this web site, unless otherwise noted, is copyrighted by Richard Cappels. Use of information presented on this site for personal, nonprofit educational and noncommercial use is encouraged, but unless explicitly stated with respect to particular material, the material itself may not be republished or used directly for commercial purposes. For the purposes of this notice, copying binary data resulting from program files, including assembly source code and object (hex) files into semiconductor memories for personal, nonprofit educational or other noncommercial use is not considered republishing. Entities desiring to use any material published in this pages for commercial purposes should contact the respective copyright holder(s).