Tomas Rigaux
Published © GPL3+

Door Alarm with Display

Simple door alarm using ultrasonic sensor and 16x2 LCD display.

BeginnerShowcase (no instructions)2 hours871
Door Alarm with Display

Things used in this project

Hardware components

Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
SparkFun 2 pin Screw Terminal
×1
SparkFun Button
×1
Resistor 100 ohm
Resistor 100 ohm
This for the speaker.
×1
Resistor 221 ohm
Resistor 221 ohm
*220 ohm
×1
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

DoorAlarm

Code

DoorAlarm

Arduino
This is the code for the door alarm. It gets input from the button and ultrasonic sensor, and displays a message to the LCD based on the current distance between the sensor and the door.
#include <LiquidCrystal.h>
#include <pitches.h>

// Set up the display pins.
const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

/*** Setup our door alarm variables. ***/
// The pins connected to thr ultrasonic sensor.
int echoPin = 4;
int trigPin = 5;

// The pin connected to our push button.
int buttonPin = 2;

// The pin the speaker is attached to.
int speakerPin = 7;

// A distance reading has to change more or less than the recorded distance and the tolerance.
float distToDoor_Tolerance = 10;

// The distance to our door in cm -1 means it has not yet been set.
float distToDoor = -1;

// The notes in our melody and their duration in fractions of a second. Exampls: quarter note = 4, eigth note = 8, etc.
const int melody[][2] = 
{
  // These notes can be changed based on user preference.
  {NOTE_D7, 4},
  {NOTE_A6, 4},
  {NOTE_B0, 4}
};
/***************************************/

void play_alarm()
{
  // Figure out the number of notes in our melody.
  int numberOfNotes = sizeof(melody) / sizeof(melody[0]);

  // Iterate over the notes of the melody.
  for(int i = 0; i < numberOfNotes; i++)
  {
    // Grab our note and note duration.
    int thisNoteTone = melody[i][0];
    int thisNoteDuration = melody[i][1];

    // To calculate the note duration in milliseconds.
    int noteDurationMS = 1000 / thisNoteDuration;

    // Play the note.
    tone(speakerPin, thisNoteTone, noteDurationMS);

    // Distinguish the notes, set a minimum time between them. The notes duration plus 30% seems to work well.
    delay(noteDurationMS * 1.3);
  }
}

float get_distance_cm()
{
  float distance_cm;
  int pulseLenMS;

  // Bit-bang a small square wave on the trig pin to start the wave finder.
  digitalWrite(trigPin, LOW);
  delayMicroseconds(20);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigPin, LOW);

  // Measure the pulse length from the echo pin.
  pulseLenMS = pulseIn(echoPin, HIGH);

  // Calculate the distnce using the speed of sound.
  distance_cm = pulseLenMS / 29.387 / 2;

  return distance_cm;
}

void setup() {
  // Put your setup code here, to run once:
  // Set the pin mode on your ultrasonic echo and trig pins.
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);

  lcd.clear();
      
  // Set the pin mode for our button to input with pull up.
  pinMode(buttonPin, INPUT_PULLUP);

  lcd.begin(16,2);
}

void loop() {
  // Put your main code here, to run repeatedly:
  int button;
  float distance;


  // Read the ultrasonic sensor.
  distance = get_distance_cm();

  // if the distance is greater or less than our preset alarm distance, and we have set a distance, sound the alarm.
  if(distance > distToDoor + distToDoor_Tolerance || distance < distToDoor - distToDoor_Tolerance)
  {
    if(distToDoor != -1){
      play_alarm();
      // Print to the LCD screen.
      lcd.clear();
      lcd.print("Door is open!");
      delay(100);
    }
    else{
      // Print to the LCD screen.
      lcd.clear();
      lcd.print("Door is closed.");
      delay(100);
    }
  }
  else
  {
    // Print to the LCD screen.
    lcd.clear();
    lcd.print("Door is closed.");
    delay(100);
  }

  // Read our button state.
  button = digitalRead(buttonPin);

  // if the button is pressed, set the distance to the last distance read.
  if(button == LOW)
  {
    distToDoor = distance;
  } 
}

Credits

Tomas Rigaux
1 project • 0 followers
Quantum in me fuit.
Contact

Comments

Please log in or sign up to comment.