Introduction: Arduino Mega 2560 Metal Detector

About: I love nature, electronics, and mechanics. I love to reuse whatever trash I find. I love building random projects and gardening.

Do you need to find a lost metal object, search for lost pocket change, have fun at the landfill collecting old items and making use of them, or collect old coins? If so, then this metal detector is for you. It is cheap and reliable and does what an $100 detector would do.

Supplies

  • arduino mega 2560
  • low-forward scottky diode
  • 0.1 uf mylar capacitor
  • small 0.5w 4~8 ohm speaker
  • 11-turn, 6 cm (diameter) coil
  • usb power bank (should fit in your hand)
  • USB-B-USB-A cable
  • USB-C-USB-A cable

Step 1: Plug in the Power Bank

Plug in the power bank to charge using the USB-A-USB-C cable.

Step 2: Wind the Coil

If you are making your coil youself, then go and wind it. Wind it around an average yogurt bottle or something similar. After winding it, leave the two ends hanging off the coil, and put one piece of tape on each side of the coil. See the images in other steps for more details on how it should look when finished. Also, make sure to strip a bit of insulation off of each end of the coil's wires before connecting to ensure a solid connection.

Step 3: Build the Main Circuit

I built the circuit using an Elenco Snap Circuits Prototyping Kit. You can use whatever you want (breadboard, perfboard).

Step 4: Upload the Code

Here is the code. Copy-paste this code into your IDE or open the file. Afterwards, select your board and hit upload. The ".ino" file is also included at the end of this step:


#define capPin A5


#define buz 31


#define pulsePin A4



#define led 13



long sumExpect=0; //running sum of 64 sums 


long ignor=0;  //number of ignored sums


long diff=0;    //difference between sum and avgsum


long pTime=0;


long buzPeriod=0; 



void setup() 


{


 Serial.begin(9600);


 pinMode(pulsePin, OUTPUT); 


 digitalWrite(pulsePin, LOW);


 pinMode(capPin, INPUT);  


 pinMode(buz, OUTPUT);


 digitalWrite(buz, LOW);


 pinMode(led, OUTPUT);

 pinMode(led, LOW);


}



void loop() 


{


 int minval=1023;


 int maxval=0;


 long unsigned int sum=0;


 for (int i=0; i<256; i++)


 {


  //reset the capacitor


  pinMode(capPin,OUTPUT);


  digitalWrite(capPin,LOW);


  delayMicroseconds(20);


  pinMode(capPin,INPUT);


  applyPulses();



  //read the charge of capacitor


  int val = analogRead(capPin); //takes 13x8=104 microseconds


  minval = min(val,minval);


  maxval = max(val,maxval);


  sum+=val;



  long unsigned int cTime=millis();


  char buzState=0;


  if (cTime<pTime+10)


  {


   if (diff>0)


    buzState=1;


   else if(diff<0)


    buzState=2;


  }


  if (cTime>pTime+buzPeriod)


  {


   if (diff>0)


   buzState=1;


   else if (diff<0)


   buzState=2;


   pTime=cTime;  


  }


  if (buzPeriod>300)


  buzState=0;



  if (buzState==0)


  {


   digitalWrite(led, LOW);


   noTone(buz);


  }  


  else if (buzState==1)


  {


   tone(buz,2000);


   digitalWrite(led, HIGH);


  }



  else if (buzState==2)


  {


   tone(buz,500);


   digitalWrite(led, HIGH);


  }


 }



 //subtract minimum and maximum value to remove spikes


 sum-=minval; 


 sum-=maxval;



 if (sumExpect==0) 


 sumExpect=sum<<6; //set sumExpect to expected value


 long int avgsum=(sumExpect+32)>>6; 


 diff=sum-avgsum;


 if (abs(diff)<avgsum>>10)


 {


  sumExpect=sumExpect+sum-avgsum;


  ignor=0;


 } 


 else 


  ignor++;


 if (ignor>64)


 { 


  sumExpect=sum<<6;


  ignor=0;


 }


 if (diff==0) 


  buzPeriod=1000000;


 else 


 buzPeriod=avgsum/(2*abs(diff));   


}



void applyPulses()


{


  for (int i=0;i<3;i++) 


  {


   digitalWrite(pulsePin,HIGH); //take 3.5 uS


   delayMicroseconds(3);


   digitalWrite(pulsePin,LOW); //take 3.5 uS


   delayMicroseconds(3);


  }


}

Step 5: Connect the Power Bank

Disconnect the power bank from the USB-C-USB-A wire and connect the USB-B-USB-A wire between the arduino and power bank. To make it neat, attach the power bank to your project. I just placed the power bank on the unused side of my Elenco Snap Circuits baseplate.

Step 6: Use

This detector will continuously produce a high-pitched tone in short bursts when no metal is detected. This tone will change to a low-pitched one when metal is detected. To charge, simply disconnect the arduino from the power bank and connect the USB-C-USB-A cable to the charging port. I included the full diagram with the power bank both charging and powering the arduino at the same time. This is not good for the power bank's batteries, and only the port labeled "OUT" should be connected to the arduino while using, while only the "CHRG" port connected when charging. It's just an explanation on how everything should be connected.

Step 7: Original Source

I never steal information and make everyone think that I created it myself. This instructable's circuit was based on a circuit posted on circuitdigest.com. I modified the circuit by removing the resistor and reducing the number of turns in the coil. Also, I did not use a tape roll for supporting the coil. That way, I could make the coil smaller, which I did.