PIC Based Metal Detector
Introduction
This project is a metal detector using a frequency counting approach. It can generate an audio output, or connect to a computer using a 5 V TTL serial connection. PIC Based Metal Detector
Circuit
The circuit for this project consists of an LC oscillator with a frequency of around 4 MHz and a PIC microcontroller which can count the oscillations. The inductor is a detector coil, with an inductance that changes slightly in the presence of metal. The PIC’s PWM output is connected to a small current buffer amplifier so that the metal detector can be used without being attached to a computer.
Connector | Purpose |
---|---|
K1 | 5 V DC power |
K2 | Detector coil |
K3 | 5 V TTL Serial |
K4 | Audio Out |
K5 | Expansion GPIO |
Component | Count | Value | Example |
---|---|---|---|
C1, C2 | 2 | 100pF, ±5% | CAP, CERAMIC, 100PF, 50V, Y5P, DISC |
C4, C5 | 2 | 4.7nF, ±5% | CAPACITOR, 4.7NF, 50V |
C3, C6 | 2 | 100nF, +80%, -5% | CAP, CERAMIC, 0.1UF, 50V, Y5V |
C7 | 1 | 100µF, ±20% | CAPACITOR, N/P, 100UF, 16V |
IC1 | 3 | PIC16F648A | IC, 8BIT FLASH MCU, 16F648, DIP18 |
LED1, LED2, LED3 | 3 | 3mm, 2V, 20mA | LED, 3MM, GREEN, 30MCD, 565NM |
R1, R10, R11, R12, R13, R14 | 6 | 1kΩ, ±5%, 0.125W | RESISTOR, CARBON FILM, 1K, 125MW, 5% |
R4 | 1 | 2.2kΩ, ±5%, 0.125W | RESISTOR, CARBON FILM, 125MW, 2K2 |
R2, R5, R7, R8, R9 | 5 | 10kΩ, ±5%, 0.125W | RESISTOR, CARBON FILM, 10K, 125MW, 5% |
R6 | 1 | 22kΩ, ±5%, 0.125W | RESISTOR, CARBON FILM, 125MW, 22K |
R3 | 1 | 47kΩ, ±5%, 0.125W | RESISTOR, CARBON FILM, 125MW, 47K |
S1 | 1 | Single Pole Single Throw Make on Push Button | SWITCH, 6X6, SPNO, 260GF |
Q1, Q2, Q3 | 3 | 2N5551 | TRANSISTOR, NPN, TO-92 |
In my build, I have added a bit of additional circuitry for future enhancement: a few LEDs (which I use as power, PWM active, and counter active indicators), and a GPIO header. I have also added a second power header to connect to other boards where the detector is used as part of a larger system, or to power a TTL to RS-232 board.
Firmware
The microcontroller firmware is responsible for counting the number of oscillations over a fixed period of time, setting the frequency of the PWM if audio output is enabled, controlling the LEDs, processing commands from the serial interface, and returning oscillation counts over the serial interface. The serial interface runs at 9600 baud; there is a set of scripts to control the metal detector in the firmware tarball.
When audio output is enabled, the firmware automatically calibrates itself – adjusting the expected count, the deviation from which the frequency of the audio output is derived. The length of time to count for can be configured, and stored in EEPROM post-programming. The rate at which self-calibration occurs is also configurable in the same way.
The firmware recognises the following commands:
Command | Description |
---|---|
C | Toggle continuous counting and output to the serial port |
c | Output one count over the serial port |
L | Load a value from EEPROM: ‘c’ for count time, and ‘r’ for reference adjust rate |
p | Enable (PWM) audio output; audio output may also be enabled using the physical button |
S | Store a value to EEPROM: ‘c’ for count time, and ‘r’ for reference adjust rate |
T | Print a firmware timestamp |
The Metal Detector Firmware is built with SDCC and gputils. The tarball contains both source code and a pre-built hex file. The current version of the firmware is: 2013112501.
Build
The most important part of the build is the detector coil. It should be designed to give a frequency of oscillation between around 2 MHz (to give a high enough count in a short period of time) and 5 MHz (to stay within the timing limits of the PIC’s counter peripheral). Frequencies outside this range may also work. The coil features in my build is a rectangle: 70 mm x 60 mm, with 10 turns of stranded 10 x 0.254 mm wire. It is advisable to put the circuit near the detector in a box to reduce noise caused by changing the shape of the connection between the oscillator circuit and the detector coil.