Maker Pro
Maker Pro

How do I build a Metal Detector?

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Have you heard about comments? You use them throughout your program to describe what the program is doing. They are almost as important as the code itself because they enable other people to follow your logic, and they will also remind you of how the program works when you come back to it in six months.

So you have just the search coil connected between RC1 and RC2, with no capacitor across it to form a tuned circuit? Your code sets RC2 as an output and RC1 as an input? What does it do then? Does it generate a pulse on RC2 and measure how long it takes before RC1 changes?

I don't think you'll get any useful results doing that. Look at other metal detector designs. They ALL use the search coil as part of a tuned circuit in an oscillator. If you build an oscillator around the search coil, you could measure its frequency if you feed its output into a timer clock input. How you determine the frequency depends on what capabilities the timer(s) have. You might be able to use some kind of input capture. You might need to divide the oscillator frequency externally using something like a 74HC4040 and capture transitions from the divider. The PIC would need to be clocked from a crystal though, for accuracy and stability. If you really want to do the frequency detection with the PIC, that's what I would try next.

Try to understand how metal detectors work. Google metal detector schematic and click on the image results. For all the designs that aren't too basic, open the image in a new tab, then go through those tabs and go to the web page. You will find explanations for how the circuits work.
 

ami85t

Feb 19, 2014
71
Joined
Feb 19, 2014
Messages
71
First of all thanks for the advice, I'll try I and be more exact.
The PIC has an external crystal connected for it's routine function.
(it doesn't have an internal osc.)

The code includes generating a wave using the PWM module and
connecting is output to a inductor. the output of the inductor is connected
to a capture module of the PIC which uses a timer of the PIC, based on the ext. osc.
every loop of operation the timer is reset. The capture module counts
16 rising edges of the input wave which is connected to the inductor,
when that happens the timer's value is captured and then transmitted.
The theory idea is that metal close to the inductor changes the frequency
and therefor the value of the timer changes to less of more (time value)

About a LC tank- I have read about the different types of implementing the
detector and as you said there's always a LC tank involved somehow.
I have seen implementations which use the tank as a parallel LC
circuit and also as a serial one.
As far as I understand (and correct me if I'm wrong..) in these projects the capacitors sometimes are used for noise reducing purpose.
(e.g.:
http://www.micro-examples.com/public/microex-navig/doc/076-picodetector.html)

moreover, measuring the transmitted data at the PC gives the same result when
I connect a cap as suggested in the link. again - it seems that the circuit does it's job
but it's not clear enough and the number which represents the timer value doesn't change
sufficiently enough for good measurement and here I'm back to what you wrote for using a tuned circuit.
Maybe should i add a capacitor in parallel to the inductor in a different way which can improve the circuit's operation? or maybe try different types of inductor? I'm looking for something small and not a big circled wire object.

Thanks,
Amitai
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
OK. First, an inductor doesn't have an "input" and an "output". You need to use it as part of an L-C "tank" circuit in an oscillator. The frequency of the oscillator is determined by the capacitance and the inductance. The inductance will change very slightly when the inductor is brought near to metal, and this will change the oscillator frequency. If the metal contains iron, the inductance will increase slightly, and the frequency will drop slightly. You need to detect small variations in the oscillator frequency. This is how metal detectors work. You should have been able to find this out by reading articles about them.

Connecting an inductor between two pins of a PIC and sending a pulse through it will not produce a measurable timing change when the inductor is brought near to metal. Adding a capacitor across the inductor won't help either. You need to use the inductor-capacitor (LC) "tank" circuit as part of an oscillator, and detect small variations in oscillator frequency.

To use a microcontroller to measure frequency very accurately, so that slight variations can be detected, you need to measure the period of the oscillator with high resolution. One way to do this is to have a timer that is clocked at a very high rate, and a signal from the oscillator that causes an input capture to occur.

Some trick is needed to improve the resolution. Here's why. Let's assume that the oscillator runs at 100 kHz and the timer is clocked at 5 MHz. Without a frequency divider, one oscillator cycle corresponds to a count of 50 (5 MHz divided by 100 kHz). If your code measures the time between two adjacent input captures, using a free-running counter, it will see a value of 50 for each cycle.

To detect a change, you would need the value to change to 49 or 51. These counts correspond to frequencies of:
49: 5 MHz / 49 = 102040.8 kHz = 100 kHz + 2.04%
51: 5 MHz / 51 = 98039.2 kHz. = 100 kHz - 1.96%

In other words, the oscillator frequency needs to change by about 2% before you will see a change of 1 in the number of timer clocks between two consecutive input captures. But the change in inductance, in response to metal, is very much smaller than 2%.

You can detect small changes by adding together multiple input capture intervals. If you measure 1000 consecutive input capture intervals and add them together, you will measure the frequency with 1000 times higher resolution and you will be able to detect changes as small as 0.002%.

So to summarise. You need to build an oscillator whose frequency is determined by the inductance of the search coil and an accurate, stable capacitor connected together as an L-C tank circuit. You need to clock the PIC from a high, accurate frequency and feed this frequency into a timer with input capture, which is driven from the oscillator. You need to measure and accumulate multiple input capture periods to detect the slight variations in the oscillator frequency due to the proximity of metal to the search coil.
 

ami85t

Feb 19, 2014
71
Joined
Feb 19, 2014
Messages
71
Thanks again for the patience, and explanations.
The issues you have brought are the things I'm focusing on right now.

So to summarise. You need to build an oscillator whose frequency is determined by the inductance of the search coil and an accurate, stable capacitor connected together as an L-C tank circuit. You need to clock the PIC from a high, accurate frequency and feed this frequency into a timer with input capture, which is driven from the oscillator. You need to measure and accumulate multiple input capture periods to detect the slight variations in the oscillator frequency due to the proximity of metal to the search coil.

Isn't that what I'm doing besides the LC part?
oscillating a square wave using the pic, feeding through an inductor and then
feeding to a timer capturing using the pic's timer and capture modules?

Amitai
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Isn't that what I'm doing besides the LC part?
oscillating a square wave using the pic, feeding through an inductor and then
feeding to a timer capturing using the pic's timer and capture modules?
No. This is what you are trying to do at the moment:
Connecting an inductor between two pins of the PIC
Generating pulses on one pin
Measuring the amount of time taken for the pulse to travel through the inductor using a timer monitoring the second pin.

This is what you need to do:
Make an oscillator that produces a continuous frequency that is controlled by the inductance of the inductor, in combination with a very stable capacitor
Measure the time between cycles of the oscillator very accurately
Accumulate a large number of timing measurements so you can detect very small changes in frequency.

Or as I said in paragraph 2 of post #23:
Connecting an inductor between two pins of a PIC and sending a pulse through it will not produce a measurable timing change when the inductor is brought near to metal. Adding a capacitor across the inductor won't help either. You need to use the inductor-capacitor (LC) "tank" circuit as part of an oscillator, and detect small variations in oscillator frequency.
 

ami85t

Feb 19, 2014
71
Joined
Feb 19, 2014
Messages
71
Isn't generating cycled pulses with 50% duty cycle an oscillator?
You mean a pure AC analog signal and not a square wave?

Amitai
 
Last edited:

jpanhalt

Nov 12, 2013
426
Joined
Nov 12, 2013
Messages
426
Microchip has an inductance measuring method that highlights its Charge Time Measurement Unit (CTMU) peripheral. Is that what you are trying to do?

http://ww1.microchip.com/downloads/en/AppNotes/CTMU 01375a.pdf

I only found that module in 16-bit chips, but I didn't check very thoroughly for it.

While looking for that app note, I found this senior project thesis related using a microcontroller for measuring inductance:

http://digitalcommons.calpoly.edu/cgi/viewcontent.cgi?article=1142&context=eesp

John
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Isn't generating cycled pulses with 50% duty cycle an oscillator?
No. An oscillator produces a continuous alternating signal. It doesn't use pulses generated by a microcontroller. An LC oscillator oscillates at the resonant frequency of the inductance and the capacitance.
You mean a pure AC analog signal and not a square wave?
Well, the waveform across the tank circuit will be close to a sinewave, but you're only interested in timing the cycles, so it's not necessary to feed it into an ADC and analyse the waveform - not that this would be possible anyway with a PIC because of the speed of the ADC and the amount of processing power available.

There will generally be a point in the oscillator circuit where the waveform is closer to a squarewave - for example, at the collector of the transistor, if it's a transistor-based oscillator - and that is the signal you would feed into the microcontroller. As long as it has reasonably clean rising and falling edges, and comfortably exceeds the maximum and minimum input voltage thresholds of the PIC, that will be fine.

If the oscillator doesn't have a suitable signal takeoff point, you would need to take a signal from the best available point in the oscillator and feed it through a simple transistor amplification stage, or maybe a tracking comparator with a small amount of hysteresis, to convert it into a clean signal with a wide voltage swing.

Microchip has an inductance measuring method that highlights its Charge Time Measurement Unit (CTMU) peripheral. Is that what you are trying to do?
http://ww1.microchip.com/downloads/en/AppNotes/CTMU 01375a.pdf

While looking for that app note, I found this senior project thesis related using a microcontroller for measuring inductance: http://digitalcommons.calpoly.edu/cgi/viewcontent.cgi?article=1142&context=eesp
Thanks John. But unfortunately neither of these methods will be anything like accurate enough for this application, where the frequency needs to be resolved to better than 100 ppm. Absolute accuracy is not important, but the change in frequency caused by a metal object in the vicinity of the coil is quite small.

The microcontroller-based inductance meter uses a ringing method, not continuous oscillation, so it will not be very accurate (the text claims 10%). The method used to convert the decaying oscillation into a squarewave for the microcontroller to process could have been useful, but the document doesn't contain a full schematic!
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Welcome to our forum.

Others have written tons of stuff on metal detectors. Here's a collection of different detector circuits.
All these detetcors have in common is that they either give a yes/no indication for metal detected or an audible signal (beat frequency) which changes with distance between the metal and the detector (and with the type of metal).

I'm not aware of a detector that gives an anlog signal where the amplitude changes with distance. You have at least these options:
1) convert the beat frequency into an analog signal (you could use a rectifier and a filter for a crude conversion)
2) feed the beat frequency (with suitable voltage level) to an input pin of the microcontroller. Have the microcontroller count the freqeuncy directly.

Regards,
Harald


Guys, while I've enjoyed reading through this thread with all its cranial expertise I can't help but muse that this is a classic "Forest - Trees" analogy. I've never used a metal detector and I've never researched how they work. Yes, I've always assumed that they functioned via some form of oscillator/Inductor "pulling".

After reading Harald's post I can't help but conclude that any method employed must provide either user or automatic near zeroing control. This is because real world environmental variables seem to have been lost or ignored in this thread. The level of component quality and tolerance mentioned (in the thread or links) will mean little outside a laboratory environment.

The measurement of Δf or Δt (as used in a metal detector) would be a short term measurement. This makes component value drift much easier to deal with because the user can compensate for it. Whether it's 10° or 110°, at the beach or on top of a mountain the instrument must be capable of dealing with it. Thus far I haven't read anything in any of the uC concepts that do.

Chris
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
After reading Harald's post I can't help but conclude that any method employed must provide either user or automatic near zeroing control. This is because real world environmental variables seem to have been lost or ignored in this thread. The level of component quality and tolerance mentioned (in the thread or links) will mean little outside a laboratory environment.
The measurement of Δf or Δt (as used in a metal detector) would be a short term measurement. This makes component value drift much easier to deal with because the user can compensate for it. Whether it's 10° or 110°, at the beach or on top of a mountain the instrument must be capable of dealing with it. Thus far I haven't read anything in any of the uC concepts that do.
Yes, the changes in frequency (or period) are small, short-term frequency variations. The microcontroller needs to establish the baseline frequency, which is presumably the frequency set by the inductor with no metal nearby, then detect and report increases and decreases in the oscillator frequency relative to that baseline frequency. It will also need to compensate for medium-term and long-term drift due to temperature variations and perhaps other factors.

I think I would do this by looking at the rate of change of the frequency. If the frequency is changing only gradually, I would assume this is oscillator drift; if it changes relatively quickly, I would assume this is due to the search coil moving towards or away from some nearby metal object.

You're right that the components need to be stable. I mentioned that the capacitor needs to be stable - a good quality part with a low temperature coefficient. Also, the search coil needs to be rigid, and properly supported. I think normally they are tightly wound with tape then clamped inside the search head at multiple points.
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Thanks John. But unfortunately neither of these methods will be anything like accurate enough for this application, where the frequency needs to be resolved to better than 100 ppm. Absolute accuracy is not important, but the change in frequency caused by a metal object in the vicinity of the coil is quite small.

The microcontroller-based inductance meter uses a ringing method, not continuous oscillation, so it will not be very accurate (the text claims 10%). The method used to convert the decaying oscillation into a squarewave for the microcontroller to process could have been useful, but the document doesn't contain a full schematic!

Kris, just so you know that this post did not exist when I posted last. While it doesn't directly address my concerns it does nibble on the fringe. ;)

Chris
 
Last edited:

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Yes Chris, we "crossed paths" :) You raised an important point, and reminded me that the search coil needs to be stable as well as the capacitor.
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Yes Chris, we "crossed paths" :) You raised an important point, and reminded me that the search coil needs to be stable as well as the capacitor.

