Refrigerator Door Sensor
Many older refrigerators have hinges that are worn and may prevent them from fully closing. To combat this issue, I designed an Arduino based distance measurement system to detected if the door was left open. It uses a HC-SR04 ultrasonic sensor mounted on the side of the fridge and a small plastic popup as an area to reflect the sound waves emitted by the ultrasonic sensor.
The assembly uses an Arduino-based board as a controller. A 3V buzzer is used to alert the user when the fridge has been open for too long. The ultrasonic sensor measures the distance from the popup to determine if the fridge is open. If the fridge is slightly open, it will beep after 10 seconds. If the fridge is wide open, it will beep after 3 minutes. Additionally, the builtin LED on the board is illuminated when the fridge is open. This is useful when adjusting the distance variables for the "slightly open" state and the "fully open" state.
#define DELAY 180 // Delay before buzzer activation (seconds)
#define DELAY_SHORT 10 // Delay before buzzer activation when the door is barely open (seconds)
#define OPEN_THR 10.8 // Distance for door to be detected as "open"
#define B_OPEN_THR 4// Distance for door to be detected as "barely open"
#define BUZZER 9 // Buzzer pin
#define TRIG 3 // Sensor TRIGGER pin
#define ECHO 2 // Sensor ECHO pin
#define POWER 4 // Pin used as power bus
#define SOUND_SPEED 0.034 // Speed of sound in cm/μs
float distanceCm; // Variable to store distance in cm
void setup() {
Serial.begin(9600); // Initialize serial monitor for debugging
// Initialize pins
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(BUZZER, OUTPUT);
pinMode(POWER, OUTPUT);
digitalWrite(POWER, HIGH); // Pin 4 is being used as a power bus for easy mounting of the ultrasonic censor
}
void loop() {
distanceCm = measDist(); // Update distance variable
if (distanceCm > OPEN_THR) { // Check if the door is wide open
Serial.println("Open"); // Send info to serial monitor for debugging
digitalWrite(LED_BUILTIN, HIGH); // Turn on builtin LED for debugging
for (unsigned int i = 0; i <= DELAY; i++) { // Wait for 180 (DELAY variable) seconds
distanceCm = measDist(); // Update distance variable
if (!(distanceCm > OPEN_THR)) { // If door has been closed, cancel timer
return;
}
delay(1000);
}
pulse(3000); // Activate buzzer @ 3000hz
} else if (distanceCm < OPEN_THR && distanceCm > B_OPEN_THR) { // Check if the door is barely open
Serial.println("Barely open"); // Send info to serial monitor for debugging
digitalWrite(LED_BUILTIN, HIGH); // Turn on builtin LED for debugging
for (unsigned int i = 0; i <= DELAY_SHORT; i++) { // Wait for 10 (DELAY_SHORT variable) seconds
distanceCm = measDist(); // Update distance variable
if (!(distanceCm < OPEN_THR && distanceCm > B_OPEN_THR)) // If door has been closed, cancel timer
return;
delay(1000);
}
pulse(4000); // Activate buzzer @ 4000hz
} else {
digitalWrite(LED_BUILTIN, LOW); // Turn off builtin LED when door is not open.
}
Serial.print("Distance (cm): "); // Print distance in cm for easy configuration of "open", "barely open" variables.
Serial.println(distanceCm);
delay(500); // Cycle time
}
void pulse(const unsigned int FREQ) {
for (unsigned int i = 0; i < 3; i++) { // Activate buzzer 3 times for 500ms.
tone(BUZZER, FREQ, 500); // Send signal to BUZZER PIN at FREQ for 500ms
delay(1000);
}
}
float measDist() { // Measures distance
static long duration;
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH); // Measure how long it takes an emitted ultrasonic pulse to return
return duration * SOUND_SPEED / 2;
delay(20);
}