Build Your Own Metal Detector with an Arduino

How does it work?
Tank circuit
The tank circuit

In the above circuit, the series capacitor and inductor form a tank circuit. In an ideal tank circuit, the electrical energy dissipated from the capacitor will charge the inductor, storing the energy as a magnetic field.

After the capacitor discharges, the magnetic field built up by the inductor begins to collapse, creating a current flow in the opposite direction from which it was charged with. This, in turn, charges the capacitor once again. When the magnetic field has collapsed, this cycle repeats. The continuous energy build-up between the capacitor and inductor creates an oscillating current between the two components.

The inductor of the above tank circuit forms the detector of the metal detector (a large coil of wire). When metallic material approaches the center of the inductor (the detector coil), it enters the magnetic field created by the inductor. This changes the magnetic permeability of the inductor’s core, causing the inductance to change. The change in inductance, in turn, changes the oscillating frequency of the tank circuit.

If the components were ideal, the tank circuit would oscillate indefinitely without an external power source. But, in practice, the components are non-ideal. The unwanted resistance of the components will introduce energy loss, causing the oscillating current to taper to a stop. To counter this, a single stage BJT inverting amplifier is used to continuously add gain into the tank circuit.

Colpitts oscillator
The Colpitts oscillator

Since the oscillation at the nodes before and after the inductor are 180° out of phase of with each other, one of the nodes will supply the oscillation to the transistor base, amplify and invert the signal at the collector, then return it in phase to the other node of the tank circuit. This entire circuit is called the Colpitts oscillator.
The Colpitts oscillator above provides a steady oscillation with a frequency in the 100kHz range. Metals from household items changing the permeability of the inductor core will fluctuate this frequency around 10kHz. Since this frequency range is outside of the human audio spectrum (20Hz to 20kHz), we will need to translate the oscillation into an audible tone.
Traditional BFO (beat-frequency oscillator) metal detectors overcome this problem by incorporating another tank circuit with a fixed frequency equal to the frequency of the detector tank circuit without the influence of any metals. Then, taking the difference between the two frequencies will isolate the fluctuating frequencies of the detector circuit and bring it down to an audible range.
For this metal detector project, we will be using an Arduino to process the oscillation signal instead of offsetting the oscillation with a second tank circuit. The Arduino will store the fixed frequency and continuously compare the incoming frequency of the detector circuit with the stored frequency (more on the Arduino program below).

Materials


Weed-whacker toy

For this project, a toy weed-whacker was chosen to house all the components. It includes the following features:
  • a trigger button, which we will repurpose to trigger the speaker
  • a side button, which we will use to set the fixed frequency
  • a battery compartment (3xAA batteries) with an ON/OFF switch
  • a speaker, which we will play the tone through
  • a motor with LEDs attached which we will be activated when the frequency difference exceeds a certain threshold
  • a circular head where we will fit a coil of wire into for the inductor of the tank circuit 
We will also add a potentiometer (silver) to make the sensitivity of the tone changes adjustable.

Detector coil

The inductor coil is made from approximately 50 wraps of 26 AWG wire around a spool of 5.5 inches in diameter.


Inside the housing

Inside the housing, we will replace the original circuit board with our own circuit and attach all the peripherals to the circuit with pin headers.

Schematics


Full schematics

I used an Arduino UNO to program a DIP ATMega328. I then removed the ATMega328 from the development board and embedded into a perfboard along with the rest of the circuit.
The Colpitts oscillator, on the bottom left on the diagram, feeds the oscillation into counter 1 (pin T1) of the chip (marked as digital pin 5 on the Arduino UNO), where it constantly counts the frequency of oscillation.
On the top level of the diagram, a power supply of 4.5V (3xAA batteries, with bypass capacitors) is used to power the ATmega328, oscillator, speaker, and motor (with LEDs).
To keep the current draw of the microcontroller’s digital pins at a safe level (40 mA per pin maximum for the ATmega328), an NPN transistor (C2878) is used to drive the speaker, and an N-channel MOSFET (SUB45N03) to drive the motor.
Both the trigger and reset (sets fixed frequency) switches are wired to digital pins using internal pull-up configuration. Small capacitors are added in parallel to debounce the switches.
The sensitivity potentiometer is set up as a voltage divider, and the division is read using an analog pin.