Kris, I think user or automatic null control to compensate for the real world is a must. If I had my choice it would be user controlled.

Chris
 

ami85t

Feb 19, 2014
71
Joined
Feb 19, 2014
Messages
71
Thanks again all for the good advice and explanations!

Amitai
 

ami85t

Feb 19, 2014
71
Joined
Feb 19, 2014
Messages
71
Hi,

I tried implementing the sensor using all the advice I got here.
I built up this circuit as the LC oscillator:
http://sicimatic.blogspot.co.il/2013/11/simple-metal-detector-using-555-timer.html

,then used a frequency capture module in a uC and transmitted the results to a PC.

The data received is a sequence of numbers which clearly increases in presence of
metal. The range of the numbers in any case is wide and for the detecting being
more accurate I want to minimize this range.
In the thread there are a few references to this issue and I tried changing my
code according to them, till now there's now real change.

Does anyone have any idea what can help to reduce the range?
maybe trying to reach a specific frequency? use specific components?


Measure the time between cycles of the oscillator very accurately
Accumulate a large number of timing measurements so you can detect very small changes in frequency.
summing up a big amount of timing measurements eventually results in a maximum
value of FF (hex). Maybe I can sum up and then divide the result (= rotate right
the binary value).

the search coil needs to be stable as well as the capacitor.
What's the meaning of a stable component? physical size specification, induction/capacitance value?
Thanks, Amitai
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Hi,
What's the meaning of a stable component? physical size specification, induction/capacitance value?
Thanks, Amitai

