Metal detector on AVR microcontroller
Metal detector on AVR microcontroller
Since ancient times, people have been attracted by devices for searching for hidden metal objects. The reasons for this interest are varied. Builders are interested in the location of metal reinforcement in the walls, treasure hunters dream of finding a jug of gold coins in the ruins of an old building, sappers search for unexploded “gifts” from past wars. All these people are united by the desire to have an inexpensive, compact and economical device that will help them detect metal objects through a layer of earth or concrete and, if possible, determine what metal they are made of. If we exclude exotic methods, such as dowsing and psychics, then the vast majority of such devices are based on electronic devices that respond to changes in the electromagnetic field excited by the search device by metal objects. Most often, a frame coil consisting of several hundred turns of copper wire and included in the circuit of an autogenerator is used as an excitation coil and simultaneously a sensor of the device. Such devices use the effect that when a metal object approaches the coil, its inductance changes and, as a consequence, the frequency of the autogenerator. In this case, in general, ferromagnetic objects (iron, cast iron) lower the frequency, and non-ferromagnetic (copper, gold, aluminum) increase the generation frequency. By registering the magnitude and sign of the frequency deviation, one can draw a conclusion about the type of metal object that has entered the search zone of the frame. The main differences between most types of such metal detectors are in the methods of registering the frequency change. Below is a brief description of the most frequently used methods. Metal detector on AVR microcontroller.
Frequency detector
One of the simplest ( Metal detector on AVR microcontroller) is a device that operates on the principle of “resonance breakdown” (OR – Off Resonance). The principle of operation of this device is based on the use of a frequency detector based on an oscillatory circuit. See Fig. 1.
Figure 1. Structural diagram of the OR metal detector
The oscillatory circuit of the frequency detector has a resonant frequency close to the frequency of the search generator. Changing the frequency of the generator leads to a change in the amplitude of the signal on the circuit, which is recorded using an indicator, such as a pointer device. Such devices have not found wide application. Their disadvantages are the need to ensure a stable signal amplitude at the generator output, as well as the need to adjust the resonant frequency of the circuit due to the influence of destabilizing factors on both the search generator circuit and the frequency detector circuit.
Beating method
Another device is a metal detector based on beats (BFO – Beat Frequency Oscillation). The operating principle of such a metal detector is based on the beats of the frequency of the reference generator and the frequency of the search generator. See Fig. 2.
Figure 2. Structural diagram of the BFO metal detector
The measuring and reference generators are tuned to the same frequency. When the frequency of the measuring generator changes, a difference frequency signal appears at the mixer output. The operator perceives this signal by ear or visually, depending on the design. Such devices have been manufactured for several decades. Nowadays, inexpensive toy metal detectors and amateur metal detectors are mainly built according to this principle. Such devices have a number of disadvantages. The first is the presence of parasitic mutual synchronization of both generators. This leads to the fact that it is impossible to estimate a very small frequency difference and, as a result, the sensitivity of the device is significantly reduced. The second disadvantage is the lack of selection by metal types. Ferromagnetic objects cause a decrease in frequency, and metallic non-ferromagnetic objects cause an increase in the frequency of the measuring generator. However, after the mixer in the BFO metal detector, information about the frequency drift sign is lost.
PLL based frequency detector
The next device (PLL – Phase Locked Loop) is a device in which the disadvantage of a metal detector on beats is used for the benefit. In such a device, both generators, measuring and reference, work strictly at the same frequency. Moreover, the frequency of the measuring generator is adjusted to the frequency of the reference generator using the PLL system. See Fig. 3.
Figure 3. Structural diagram of the PLL metal detector
The voltage signal of the adjustment is used to determine the magnitude and sign of the frequency change. Such metal detectors have selection by types of metals. There are several amateur radio designs of this type. The following can be attributed to the disadvantages of such devices – the presence of a “useful” PLL does not exclude the presence of parasitic mutual synchronization of both generators, as in a device on beats. This leads to the fact that the steepness of the adjustment characteristic decreases, and as a result, the detection range decreases.
Digital frequency meter
The idea of using a digital frequency meter to register the frequency drift of the measuring generator is not new. Such a metal detector (FM – Frequency Meter, see Fig. 4.) is free from most of the shortcomings inherent in previous circuits. Its operating principle is as follows:
First, the electronic frequency meter evaluates the frequency of the measuring generator when the sensor is far from the search objects. This value is entered into the storage register. Then, during the search, the frequency meter continuously measures the current frequency of the measuring generator. The value of the reference frequency is subtracted from the obtained values, and the result is sent to the indicator device. It is obvious that in such a design, the effect of parasitic mutual synchronization of generators will be expressed much weaker – after all, now the frequency of the measuring generator (units-tens of kilohertz) is several orders of magnitude lower than the frequency of the reference generator (tens of megahertz). Using a frequency meter, it is possible to measure not only the magnitude of the frequency drift of the measuring generator, but also its sign, therefore, such a metal detector has selectivity by types of metals.
Figure 4. Structural diagram of the FM metal detector
However, the implementation of this idea “head-on” does not allow obtaining real sensitivity greater than in a device on beats. This is due to the fact that it is impossible to directly register very small frequency deviations (units and fractions of hertz) in real time (20…40 ms per count). We managed to solve this problem in the following way – from the theory of radio measurements, a method of “fast” measurement of low frequencies is known – the so-called method of reverse counting. In this method, the period of the signal is measured, and the frequency is calculated as its reciprocal value. Only the task of practical implementation remained.
Practical design of a metal detector
It is obvious that if such a device is implemented on elements of medium integration, it will be a relatively complex and bulky device, which is undesirable for mobile implementation. The way out of this situation was the use of a microcontroller. It turned out that it was possible to assign not only the task of measuring the period to the microcontroller, but also almost all functions for processing the results – calculating the frequency difference, sound and light indication of the measurement results. Our metal detector is implemented on the AT90S2313-10PI microcontroller manufactured by Atmel .
This is an 8-bit economical RISC microcontroller. It has a performance of 10 MIPS at a frequency of 10 MHz. Contains: 2 KB of flash memory, 128 bytes of EEPROM, 15 input/output lines, 32 working registers, two timers/counters, a watchdog timer, an analog comparator, a universal serial port. More details about the AVR family of microcontrollers can be found on the manufacturer’s website.
Main technical characteristics of the metal detector
- Supply voltage: 5.5-20 V
- Current consumption: 15 mA
- Indication: light – 7 LEDs and sound
- Search modes: static and dynamic
- Discrimination: ferromagnets / non-ferromagnets
- Detection depth (in air):
– Coin with a diameter of 25 mm: 11 cm
– “Pistol”: 17 cm
– “Helmet”: 37 cm
Schematic diagram
The basic diagram of a metal detector based on the frequency meter principle is shown in Fig. 5.
Figure 5. Schematic diagram of the metal detector
The measuring generator is built on the timer D1 NE555. It is used in a somewhat unusual connection – as an LC generator. The oscillatory circuit of the generator consists of capacitors C1, C2 and the sensor inductance coil. The resonant frequency of the circuit is determined as:
where C is a series connection of capacitors C1 and C2. Since the microcontroller automatically adjusts to the frequency of the measuring generator, the circuit does not provide for adjusting the generator frequency. When using a sensor with a diameter of 190 mm (100 turns) and capacitor capacities C1=0.047 F and C2=0.01 F, the frequency will be about 20 kHz. If necessary, it can be changed by replacing capacitors C1, C2. In this case, it is desirable that their capacities are in a ratio of approximately (4 … 6): 1.
The D2 microcontroller is responsible for all other functions for processing the signal of the measuring generator, including indication. This circuit uses the AT90S2313 microcontroller described above. Industrial version (temperature range -40C…+85C). This is done so that the device can be used in the field at sub-zero temperatures. Both the controls and indication elements are connected directly to the microcontroller chip. The metal detector has two operating modes, which are set using switch S1 – static and dynamic. In the static mode, the signal, which is a digital code of the frequency difference, is logarithmized and immediately sent to the indication. Each level of light indication is accompanied by its own tone of sound indication.
The dynamic mode is designed to search for targets in difficult conditions, against the background of interference from the soil, minerals, etc. In the dynamic mode, the signal undergoes digital filtering, which selects the useful signal from the background of interfering signals. In our device, we used optimal matched filtering. In short, its essence is that for any signal there is an optimal filter that allows you to get the maximum response at the filter output. We have implemented such a digital filter for the frequency detuning signal that occurs when the search coil moves over small targets at a speed of 0.5-1 m/s. The filter is implemented in software.
The sensitivity of the device is adjusted using the variable resistor R6. The LEDs VD1…VD3 indicate the level of deviation of the measuring generator frequency in the case of prevalence of the ferromagnetic effect. The LEDs VD5…VD7 – in the case of prevalence of the conductivity effect. The LED VD4 indicates zero frequency shift. The earphone Y is intended for audible indication of deviation of the measuring generator signal frequency.
The circuit contains a record low number of components. At the same time, no special requirements are put forward to them. The AT90S2313-10PI chip can be replaced with AT90S2313-10PC, however, in this case, operation at temperatures below 0C is not guaranteed. (which may well be the case in field conditions).
Microcircuit D1 can be replaced with KR1006VI1. It is advisable to choose LEDs with increased brightness. Stabilizer D3 can be replaced with K1184EN1 or, which is slightly worse, 78L05. In the latter case, the minimum permissible battery voltage will be 6.7 V. There are no special requirements for resistors. They can have a dissipated power of 0.125-0.25 W.
Capacitors C1 and C2 – must have a minimum TKE, especially C2. There are no special requirements for the other capacitors.
Earphone Y (or earphones) can be taken from the player. It may be necessary to select the value of resistor R3 to obtain acceptable volume. In extreme cases, the earphone can be replaced with a piezo emitter.
The design of the device body can be quite arbitrary.
The search coil design deserves special attention – it can be implemented in various ways. The main requirements for it are rigidity of the design, tightness and the presence of an electrostatic screen. The following coil manufacturing technology can be proposed:
A board of a suitable size is taken and a circle with a diameter of 190 mm is drawn on it. Then small nails are hammered into the board evenly around the circumference – 15…20 pieces. 100 turns of enameled wire with a diameter of 0.3 – 0.56 mm are wound on these nails. After winding, the nails are removed or bent and the coil is removed from the mandrel. The next step is to wind the coil with electrical tape. The winding is done with an overlap. See Fig. 6
Figure 6. Wrapping the coil with adhesive tape
In a similar manner, a layer of aluminum foil is applied over the layer of adhesive tape, which serves as a screen for the sensor winding. For this, the foil is cut into strips about 10 mm wide. To prevent the formation of a short-circuited turn, which reduces the quality factor of the circuit, the foil winding should not occupy the entire surface of the sensor winding ring – a small section of 10-20 mm in length is left free from the foil. The lead from the screen is made with a tinned single-core wire, which is secured with a knot over the screen. Finally, the sensor winding ring is wrapped with another layer of adhesive tape over the entire surface, releasing the leads of the winding and screen. A shielded cable is soldered to these leads, which connects the coil to the metal detector. Rigidity of the coil can be imparted in various ways. One of them is to select a suitable case, for example, take the lid of a set of plastic dishes, place the coil in it and fill it with epoxy resin. First, you need to make a hole in the case and thread the cable through it. Also, a rod mount must be provided on the coil body.
The type of printed circuit board, the arrangement of elements on the printed circuit board and the printed circuit board drawing (M1:1) are shown in Fig. 7, 8 and 9.
Setting up the device
The following procedure for setting up the device can be suggested.
Check the correctness of the circuit installation and apply power.
Measure the current consumption. It should not exceed 15 mA.
Make sure that pin 3 of the D1 microcircuit has a meander of the calculated frequency (about 20 kHz for the above-mentioned ratings of the capacitors C1 and C2 and the standard sensor).
Remove the device frame away from metal objects and press the S0 “Reset” button.
Make sure the indicator organs are working by bringing various metal objects to the sensor.
Working with the device
If switch S1 is closed, the device switches to the static mode. In this mode, when the coil approaches a ferromagnetic target, LEDs VD3, VD2, VD1 start to light up in sequence. If the coil is brought closer to a non-ferromagnetic metal object, LEDs VD5, VD6, VD7 will light up in sequence. Unfortunately, the device reacts in the same way to iron objects with a large surface area (for example, a tin can). This is due to the fact that when the search coil is exposed to metal ferromagnetic objects, two effects occur at once – the conductivity effect and the ferromagnetic effect. At a certain ratio of the surface area of the object to the volume, the conductivity effect begins to prevail.
When the switch S1 is opened, the device switches to the dynamic mode. In this mode, the coil should move above the ground at a speed of approximately 0.5-1 m/s. The location of an object in the dynamic mode is found using the “artillery fork” method by moving the coil above the object twice – from left to right and from right to left. In this mode, it is important to feel the lowest speed at which the coil can be moved. This can be easily mastered with a short training. The indication in the dynamic mode looks a little different. When moving the coil above a ferromagnetic object, the LEDs from the “scale” VD5, VD6, VD7 first light up, and then from the “scale” VD3, VD2, VD1. When moving the coil above a non-ferromagnetic object, the indication works the other way around.
As already mentioned above, each LED has its own tone of sound indication. After a short period of work with the metal detector, the “chants” characteristic of different types of targets are remembered. This allows you to use mainly sound indication when searching, which is quite convenient.
Before starting work in both modes, it is necessary to set the optimal sensitivity of the device using the variable resistor R6. It is set to such a position that the device begins to indicate false responses. Then, slowly rotating the rotor of this resistor, it is necessary to achieve the disappearance of these false responses.
All other things being equal, the dynamic mode allows for better sensitivity than the static mode due to filtering. However, the static mode is sometimes necessary. For example, it is necessary to check the bottom of a narrow pit. In this case, it is not possible to perform horizontal swings of the search coil, which are necessary for the dynamic mode. The static mode will help out here.
During field tests, the metal detector showed good results.
Download: Microcontroller firmware file