Ultrasonic Lock

Ultrasonic Lock © GPL3+

Anything can be used as a key as long as you know the password.

  • 4,963 views
  • 2 comments
  • 8 respects

Components and supplies

Apps and online services

About this project

A fun way to make a lock. You set a password initially and the ultrasonic sensor every 3 seconds measure a distance (between the sensor and an object/card) and it compares the password set with the one you enter. If the password is valid, the lock will remain unlocked as far as the distance measured is greater than 5 cm hence, you can lock the box whenever you want.

Schematic:

How it works:

The ultrasonic Sensor makes make a measurement every 3 seconds. That measurement corresponds to a single element of the password. The password is given in the form of an Array("pass"). Also, I created another Array("checkPass") with all of its elements equal to 0. The measurements taken by the ultrasonic Sensor replace the 0s in the "checkPass" Array. Lastly, the 2 Arrays are compared and if the values of the Array "checkpass" are equal and in the same order as the "pass" Array, the Servo rotates and unlocks the Box.

Then to lock again the Box you just need to bring an object/card close to the ultrasonic Sensor (less than 5 cm) and the Servo will move back to its initial position.

Code

Ultrasonic Lock-CodeC/C++
#include<Servo.h>
#include<LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);
Servo myServo;

const int trigPin = 6;
const int echoPin = 7;
const int sound = 340;

float duration;
float distance;
float distanceLoop;
int pass[] = {8,7,6,5};//password numbers
int checkPass[5];//array with 4 values(initial every elemnet is equal with 0)
int number;
int count = 0;
int sizeArray;
float distanceCheck;// to check when it is ready to close box

void setup(){
  Serial.begin(9600);
  lcd.begin(16,2);
  myServo.attach(9);
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
  sizeArray = sizeof(pass)/2;//size of array
  int checkPass[5];
  lcd.print("Password:");
  myServo.write(120);
  delay(1000);
}

void loop(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Password:");
  lcd.setCursor(1,0);
  for(int num = 0; num <= sizeArray ; num ++){//takes enough measurements(password keys) as the number of elements of password
    for(int i = 3; i > 1 ;i--){//countdown before a measurement(password key) is taken
      lcd.setCursor(15,0);
      lcd.print(i);
      lcd.setCursor(12,2);
      lcd.print(Distance());
      delay(1000);
    }
    distanceLoop = Distance();//measurement is taken
    number = check(distanceLoop,pass[num]);//rounds up number
    checkPass[num] = number;//assigns measurement to the array
    lcd.setCursor(num + 1,2);
    lcd.print(number);
  }
  for(int i = 0; i < 4 ; i ++){
    Serial.println(checkPass[i]);
  }
  count = checkCount(0,checkPass,pass,3);/*compares 2 arrays to check if the password is correct
                                         for every wrong value count variable increase by one*/
  if(count != 0){
    lcd.clear();
    lcd.print("Wrong Password");
    myServo.write(120);
    delay(5000);
  }
  else{
    lcd.clear();
    lcd.print("Valid Password");
    myServo.write(30);
    distanceCheck = Distance();
    while(distanceCheck > 5){
      distanceCheck = Distance();
      myServo.write(30);
      delay(500);
    }
    myServo.write(120);
    delay(1000);
  }
}

float Distance(){//measures distance in cm
  digitalWrite(trigPin,LOW);
  delayMicroseconds(15);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(2000);
  digitalWrite(trigPin,LOW);
  delayMicroseconds(10);
  duration = pulseIn(echoPin,HIGH);
  distance = sound * duration/20000;
  return distance;
}

int check(float msr,float nUm){//rounds up number
  if(msr >= nUm - 0.5 && msr <= nUm + 0.5){
    msr = nUm; 
    return msr; 
  }
  else{
    return (int) msr;
    
  }
}

int checkCount(int cnt,int checkPass[],int pass[],int times){//check password
  for(int num = 0; num <= times ; num ++){
    if(checkPass[num] == pass[num]){
      cnt = cnt;
    }
    else{
      cnt += 1;
    }
  }  
  return cnt;
}

Schematics

Ultrasonic Lock-Schematic
untitled_sketch_2_JrcTSPFANm.fzz

Comments

Similar projects you might like

Auto Ultrasonic Car

by Ishaq Yang

  • 22,672 views
  • 2 comments
  • 19 respects

Ultrasonic Sensor with Alarm, LCD and Temperature

Project tutorial by MichDragstar

  • 10,142 views
  • 7 comments
  • 24 respects

Gesture-Controlled Lock

Project tutorial by Arduino “having11” Guy

  • 11,609 views
  • 9 comments
  • 38 respects

Arduino Controlled Lock Box with Solenoid and RFID

Project tutorial by RobotGeek Projects Team

  • 7,861 views
  • 3 comments
  • 23 respects

Arduino Keyless Door Lock System with Keypad and LCD

Project tutorial by DIY Hacking

  • 32,441 views
  • 19 comments
  • 59 respects
Add projectSign up / Login