# Detect if coil is near steel piece using MCU

How to detect inductance change using MCU (lowest power methods preferred). I want to detect if the coil touches steel piece (higher inductance) and when it does not (lower inductance). Basically I need to compare inductances at two positions and compare the values. Then I will derive a threshold value to detect when the coil is away from the steel piece.

I used a PIC12F509 to make this kind of sensor, years ago. It used 100uH leaded-resistor type inductor L=6mm D=2.5mm. The actual capacitor values I can't recall. It was used to make a waterproof switch that reliably worked in salt water. In my case a spring (ID=8mm L=4mm) slid over the coil forming a shorted turn. simulate this circuit – Schematic created using CircuitLab

C1,L1 form a resonant circuit at 150kHz (for values shown). One half cycle is approx 3us The output is enabled, then a single pulse energises the circuit (hi for 2us), then you switch back to input and sample the input. The LC tank will now resonate. i.e it will going - + - + at the resonant frequency for a couple of cycles. Immediately after energising it goes negative, then a half cycle later the voltage will peak +ve, and you will sample as logic 1 . When there is metal forming a shorted turn the peak happens earlier, because L is effectively reduced, and you will sample 0. Code is very simple - probably less than 10 opcodes.

You need to choose L and C to suit how fast your micro works (PIC was 1us instruction cycles). You need to experiment with coil types to suit your situation. You need to empirically test the best excitation pulse (how wide, single +ve pulse vs 1/0 pulse) and explore what the resonance looks like and how it moves, using an oscilloscope. Note that there are two detection modes possible. I used change in L and therefore change in position of resonant pulse.

It is also possible to look at the decay-time of the ringing, by counting the number of ring cycles. In this case the presence of metal will increase the loss, and the ringing dies faster. e.g ring count drops from 3 to 1.

• +1, I like the simplicity of both the hardware and the software. – Janka Mar 13 '18 at 23:01
• power consumption is also low as it energises 2us only @15mA, 10Hz this is 300nA average. – Henry Crun Mar 13 '18 at 23:42
• I really like this answer, but I'm curious about negative voltages at MCU pin. I know capacitor in series blocks DC, but I'm not sure how capacitor C2 protects the pin. – Piotr Szturmaj Mar 14 '18 at 0:41
• The pin is protected by its input protection diode. Since the pin itself is energising the LC tank, the current that the protection diode can see is limited, and cannot be high enough to damage anything. After the first negative excursion, there will be a bias establish across C2, subsequent rings don't go negative. (I think) – Henry Crun Mar 14 '18 at 0:58

Create an LC (inductor-capacitor) oscillator. The L is the inductance of your sensor, which varies. C is a known capacitance. The frequency of the oscillator will depend on the inductance.

There are several methods for determining frequency by a microcontroller. Some MCUs have counters, so you can count the number of LC circuit oscillations over a known period of time. Some MCUs have peripherals which can measure time periods.

There are also frequency to voltage converter circuits. Output voltage is DC proportional to frequency. The resulting voltage can be read by an ADC on a microcontroller.

edit: Apparently, there are specialized ICs which are inductance to digital converters (also here).

Research EFM32 MCUs with LESENSE peripheral - very low power inductive sensing. Silicon Labs has an application note covering your exact topic.

There are two obvious approaches.

Make the coil part of a LC oscillator and look for the frequency changing. This has already been covered in another answer so I won't expand on it here.

The second way is to create a fixed frequency oscillator. Apply it across a series LR circuit or LCR and look for a change in the current by measuring the voltage across the resistor.

For inductance change detection the conventional scheme is to design the coil into an oscillator circuit. Changes of inductance are then determined indirectly by detecting the change of frequency of the oscillator.

An MCU timer can be used to check the frequency by one of two methods.

1) Measure the time period of cycles of the oscillator signal. Take several readings and average.

2) Count cycles of the signal from the oscillator over a given time interval.

To make the solution as low power as possible design the oscillator so that it can be gated off via a separate GPIO from the MCU. Then only start the oscillator and make your readings on a periodic basis that is compatible with the maximum latency that you can live with in your application.

• I would go so far and only trigger the LC circuit with a single edge, then Hi-Z the output and measure the oscillation with the µC's input on the same pin. – Janka Mar 13 '18 at 20:48
• @janka Indeed this works well - see my long winded answer – Henry Crun Mar 13 '18 at 21:56