Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Arduino
80 min
Share

Build a Metal Detector using Arduino

Metal detection is a great pastime that allows you to enjoy the outdoors, explore new places. A metal detector is a device used to detect the presence of metal in its proximity without physical contact. In this tutorial, we will build a metal detector using an Arduino.

Project Video

Overview

In this tutorial, we will learn how to use an Arduino to create a metal detector that can detect the presence of metals, sound an alarm when metal is detected.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
Multilayer Ceramic Capacitor (5 pcs)
Get Item
Rectifier Diodes 1N4007 (4 pack)
Get Item
1/4 Watt Resistor (20 Pack)
Get Item
Passive Buzzer – 5V
Get Item
Half-size Breadboard
Get Item
Jumper Wires - Male to Male (40 Pack)
Get Item

Steps

Wiring it Up

Make the coil yourself from any insulated copper wire, 3 meters long and ½ to 2 mm thick, and wrap it in circular coils with a diameter of 5 cm.

Connect the wires between the Arduino and the rest of the electronic components as shown in the image below.


Connections from the Arduino to the breadboard :



• Arduino GND pin → Breadboard ground line

• Arduino 5V pin → Breadboard 5V line



Connections from the 10 n capacitor :



• capacitor first pin → Breadboard ground line → coil first wire

• capacitor second pin → diode first pin → Arduino A1 pin



Connections from the diode :



• diode first pin → capacitor second pin → Arduino A1 pin

• diode second pin → 330 ohm resistor first pin



Connections from the 330 ohm resistor :



• 330 ohm resistor first pin → diode second pin

• 330 ohm resistor second pin → coil second wire → Arduino A0 pin



Connections from the passive buzzer :



• passive buzzer GND pin → Breadboard ground line

• passive buzzer VCC pin (+ pin) → Arduino pin 3

Coding


/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial:

Tutorial: Build a Metal Detector using Arduino!

The purpose of this sketch is to detect the presence of metals, sound an alarm when metal is detected.

Connections from the Arduino to the breadboard:

• Arduino GND pin → Breadboard ground line

• Arduino 5V pin → Breadboard 5V line

Connections from the 10 n capacitor:

• capacitor first pin → Breadboard ground line → coil first wire

• capacitor second pin → diode first pin → Arduino A1 pin

Connections from the diode:

• diode first pin → capacitor second pin → Arduino A1 pin

• diode second pin → 330 ohm resistor first pin

Connections from the 330 ohm resistor:

• 330 ohm resistor first pin → diode second pin

• 330 ohm resistor second pin → coil second wire → Arduino A0 pin

Connections from the passive buzzer :

• passive buzzer GND pin → Breadboard ground line

• passive buzzer VCC pin (+ pin) → Arduino pin 3


*/


const byte npulse = 12; // number of pulses to charge the capacitor before each measurement

const byte pin_pulse = A0; // sends pulses to charge the capacitor (can be a digital pin)
const byte pin_cap  = A1; // measures the capacitor charge
const byte pin_LED = 3; // LED that turns on when metal is detected

void setup() {
 pinMode(pin_pulse, OUTPUT);
 digitalWrite(pin_pulse, LOW);
 pinMode(pin_cap, INPUT);
 pinMode(pin_LED, OUTPUT);
 digitalWrite(pin_LED, LOW);
}

const int nmeas = 256; //measurements to take
long int sumsum = 0; //running sum of 64 sums
long int skip = 0; //number of skipped sums
long int diff = 0;      //difference between sum and avgsum
long int flash_period = 0; //period (in ms)
long unsigned int prev_flash = 0; //time stamp of previous flash

void loop() {

 int minval = 2000;
 int maxval = 0;

 //perform measurement
 long unsigned int sum = 0;
 for (int imeas = 0; imeas < nmeas + 2; imeas++) {
   //reset the capacitor
   pinMode(pin_cap, OUTPUT);
   digitalWrite(pin_cap, LOW);
   delayMicroseconds(20);
   pinMode(pin_cap, INPUT);
   //apply pulses
   for (int ipulse = 0; ipulse < npulse; ipulse++) {
     digitalWrite(pin_pulse, HIGH); //takes 3.5 microseconds
     delayMicroseconds(3);
     digitalWrite(pin_pulse, LOW); //takes 3.5 microseconds
     delayMicroseconds(3);
   }
   //read the charge on the capacitor
   int val = analogRead(pin_cap); //takes 13x8=104 microseconds
   minval = min(val, minval);
   maxval = max(val, maxval);
   sum += val;

   //determine if LEDs should be on or off
   long unsigned int timestamp = millis();
   byte ledstat = 0;
   if (timestamp < prev_flash +12) {
     if (diff > 0)ledstat = 1;
     if (diff < 0)ledstat = 2;
   }
   if (timestamp > prev_flash + flash_period) {
     if (diff > 0)ledstat = 1;
     if (diff < 0)ledstat = 2;
     prev_flash = timestamp;
   }
   if (flash_period > 1000)ledstat = 0;

   //switch the LEDs to this setting
   if (ledstat == 0) {
     digitalWrite(pin_LED, LOW);
   }
   if (ledstat == 1) {
     digitalWrite(pin_LED, LOW);
   }
   if (ledstat == 2) {
     digitalWrite(pin_LED, HIGH);
   }

 }

 //subtract minimum and maximum value to remove spikes
 sum -= minval; sum -= maxval;

 //process
 if (sumsum == 0) sumsum = sum << 6; //set sumsum to expected value
 long int avgsum = (sumsum + 32) >> 6;
 diff = sum - avgsum;
 if (abs(diff)<avgsum >> 10) {   //adjust for small changes
   sumsum = sumsum + sum - avgsum;
   skip = 0;
 } else {
   skip++;
 }
 if (skip > 64) {  // break off in case of prolonged skipping
   sumsum = sum << 6;
   skip = 0;
 }

 // one permille change = 2 ticks/s
 if (diff == 0) flash_period = 1000000;
 else flash_period = avgsum / (2 * abs(diff));
}

Testing it Out

Once you’ve uploaded the code to the Arduino board, you will find that the buzzer will sound an alarm when metal is detected, as shown in the video below.

Resources

No items found.