That would be components that maintain their value (to a high degree) with change in temperature, humidity, altitude and sometimes physical shock. Mil Spec components are noted for these qualities.

Chris
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
In terms of things like your search coil it also means it is mechanically solid with no flexing or flopping around of wires or other things that could affect the inductance or capacitance. Often this means potting the search coil.
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
The data received is a sequence of numbers which clearly increases in presence of metal. The range of the numbers in any case is wide and for the detecting being more accurate I want to minimize this range.
I'm not sure what you mean. Are you saying that the frequency reported by the microcontroller's frequency capture module is varying constantly over a wide range? If so, that could mean that the oscillator is not stable, or that the frequency capture module doesn't work properly or is not being properly set up.

It should use the "input capture" method, where a counter is clocked at a very accurate frequency, as high as possible (preferably the crystal frequency without any prescaling), and when a rising edge arrives on the input capture pin, the hardware captures the count at that time and holds it in an input capture register.

Firmware then detects that an input capture has occurred (either by polling, or with an interrupt), reads the captured timer value, calculates how much it has changed since the previous one, and adds the difference into an accumulating register.

After a certain number of captures (more gives better accuracy; less gives faster update rate), the value in the accumulating register contains an accurate representation of the frequency. You then apply the "follow slow changes; report fast changes" algorithm to compensate for oscillator drift but detect changes due to metal near the coil, and clear the accumulator ready for the next set of samples.
In the thread there are a few references to this issue and I tried changing my
code according to them, till now there's now real change. Does anyone have any idea what can help to reduce the range? maybe trying to reach a specific frequency? use specific components?
You need to explain the problem more clearly.
summing up a big amount of timing measurements eventually results in a maximum value of FF (hex). Maybe I can sum up and then divide the result (= rotate right the binary value).
No, if you do that, you'll lose resolution, which defeats the reason for accumulating them in the first place. You need to be able to detect small variations in the frequency.

