Build Your Own Metal Detector with an Arduino

October 03, 2016 by Evan Kale

Building a metal detector using a Colpitts oscillator and an Arduino.

Learn how to build a metal detector using a Colpitts oscillator and an Arduino.

How Do Metal Detectors Work?

Tank circuit

The tank circuit


In the above circuit, the series capacitor and inductor form a tank circuit. In a tank circuit, energy is transferred repeatedly between a capacitor and an inductor, resulting in oscillation. Current discharged from the capacitor flows through the inductor; when the capacitor is completely discharged, the inductor's decreasing magnetic field maintains the current flow. The capacitor will then charge with the opposite polarity, and when the magnetic field has completely collapsed, the capacitor will discharge, resulting in current flow in the direction opposite to that of the original current. This cycle continues.

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 for your DIY Metal Detector

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.

Metal Detection Circuit 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).


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.

How Functional is Our Arduino-Based Metal Detector?

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!

Give this project a try for yourself! Get the BOM.

  • AEKron October 03, 2016

    We didn’t have Arduinos when I mead my metal detector in 8th grade electronics class back in 1973. We did get to make the PCB, vacu-form the coil mold, and pour acrylic plastic into the mold for the detector.
    Mine never worked…

    Like. Reply
  • upand_at_them October 03, 2016

    When the magnetic field of an inductor collapses the current created flows in the SAME direction as the current that charged it.

    Like. Reply
  • Fixate October 08, 2016

    Hey Evan!

    Do you have any specs on the performance of this metal detector? (Penetration, size of the detection “box”)

    Also, how can we make it BETTER?? 😊
    (More penetration)

    Like. Reply
    • redrooster01 October 10, 2016
      He explained how to do that when he said how to extend the range? More Voltage=more current=more gain=more magnetism=more penetration. Oh and more windings on the coil. lol
      Like. Reply
  • ali bakhtiyari December 15, 2016

    Can i use this oscillator circuit with a bigger loop( 1.8×1.8 meters about 130uH) and arduino due to make a car detector?

    Like. Reply
  • Albert Albert September 18, 2017

    how to increase voltage input to the oscillator , is it safe to use a 12v power supply instead of 4.5v ?

    Like. Reply
    • R
      Robert Keim September 26, 2017
      You would have to check to see if your microcontroller board can tolerate a 12V power supply.
      Like. Reply
  • EasyGoing1 October 05, 2017

    VERY nice write-up - well done with excellent detail!  I need to build something like this but the loop needs to be about 1.5 feet in diameter to detect the passing of a bullet. Your article has helped in my pursuit.  Thank you

    Like. Reply
    • xmass January 03, 2018
      did you finished your "bullet passing" project? I used a computer soundcard, two ir leds and sensors, two AA batetries and Audacity to count the time passed from first impulse to the second one; it was an airsoft chronograph
      Like. Reply
  • B
    bassmanjess January 19, 2018

    hi, i put this oscillator circuit together on a bread board but the most variation on the frequency is about 100hz. the article says it should be about 100 times that. i am measuring the frequency with my multimeter, could this be doing anything to the circuit? or maybe the breadboard?
    any help on this would be appreciated.

    Like. Reply
    • RK37 February 01, 2018
      I wouldn't expect a multimeter or a breadboard to cause that amount of deviation from the expected frequency. A great way to get help with a problem such as this is to post it as a question in the forum.
      Like. Reply
  • AlexOlesco March 29, 2018

    Any one already try this?, how’s the range of this, in how many meters? Thanks

    Like. Reply
  • A
    abe_akif April 23, 2018

    hi. may i know length of the wire that you use i search coil? and how about simulation that you used in this project?

    Like. Reply
  • C
    CONN1 May 24, 2018


    Like. Reply
  • Erik84750 October 27, 2018

    Nice program, works well. However oscillator needs minimal 10V to supply stable and usefull signal, preferably even 20V. Also would be advisable to use 100nF cap from collector to output in order to eliminate the DC component; also recommended to use 74HC132 schmitt trigger to supply nice square wave to the Arduino input.

    Like. Reply
  • hamid reza January 05, 2019

    I want to use gold to find it. Will it be?
    What is the rumor of finding metal?

    Like. Reply
  • B
    boiporitto April 30, 2019

    I am interested actually about gould nuggets and treasure. Can it be used for detecting Gold? If yes, then awesome. If not, please suggest me the best option as per your knowledge.

    Like. Reply
  • Slarti Fartfast July 07, 2019

    Nice work,  Evan;  I subbed a 548 transistor for the 338, used a smaller 45 turnx50mm coil from another (failed) detector project, and dropped the buzzer and motor control stuff in favour of a simple LED alert on pin 9, and it still worked!  My plan is for this to be a cat detector (triggered from copper wire in their collars!) because my RFID detector wasn’t sensitive enough.

    (yeah, I know - if the neighbours put a bell on THEIR cat , he might get past, but hey I’ll worry about that then!)

    Like. Reply
    • AminAbudahab August 19, 2020
      Hi there, I have a similar purpose, was wondering if it's okay to leave the circuit running forever? I.e keep the trigger pressed
      Like. Reply
  • Ajeet Rajawat July 16, 2019

    you are using a crystal on your pcb circuit… but you never says anything about it ... and not showing in our circuit diagram…

    Like. Reply
    • J
      juanemc2 March 28, 2020
      I have been building this project with some trouble. I think the main problem is the missing crystal. Though the microcontroller can work on its internal clock, it has to be programmed differently to do this. I could make this work with an arduino, the tank circuit and the sound little part with the transistor and the 1.2 kohms resistence. I used BC547 transistors for both cases because the ones listed here aren't easy to get in my country.
      Like. Reply
      • H
        hipsterkevster March 29, 2020
        If you're using an Arduino, then refer to this guide to load the 8mhz internal clock bootloader: Scroll down to Minimal Circuit section. You can take out the chip from the arduino board after and don't connect the XTAL pins.
        Like. Reply
    • H
      hipsterkevster March 29, 2020
      You don't need a crystal. In fact you shouldn't be using a crystal on battery powered projects like this to maximize the operating voltage range. If you use a 16mhz crystal, your atmega dies when battery drops below ~3.78v. At 8mhz (internal clock), you can still run below 2.7v. See "Minimal Circuit (Eliminating the External Clock)" in this link: Also refer to the atmega datasheet
      Like. Reply
  • J
    juanemc2 March 28, 2020

    The circuit does not work without a 16Mhz crystal for the Atmega 328.

    Like. Reply
  • J
    juanemc2 March 28, 2020

    I used two BC547 transistors to replace both of the ones in the circuit with success.

    Like. Reply
  • A
    alices02 June 05, 2020

    what are the changes needed if realized with an Arduino Uno?

    Like. Reply