Code Walkthrough

The full source code for this project can be found here:
Below is a detailed walkthrough of the code.

Setup Function

To keep track of the detector oscillation frequency through timer counter 1, we first need to configure the timer/counter controller registers (TCCR). These TCCRs are accessed through the three integers: TTCR1A, TTCR1B, and TTCR1C.

                    TCCR1A = 0b00000000;
TCCR1B = 0b00000111;
                  

We will need to set the waveform generation to normal mode by setting the WGM flags of TCCR1A and TCCR1B to 0, and set the clock speed selection mode to external clock source by setting CS flags of TCCR1B to mode 3 (external clock on rising edge). In this configuration, the register OCR1A will decrement by 1 every time a rising edge is detected from the oscillation.

                    TIMSK1 |= (1 << OCIE1A);
                  

Next we'll need to enable timer/count interrupt A by setting the OCIE1A flag in TIMSK1 register. This will enable the SIGNAL(TIMER1_COMPA_vect) interrupt function to be called whenever OCR1A register reaches 0.

                    OCR1A = 1;
                  

Now initialize OCR1A to 1 so that the interrupt function is called as soon as the first rising edge is detected.

Interrupt Function

This is the SIGNAL(TIMER1_COMPA_vect) function. It's called when the OCR1A register reaches 0. In this function, we want to keep track of the number of microseconds elapsed since the last time the function was called. This time delta is stored as signalTimeDelta.
storedTimeDelta is the “fixed frequency” time delta that signalTimeDelta is compared to in the main loop. storedTimeDelta is set to signalTimeDelta when storedTimeDelta is zeroed (on bootup and when the reset switch is pressed).

                    OCR1A += CYCLES_PER_SIGNAL;
                  

After performing interrupt operations, OCR1A needs to be reset by incrementing it with our predefined constant, CYCLES_PER_SIGNAL (number of cycles before next interrupt occurs).

Loop Function

In the loop function, we check if the trigger is pressed. If so, then read the analog value of the sensitivity potentiometer and linearly interpolate the analog value (0 to 1023) to an easier to use scale (0.5 to 10.0).

                    int storedTimeDeltaDifference = (storedTimeDelta - signalTimeDelta) * sensitivity;
                  

The difference between the fixed frequency (storedTimeDelta) and measured frequency (signalTimeDelta) is calculated and multiplied by the sensitivity value.

                    tone(SPEAKER_PIN, BASE_TONE_FREQUENCY + storedTimeDeltaDifference);
                  

This value is then summed with an audible base tone frequency, BASE_TONE_FREQUENCY, and played out the speaker using the Arduino tone() function.
If the difference exceeds the threshold defined by SPINNER_THRESHOLD, then the motor is activated.
If the trigger is released, then the speaker tone is stopped (by calling noTone() function) and the motor is deactivated.
If the reset button has been pressed, it will zero storedTimeDelta, allowing the next interrupt call to set a new value.

Conclusion

With the lowest sensitivity setting, the metal detector can pick up large items like soda cans, cell phones, and iron tools within a few inches away from the coil. On the highest sensitivity setting, smaller items like steel rings, screws, and coins within the same proximity can also be detected. See the video at the top of the article for a demonstration!


To extend the range of the detector, we can increase the magnetic field area created by the inductor. This can be achieved by increasing the current flow through the inductor (by increasing voltage input to the oscillator, allowing a greater gain in the amplifier), or by increasing the number of wire wraps in the inductor coil.

With an Arduino-based metal detector, we can do other interesting things that cannot be done with traditional BFO metal detectors. Stay tuned for future projects on how we can take advantage of this metal detecting mechanism for other purposes!