You need to accumulate the values in a variable much bigger than 8 bits. A 16-bit variable might be big enough if the input capture timer is clocked at a slow rate, or if you sample a relatively small number of captures before you test the frequency and reset the accumulator, but both of those things will reduce the resolution of the frequency measurement. I would use a 24-bit accumulator.
 

ami85t

Feb 19, 2014
71
Joined
Feb 19, 2014
Messages
71
Hi again,
I have done a few experiments and this is my current system schematic:
Capture.PNG

What is supposed to be in the system is:
1.Oscillating a frequency based on the 555 timer circuit which includes the RLC part and all of this together is included
in the sensor part. This circuit is fine by it self and it's frequency is fixed at 465HZ on steady state.When a metal object is close to the inductor the frequency gets to 411HZ and is fixed too.
2.a PIC16f877 capture module is sampling the frequency and then transmitting the result to the Bluetooth module,
whom passes over the data to a PC terminal.

I managed twice during the experiments to implement the whole system with good result, i.e. the values received in the terminal (which are proportional to the frequency's value) clearly change
when a metal object is close to the inductor.

for some reason this situation isn't stable enough and I'm not sure why. I have tried doing several things(Chronological):
- For the sake of checking if there's any disturbances between any of the systems components I connected them gradually to each other. first the 555 oscillator, which as I mentioned is fine. When I tried to connect to the same power supply the BT and PIC the frequency became unstable.
- Therefor I tried to feed the power supply's signle through a L7805 regulator. As result the circuit didn't get enough
energy so for all the components to operate (led-on indicators weren't on). So I increased the voltage input of the regulator from 5V to 7V which led the Vout of the regulator to be ~5.4V.
That's when the system results where good ones.
after fifteen minutes i noticed that the frequency is again unstable and the regulator is very hot.

- Testing the input currents of the 555, uC and the BT results in maximum a few tens of mA.

- I then added another power source as seen in the schematic - The oscillator circuit has a 5V power source and the BT+uC has a 5V power source. I do not know if this is a good solution but as something temporary such separation doesn't cause the collision between the two parts of the system and the frequency is not interrupted.
- Trying again to sample the timer's frequency when some metal object is close to the inductor didn't give any result in the terminal software (The 555 oscillator is fine and is accurate while testing with a DMM), although Seemingly this is the same circuit which was in good shape for a while. The only change I can think of from the original is that the input voltage (single supply) had a bigger value( 7V ) but this voltage level is also what caused the regulator to overheat (maybe?).

In the schematic can be seen a bi-colored LED that flickers every operation cycle, so that if the uC isn't really sampling a frequency the continues loop operation stops. Therefor the LED is an indication of sampling progress. In retrospect, it seems the output of the sampling leg of the uC is very sensitive to changes so that even when connected to a constant voltage node it is also blinks sometimes. For examination purpose I sampled the oscillator's crystal oscillator and this result in a very rapid flashing of led, as compered to a situation of connecting to a constant voltage node. So it seems that there's a real frequency sampling process which might be influenced in a high rate from unwanted noise signals.

I would be glad for help trying to achieve a good steady state situation for the system,
Thanks
Amitai
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Where did you get that 555 circuit? I'm surprised that it works at all. Please provide a reference to the page where it's described.

The frequencies you quoted, 465 Hz and 411 Hz, they're kHz, right?

Using a 7805 regulator is a good idea. I don't know why it's getting hot. What is the peak current consumption of the bluetooth module? What is the total current flowing into the regulator, and how many volts is it dropping? Multiply those together to get the power dissipated in the regulator, and if it's more than about 0.5 watts, it will need a heatsink.

If those indicators connected to pins 34 and 35 of the PIC are LEDs, they need current limiting resistors. If they're light bulbs, they need to be very low current light bulbs!

You need decoupling capacitors on the PIC.

555-based oscillators are very noisy. They inject noise back onto the power supply rails. You can try isolating the noise using a PI filter, or perhaps a separate regulator powered from the same input voltage source. I would use an LC oscillator based on discrete components like transistors, instead of the 555.
 
Top