Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
shashi shekhar
Published © MIT

Metal Detector Alert Monitoring System

This Bolt IoT-based project shows how to build a metal detector alert monitor.

IntermediateFull instructions provided5 hours13,804
Metal Detector Alert Monitoring System

Things used in this project

Hardware components

Proximity Sensor
Proximity Sensor
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Capacitor 47 µF
Capacitor 47 µF
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Resistor 100 ohm
Resistor 100 ohm
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Buzzer
Buzzer
×1
Multi-Turn Precision Potentiometer- 10k ohms (25 Turn)
Multi-Turn Precision Potentiometer- 10k ohms (25 Turn)
×1
Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Arduino UNO
Arduino UNO
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Bolt IoT Android App
Bolt IoT Android App

Story

Read more

Schematics

circuit diagram of the metal detector

schematic diagram of the project

Code

arduino code

Arduino
#define capPin A0
#define buz 9
#define pulsePin A4

#define led 10

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);
}

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);
    }
}

blueprint code

JSON
{
    "name": "Metal Detection",
    "flow": [
        {
            "id": 15,
            "module": "app#bolt-iot:executeCommand",
            "version": 1,
            "parameters": {
                "__IMTCONN__": 445281
            },
            "mapper": {
                "command": "serialBegin",
                "parameters": {
                    "baud": "9600",
                    "deviceName": ""
                }
            },
            "metadata": {
                "designer": {
                    "x": 8,
                    "y": -3
                },
                "restore": {
                    "command": {
                        "label": "Initialize serial communication"
                    },
                    "__IMTCONN__": {
                        "label": "My Bolt connection"
                    }
                },
                "parameters": [
                    {
                        "name": "__IMTCONN__",
                        "type": "account",
                        "label": "Connection",
                        "required": true
                    }
                ],
                "expect": [
                    {
                        "name": "command",
                        "type": "select",
                        "label": "Command",
                        "required": true,
                        "validate": {
                            "enum": [
                                "digitalWrite",
                                "digitalRead",
                                "analogRead",
                                "analogWrite",
                                "serialBegin",
                                "serialWrite",
                                "serialRead",
                                "version",
                                "restart",
                                "isAlive"
                            ]
                        }
                    },
                    {
                        "name": "parameters",
                        "spec": [
                            {
                                "name": "deviceName",
                                "type": "text",
                                "label": "Device name",
                                "required": true
                            },
                            {
                                "name": "baud",
                                "type": "number",
                                "label": "Baud",
                                "required": true
                            }
                        ],
                        "type": "collection",
                        "label": "Parameters"
                    }
                ]
            }
        },
        {
            "id": 1,
            "module": "app#bolt-iot:executeCommand",
            "version": 1,
            "parameters": {
                "__IMTCONN__": 445281
            },
            "mapper": {
                "command": "serialWrite",
                "parameters": {
                    "data": "Get detected",
                    "deviceName": ""
                }
            },
            "metadata": {
                "designer": {
                    "x": 300,
                    "y": 0
                },
                "restore": {
                    "command": {
                        "label": "Send serial data output"
                    },
                    "__IMTCONN__": {
                        "label": "My Bolt connection"
                    }
                },
                "parameters": [
                    {
                        "name": "__IMTCONN__",
                        "type": "account",
                        "label": "Connection",
                        "required": true
                    }
                ],
                "expect": [
                    {
                        "name": "command",
                        "type": "select",
                        "label": "Command",
                        "required": true,
                        "validate": {
                            "enum": [
                                "digitalWrite",
                                "digitalRead",
                                "analogRead",
                                "analogWrite",
                                "serialBegin",
                                "serialWrite",
                                "serialRead",
                                "version",
                                "restart",
                                "isAlive"
                            ]
                        }
                    },
                    {
                        "name": "parameters",
                        "spec": [
                            {
                                "name": "deviceName",
                                "type": "text",
                                "label": "Device name",
                                "required": true
                            },
                            {
                                "name": "data",
                                "type": "text",
                                "label": "Data",
                                "required": true
                            }
                        ],
                        "type": "collection",
                        "label": "Parameters"
                    }
                ]
            }
        },
        {
            "id": 14,
            "module": "app#bolt-iot:executeCommand",
            "version": 1,
            "parameters": {
                "__IMTCONN__": 445281
            },
            "mapper": {
                "command": "serialRead",
                "parameters": {
                    "till": "10",
                    "deviceName": ""
                }
            },
            "metadata": {
                "designer": {
                    "x": 600,
                    "y": 0
                },
                "restore": {
                    "command": {
                        "label": "Read incoming serial data"
                    },
                    "__IMTCONN__": {
                        "label": "My Bolt connection"
                    }
                },
                "parameters": [
                    {
                        "name": "__IMTCONN__",
                        "type": "account",
                        "label": "Connection",
                        "required": true
                    }
                ],
                "expect": [
                    {
                        "name": "command",
                        "type": "select",
                        "label": "Command",
                        "required": true,
                        "validate": {
                            "enum": [
                                "digitalWrite",
                                "digitalRead",
                                "analogRead",
                                "analogWrite",
                                "serialBegin",
                                "serialWrite",
                                "serialRead",
                                "version",
                                "restart",
                                "isAlive"
                            ]
                        }
                    },
                    {
                        "name": "parameters",
                        "spec": [
                            {
                                "name": "deviceName",
                                "type": "text",
                                "label": "Device name",
                                "required": true
                            },
                            {
                                "name": "till",
                                "type": "number",
                                "label": "Till",
                                "required": true,
                                "validate": {
                                    "max": 127,
                                    "min": 0
                                }
                            }
                        ],
                        "type": "collection",
                        "label": "Parameters"
                    }
                ]
            }
        },
        {
            "id": 18,
            "module": "math:EvaluateExpression",
            "version": 1,
            "parameters": {},
            "mapper": {
                "expression": "100-{{parseNumber(14.value)}}"
            },
            "metadata": {
                "designer": {
                    "x": 869,
                    "y": -2
                },
                "expect": [
                    {
                        "name": "expression",
                        "type": "text",
                        "label": "Expression",
                        "required": true
                    }
                ]
            }
        },
        {
            "id": 13,
            "module": "google-email:ActionSendEmail",
            "version": 1,
            "parameters": {
                "account": 445282
            },
            "filter": {
                "name": "Check if metal is detected",
                "conditions": [
                    [
                        {
                            "a": "{{18.result}}",
                            "b": "30",
                            "o": "number:greater"
                        }
                    ]
                ]
            },
            "mapper": {
                "cc": [],
                "to": [
                ],
                "bcc": [],
                "html": "metal detection",
                "subject": "Warning! metal detected",
                "attachments": []
            },
            "metadata": {
                "designer": {
                    "x": 1145,
                    "y": -3
                },
                "restore": {
                    "cc": {
                        "mode": "chose",
                        "items": []
                    },
                    "to": {
                        "mode": "chose",
                        "items": [
                            "undefined"
                        ]
                    },
                    "bcc": {
                        "mode": "chose",
                        "items": []
                    },
                    "account": {
                        "label": ""
                    },
                    "attachments": {
                        "mode": "chose",
                        "items": []
                    }
                },
                "parameters": [
                    {
                        "name": "account",
                        "type": "account",
                        "label": "Connection",
                        "required": true
                    }
                ],
                "expect": [
                    {
                        "name": "to",
                        "spec": {
                            "name": " ",
                            "type": "email",
                            "label": "Email address",
                            "required": true
                        },
                        "type": "array",
                        "label": "To",
                        "labels": {
                            "add": "Add a recipient",
                            "edit": "Edit a recipient"
                        },
                        "required": true
                    },
                    {
                        "name": "subject",
                        "type": "text",
                        "label": "Subject"
                    },
                    {
                        "name": "html",
                        "type": "text",
                        "label": "Content"
                    },
                    {
                        "name": "attachments",
                        "spec": [
                            {
                                "name": "fileName",
                                "type": "filename",
                                "label": "File name",
                                "required": true,
                                "semantic": "file:name"
                            },
                            {
                                "name": "data",
                                "type": "buffer",
                                "label": "Data",
                                "required": true,
                                "semantic": "file:data"
                            },
                            {
                                "name": "cid",
                                "type": "text",
                                "label": "Content-ID"
                            }
                        ],
                        "type": "array",
                        "label": "Attachments",
                        "labels": {
                            "add": "Add an attachment",
                            "edit": "Edit an attachment"
                        }
                    },
                    {
                        "name": "cc",
                        "spec": {
                            "type": "email",
                            "label": "Email address"
                        },
                        "type": "array",
                        "label": "Copy recipient",
                        "labels": {
                            "add": "Add a copy recipient",
                            "edit": "Edit a copy recipient"
                        }
                    },
                    {
                        "name": "bcc",
                        "spec": {
                            "type": "email",
                            "label": "Email address"
                        },
                        "type": "array",
                        "label": "Blind copy recipient",
                        "labels": {
                            "add": "Add a blind copy recipient",
                            "edit": "Edit a blind copy recipient"
                        }
                    }
                ]
            }
        }
    ],
    "metadata": {
        "version": 1,
        "scenario": {
            "roundtrips": 1,
            "maxErrors": 3,
            "autoCommit": false,
            "sequential": false,
            "confidential": false,
            "dataloss": false,
            "dlq": false
        },
        "designer": {
            "orphans": []
        },
        "zone": "eu1.integromat.com"
    }
}

Credits

shashi shekhar
2 projects • 8 followers
i am a student at graphic era deemed to be university and i have just started this by introducing myself in the robotics world

Comments