Previous
Next Post »
My photo

Hi, I`m Sostenes, Electrical Technician and PLC`S Programmer.
Everyday I`m exploring the world of Electrical to find better solution for Automation. I believe everyday can become a Electrician with the right learning materials.
My goal with BLOG is to help you learn Electrical.
Related Posts Plugin for WordPress, Blogger...

Label

KITAIFA NEWS KIMATAIFA MICHEZO BURUDANI SIASA TECHNICAL ARTICLES f HAPA KAZI TU. LEKULE TV EDITORIALS ARTICLES DC DIGITAL ROBOTICS SEMICONDUCTORS MAKALA GENERATOR GALLERY AC EXPERIMENTS MANUFACTURING-ENGINEERING MAGAZETI REFERENCE IOT FUNDAMENTAL OF ELECTRICITY ELECTRONICS ELECTRICAL ENGINEER MEASUREMENT VIDEO ZANZIBAR YETU TRANSDUCER & SENSOR MITINDO ARDUINO RENEWABLE ENERGY AUTOMOBILE SYNCHRONOUS GENERATOR ELECTRICAL DISTRIBUTION CABLES DIGITAL ELECTRONICS AUTOMOTIVE PROTECTION SOLAR TEARDOWN DIODE AND CIRCUITS BASIC ELECTRICAL ELECTRONICS MOTOR SWITCHES CIRCUIT BREAKERS MICROCONTROLLER CIRCUITS THEORY PANEL BUILDING ELECTRONICS DEVICES MIRACLES SWITCHGEAR ANALOG MOBILE DEVICES CAMERA TECHNOLOGY GENERATION WEARABLES BATTERIES COMMUNICATION FREE CIRCUITS INDUSTRIAL AUTOMATION SPECIAL MACHINES ELECTRICAL SAFETY ENERGY EFFIDIENCY-BUILDING DRONE NUCLEAR ENERGY CONTROL SYSTEM FILTER`S SMATRPHONE BIOGAS POWER TANZIA BELT CONVEYOR MATERIAL HANDLING RELAY ELECTRICAL INSTRUMENTS PLC`S TRANSFORMER AC CIRCUITS CIRCUIT SCHEMATIC SYMBOLS DDISCRETE SEMICONDUCTOR CIRCUITS WIND POWER C.B DEVICES DC CIRCUITS DIODES AND RECTIFIERS FUSE SPECIAL TRANSFORMER THERMAL POWER PLANT cartoon CELL CHEMISTRY EARTHING SYSTEM ELECTRIC LAMP ENERGY SOURCE FUNDAMENTAL OF ELECTRICITY 2 BIPOLAR JUNCTION TRANSISTOR 555 TIMER CIRCUITS AUTOCAD C PROGRAMMING HYDRO POWER LOGIC GATES OPERATIONAL AMPLIFIER`S SOLID-STATE DEVICE THEORRY DEFECE & MILITARY FLUORESCENT LAMP HOME AUTOMATION INDUSTRIAL ROBOTICS ANDROID COMPUTER ELECTRICAL DRIVES GROUNDING SYSTEM BLUETOOTH CALCULUS REFERENCE DC METERING CIRCUITS DC NETWORK ANALYSIS ELECTRICAL SAFETY TIPS ELECTRICIAN SCHOOL ELECTRON TUBES FUNDAMENTAL OF ELECTRICITY 1 INDUCTION MACHINES INSULATIONS ALGEBRA REFERENCE HMI[Human Interface Machines] INDUCTION MOTOR KARNAUGH MAPPING USEUL EQUIATIONS AND CONVERSION FACTOR ANALOG INTEGRATED CIRCUITS BASIC CONCEPTS AND TEST EQUIPMENTS DIGITAL COMMUNICATION DIGITAL-ANALOG CONVERSION ELECTRICAL SOFTWARE GAS TURBINE ILLUMINATION OHM`S LAW POWER ELECTRONICS THYRISTOR USB AUDIO BOOLEAN ALGEBRA DIGITAL INTEGRATED CIRCUITS FUNDAMENTAL OF ELECTRICITY 3 PHYSICS OF CONDUCTORS AND INSULATORS SPECIAL MOTOR STEAM POWER PLANTS TESTING TRANSMISION LINE C-BISCUIT CAPACITORS COMBINATION LOGIC FUNCTION COMPLEX NUMBERS ELECTRICAL LAWS HMI[HUMANI INTERFACE MACHINES INVERTER LADDER DIAGRAM MULTIVIBRATORS RC AND L/R TIME CONSTANTS SCADA SERIES AND PARALLEL CIRCUITS USING THE SPICE CIRCUIT SIMULATION PROGRAM AMPLIFIERS AND ACTIVE DEVICES BASIC CONCEPTS OF ELECTRICITY CONDUCTOR AND INSULATORS TABLES CONDUITS FITTING AND SUPPORTS CONTROL MOTION ELECTRICAL INSTRUMENTATION SIGNALS ELECTRICAL TOOLS INDUCTORS LiDAR MAGNETISM AND ELECTROMAGNETISM PLYPHASE AC CIRCUITS RECLOSER SAFE LIVING WITH GAS AND LPG SAFETY CLOTHING STEPPER MOTOR SYNCHRONOUS MOTOR AC METRING CIRCUITS APPS & SOFTWARE BASIC AC THEORY BECOME AN ELECTRICIAN BINARY ARITHMETIC BUSHING DIGITAL STORAGE MEMROY ELECTRICIAN JOBS HEAT ENGINES HOME THEATER INPECTIONS LIGHT SABER MOSFET NUMERATION SYSTEM POWER FACTORS REACTANCE AND IMPEDANCE INDUCTIVE RESONANCE SCIENTIFIC NOTATION AND METRIC PREFIXES SULFURIC ACID TROUBLESHOOTING TROUBLESHOOTING-THEORY & PRACTICE 12C BUS APPLE BATTERIES AND POWER SYSTEMS ELECTROMECHANICAL RELAYS ENERGY EFFICIENCY-LIGHT INDUSTRIAL SAFETY EQUIPMENTS MEGGER MXED-FREQUENCY AC SIGNALS PRINCIPLE OF DIGITAL COMPUTING QUESTIONS REACTANCE AND IMPEDANCE-CAPATIVE RECTIFIER AND CONVERTERS SEQUENTIAL CIRCUITS SERRIES-PARALLEL COMBINATION CIRCUITS SHIFT REGISTERS BUILDING SERVICES COMPRESSOR CRANES DC MOTOR DRIVES DIVIDER CIRCUIT AND KIRCHHOFF`S LAW ELECTRICAL DISTRIBUTION EQUIPMENTS 1 ELECTRICAL DISTRIBUTION EQUIPMENTS B ELECTRICAL TOOL KIT ELECTRICIAN JOB DESCRIPTION LAPTOP THERMOCOUPLE TRIGONOMENTRY REFERENCE UART WIRELESS BIOMASS CONTACTOR ELECTRIC ILLUMINATION ELECTRICAL SAFETY TRAINING FILTER DESIGN HARDWARE INDUSTRIAL DRIVES JUNCTION FIELD-EFFECT TRANSISTORS NASA NUCLEAR POWER SCIENCE VALVE WWE oscilloscope 3D TECHNOLOGIES COLOR CODES ELECTRIC TRACTION FEATURED FLEXIBLE ELECTRONICS FLUKE GEARMOTORS INTRODUCTION LASSER MATERIAL PID PUMP SEAL ELECTRICIAN CAREER ELECTRICITY SUPPLY AND DISTRIBUTION MUSIC NEUTRAL PERIODIC TABLES OF THE ELEMENTS POLYPHASE AC CIRCUITS PROJECTS REATORS SATELLITE STAR DELTA VIBRATION WATERPROOF