Example Code for Arduino-Metal Detection

Introduction

This tutorial will guide you through quickly using this product on Uno R3 and observing the results.

Hardware Preparation

Software Preparation

Wiring Diagram

FIT0658-Connection Diagram

5V Metal Proximity Switch Sensor:VCC(Brown)---Connect---UNO R3:5V
5V Metal Proximity Switch Sensor:GND(Blue)---Connect---UNO R3:GND
5V Metal Proximity Switch Sensor:VCC(Black)---Connect---UNO R3:Digital pin 4

Sample Code

const int SensorPin = 4;
const int LedPin = 13;

void setup()
{
  Serial.begin(9600);
  Serial.println("Start!");  
  pinMode(SensorPin,INPUT);
  pinMode(LedPin,OUTPUT);
  digitalWrite(LedPin,LOW);
}

void loop()
{  
  if(digitalRead(SensorPin) == LOW)              
  digitalWrite(LedPin,LOW);   
  else
  digitalWrite(LedPin,HIGH);
  Serial.print("Infrared Switch Status:");
  Serial.println(digitalRead(SensorPin),BIN);
  delay(50);
}

Result

In the Arduino serial port monitor, you can see the change of the switch state when the sensor detects the metal object, when the metal object is not detected, the status is 1, and it is 0 when detected; you can also observe whether the metal object is detected by the state of indicator on UNO board or the light on the sensor, and the indicator light is on when the metal object is detected, otherwise, off.

Expected Result

Additional Information

Note:

  • The effective detection distance is in 4mm, and the diameter of the detected object should be greater than or equal to 1mm.

  • The object shall be placed in the detection area, otherwise, it may not work.

Was this article helpful?

TOP