Introduction:
This experiment focuses on utilizing a metal detection module with a Raspberry Pi Pico to detect the presence of metal objects. The setup uses both digital and analog outputs to confirm metal detection and provides visual feedback through an LED indicator. This project is ideal for understanding basic sensor interfacing, GPIO usage, and the application of embedded systems in practical scenarios.
Scope:.
The experiment aims to:
Demonstrate the integration of a metal detection module with a Raspberry Pi Pico.
Provide hands-on experience in reading digital and analog sensor outputs.
Develop an understanding of basic microcontroller programming in MicroPython and C.
Implement LED-based feedback for metal detection.
Prerequisite:
3.1. Hardware:
Pico microcontroller
Metal Detection Module
Breadboard
Jumper wires
USB cable (to connect the computer)
3 .2. Software:
Thonny Software(For micro python)
MicroPython firmware installed on Raspberry Pi Pico
ubuntu Terminal(For embedded C)
CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.
Connection Details:
Analog Output: Connect the analog output of the metal detection module to an analog pin (e.g., GP26) on the Pico.
Digital Output: Connect the digital output of the metal detection module to a GPIO pin (e.g., GP18).
LED: Connect the anode of the LED to another GPIO pin (e.g., GP14) and the cathode to ground through a current-limiting resistor.
UART Serial Console: Use a micro-USB cable to connect the Pico to the PC, then open PuTTY and select the correct COM port and baud rate (115200).

Working Principle:

The metal touch sensor emits a signal when the front metal tip of the sensor is touched. This touch detection makes it ideal for applications where a simple and intuitive user interface is required. The sensitivity of the sensor can be adjusted with a controller so that it can respond to different levels of touch. This sensor is ideal for interactive projects, controls and other applications where a touch is required to act as a trigger.
User note
This sensor is ideally suited for threshold value measurement. This means that the sensor emits a digital high signal as soon as a threshold value set by the user on the rotary potentiometer is exceeded. It should be noted here that the digital signal is a comparison of the value set on the rotary potentiometer and the measured value on the sensor, which are then compared by the LM393 comparator installed on the board. The set value of the rotary potentiometer is the threshold value, which is used to define a logical high signal.
Digital output: If the metal tip is touched, a signal is output here
Analog output: Direct measured value of the sensor unit
LED1: Indicates that the sensor is supplied with voltage
LED2: Indicates that the metal tip has been touched
Functionality of the sensor
This sensor has two functional components on its circuit board: The front sensor unit, which physically measures the environment and outputs it as an analog signal to the second unit, the comparator. The comparator compares the measured value of the sensor with the value set on the rotary potentiometer and outputs a logical high signal on the digital pin and LED L1 if the value on the rotary potentiometer is exceeded.
Please note: The signal is inverted. If a high value is measured, this results in a lower voltage value at the analog output.
The rotary potentiometer can be set as follows:

Application code:
MicroPython:
from machine import Pin
import time
# Pin assignments
DIGITAL_PIN = 18 # Digital pin connected to the metal detection module
LED_PIN = 14 # LED pin
# Initialize pins
digital_sensor = Pin(DIGITAL_PIN, Pin.IN) # Set digital pin as input
led = Pin(LED_PIN, Pin.OUT) # Set LED pin as output
while True:
# Read the digital output from the metal detection module
if digital_sensor.value(): # If metal is detected
led.on() # Turn on the LED
print("Metal detected!")
else:
led.off() # Turn off the LED
print("No metal detected.")
time.sleep(0.5) # Delay for a bit before the next reading
Embedded C:
#include <stdio.h>
#include "pico/stdlib.h"
#define DIGITAL_PIN 18 // Digital pin connected to metal detection module
#define LED_PIN 14 // LED pin
void init_gpio() {
gpio_init(DIGITAL_PIN);
gpio_set_dir(DIGITAL_PIN, GPIO_IN); // Set digital pin as input
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT); // Set LED pin as output
}
int main() {
stdio_init_all();
init_gpio();
while (true) {
if (gpio_get(DIGITAL_PIN)) { // If metal is detected
gpio_put(LED_PIN, true); // Turn on the LED
printf("Metal detected!\n");
} else {
gpio_put(LED_PIN, false); // Turn off the LED
printf("No metal detected.\n");
}
sleep_ms(500); // Delay for a bit before the next reading
}
return 0;
}
Hardware Image:

Output:
When the metal detection module senses metal, the LED lights up, and the console outputs "Metal detected!" Otherwise, the LED remains off, and "No metal detected." is displayed.
Video Demonstration:
Appendix:
Common CMakeLists:
cmake_minimum_required(VERSION 3.12)
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)
project(pico_experiments C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()
# Initialize the SDK
pico_sdk_init()
add_compile_options(-Wall
-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that arent called
)
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wno-maybe-uninitialized)
endif()
# Hardware-specific examples in subdirectories:
add_subdirectory(expt_10_LCD)
CMakeLists:
cmake_minimum_required(VERSION 3.13)
# Initialize project
project(metal_detection)
# Include the Raspberry Pi Pico SDK
include(pico_sdk_import.cmake)
# Initialize the Raspberry Pi Pico SDK
pico_sdk_init()
# Create an executable
add_executable(metal_detection main.c)
# Link the Pico SDK libraries to your project
target_link_libraries(metal_detection pico_stdlib)
# Enable USB output, if needed
pico_enable_stdio_usb(metal_detection 1)
# Create map/bin/hex/uf2 files
pico_add_extra_outputs(metal_detection)
References:
| # | Topic | Date & time | Action |
|---|
0% Completed (0/1)