Mastering Metallic Intelligence: ESP32 and Metal Detection Systems
In the architecture of modern security and industrial sorting, the ability to identify metallic objects without physical contact is a core technological pillar. The Metal Sensor allows the ESP32 to detect ferrous and non-ferrous materials using the principles of Electromagnetic Induction. This guide provides a deep-dive into Eddy Current formation, the mechanics of High-Frequency Oscillators, and the software engineering required to build responsive, cloud-connected detection systems.
How it Works: The Inductive Principle
Inductive proximity sensors consist of an induction coil, an oscillator, and a detection circuit. The oscillator creates a high-frequency electromagnetic field emanating from the sensing face. When a metal object enters this field, the electromagnetic energy induces small circulating currents called Eddy Currents on the metal's surface. These currents create their own opposing magnetic field, which dampens the sensor's internal oscillations. The ESP32 monitors this change in signal amplitude.
Ferrous vs. Non-Ferrous Detection
The sensor's range and sensitivity vary based on the type of metal. Iron and steel (Ferrous) are detected at the maximum rated distance. Aluminum and copper (Non-Ferrous) are also detectable but usually require the object to be much closer because they are non-magnetic and dissipate the field differently.
Sensor Types: KY-036 (Touch) vs. LJ12A3 (Industrial)
There are two common ways to detect metal with an ESP32. The KY-036 is a sensitive touch/proximity module ideal for small experiments. The LJ12A3-4-Z/BX is an industrial-grade NPN sensor capable of detecting metal from 4mm away. Warning: Industrial sensors often require 6V-36V. You must use a Voltage Divider or Optocoupler to drop the signal to 3.3V before connecting to the ESP32.
| Sensor Pin | Function | ESP32 GPIO Pin |
|---|---|---|
| VCC | Power (3.3V or External 12V) | 3V3 or External VCC |
| GND | Common Ground | GND |
| Digital Out (DO) | Metal Trigger (HIGH/LOW) | GPIO 14 |
| Analog Out (AO) | Signal Intensity | GPIO 34 (ADC1) |
Programming: Interrupt-Driven Detection
In industrial counting or security, metal objects may pass the sensor very quickly. Using a standard digitalRead() loop might miss these events. We use Hardware Interrupts to ensure the ESP32 captures every detection event with microsecond precision.
#define SENSOR_PIN 14
volatile int metalCount = 0;
void IRAM_ATTR onDetection() {
metalCount++;
}
void setup() {
Serial.begin(115200);
pinMode(SENSOR_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), onDetection, FALLING);
}
void loop() {
if (metalCount > 0) {
Serial.printf("Metal Detected! Total Count: %d\n", metalCount);
// Add WiFi Alert logic here
delay(100);
}
}
Advanced Feature: WiFi Conveyor Counter
The ESP32 can act as a bridge for a smart factory. By mounting a metal sensor on a conveyor belt, the ESP32 counts finished metallic parts and streams the data to an MQTT broker or Firebase. This allows managers to monitor production quotas in real-time from anywhere in the world.
Real-World IoT Use Cases
- Smart Parking: Burying inductive sensors in asphalt to detect the presence of a car (large metal mass) and updating a WiFi parking map.
- Handheld Security Scanners: Building an IoT-connected wand that vibrates and sends a cloud alert when concealed metal is found.
- Automatic Door Triggers: Detecting approaching metal carts or wheelchairs to open automatic doors in hospitals.
- Traffic Light Control: Using loop detectors (larger versions of these sensors) to trigger green lights when a vehicle is waiting.
Common Pitfalls (Troubleshooting)
- Voltage Overload: Connecting a 12V industrial sensor directly to the ESP32 will destroy the GPIO. Always use a level shifter.
- Mounting Proximity: Metal sensors can detect their own mounting screws if they are made of metal. Use plastic brackets or ensure the sensor head extends past the mount.
- Magnetic Interference: Strong permanent magnets nearby can saturate the induction coil and cause false readings.
- Hysteresis Issues: If the sensor flickers on and off at the edge of its range, implement a 'Debounce' timer in your code to stabilize the signal.
Frequently Asked Questions (FAQs)
Q: Can this sensor find gold or silver? A: Yes, gold and silver are conductive and will generate eddy currents, but they require higher sensitivity than iron.
Q: What is the maximum range? A: For small modules, it is usually 1-5mm. Industrial sensors can reach up to 50mm, but they are significantly larger and more expensive.
Final Summary
Interfacing a Metal Sensor with the ESP32 provides a rugged, non-contact solution for proximity detection. By mastering the principles of induction and implementing robust interrupt-driven firmware, you can build everything from industrial counters to advanced security systems. In the world of sensors, the metal detector remains a definitive tool for bridging the physical properties of matter with digital IoT logic.