My fridge door tends to rebound when closed staying open a smidgen and letting all the cold out. Rather than just checking that the door is properly shut, I thought it about time to have a microcontroller make a noise when the door has been left open too long.

This circuit uses an Arduino compatible Teensy LC for all the work, it has a phototransistor to sense the state of the fridge door light, a couple of LED’s one to indicate the power and another that comes on when the door is open. There is also Piezo buzzer to make an annoying noise after forty five seconds of door open time. The unit runs of a 3.7v rechargeable Lithium-ion battery and I have added a recharging circuit that takes power via the Teensy’s 5v USB port.
Circuitry
Note: These diagrams show 3.7v as the supply voltage. The Teensy LC can only tolerate a maximum 3.3V on the data pins, so these circuits are driven from the 3V output on the Teensy. They will all work without modification on the 5v Arduino Uno.
I have used a phototransistor to detect the fridge door light, there are two variants of this circuit light activated or dark activated, the 100k resistor can be replaced with a 100k variable if you need to adjust the sensitivity, the 330k resistor provides a weak pull-down on the output. The phototransistor is being used in switch mode to provide a logical output (rather than active mode which provides an output relative to the amount of light), so the output is connected to a digital input on the Arduino. The BC547 transistor is half of the darlington pair to provide extra gain on the output.


I chose the light activated switch, either will do but will provide different logical outputs to your controller. The circuit is enclosed in a small box inside the fridge and connected by ribbon cable to the controller, the ribbon cable is flat and does not upset the magnetic ‘seal’ on the fridge door.

To make some noise I used a piezo buzzer from an old computer, this is driven through a transistor as the Teensy does not provide enough current to drive it directly.

There is also a push button to provide a reset function if the buzzer is sounding while the door is open.

I have also added two LED’s, one to show power and anther that illuminates when the door is opened.

The final circuit if for recharging the battery, it connects to the 5V connection on the Teensy LC so charges the battery when the USB connection is in use. This has been copied from the MCP3831T datasheet.

Software
This uses an interrupt to listen for the light sensor, when the state changes, the door open pin is read to determine if the door is open or not. If it is then a timer is started, this gives you forty five seconds to complete your task before the alarm sounds. With the door closed the timer is stopped and set back to zero. If the sounder goes off while you are rummaging in the fridge the reset button can be pressed, this restarts the timer from zero again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
#define TEST_LED 13 #define FRIDGE_DOOR 6 #define DOOR_LED 8 #define POWER_LED 7 #define RESET_BTN 9 #define BUZZER 10 volatile boolean doorOpen = true; // when the light is on volatile boolean doorTrigger = false; volatile unsigned long lastFridgeMillis = 0; volatile unsigned long fridgeTimeoutMillis = 10; boolean SerialDebug = false; unsigned long doorOpenDelay = 45000; // 45 second timeout before buzzer sounds unsigned long doorOpenTime = 0; unsigned long startTime; unsigned long previousToneMillis = 0; unsigned long toneInterval = 20; boolean soundTheAlarm = false; boolean outputTone = false; int toneMelody[5] = { 1600, 1900, 2000, 2100, 1800 }; const int toneMax = 5; unsigned long lastDoorOpenTime = 1; char pout[80]; // for testing // interrupt function void fridgeDoor() { if ((millis() - lastFridgeMillis) > fridgeTimeoutMillis) { doorOpen = digitalRead(FRIDGE_DOOR); // goes high when dark lastFridgeMillis = millis(); } } // see if the door is open or closed void fridgeDoorOpen() { if (doorOpen == true && doorTrigger == false) { doorTrigger = true; startTime = millis(); if (SerialDebug) { sprintf(pout, "door open: %d", doorOpen); Serial.println(pout); } digitalWrite(DOOR_LED, HIGH); return; } if (doorOpen == false && doorTrigger == true) { doorTrigger = false; if (SerialDebug) { sprintf(pout, "door closed: %d", doorOpen); Serial.println(pout); } startTime = 0; doorOpenTime = 0; soundTheAlarm = false; digitalWrite(DOOR_LED, LOW); return; } } // make some noise void soundAlarm() { if (soundTheAlarm == false) { return; } int ii = 0; while (ii < 6) { if (soundTheAlarm == false) { break; } int i = 0; while (i < toneMax) { if (soundTheAlarm == false) { break; } unsigned long currentMillis = millis(); if((currentMillis - previousToneMillis) > 50) { previousToneMillis = currentMillis; tone(BUZZER,toneMelody[i]-380,60); i++; } } ii++; } } // door open? then increment the timer - sound the alarm void fridgeDoorOpenTimer() { if (doorOpen == false && doorTrigger == false) { return; } if (digitalRead(RESET_BTN) == HIGH) { digitalWrite(POWER_LED, LOW); while (digitalRead(RESET_BTN) == HIGH) { delay(10); } if (SerialDebug) { Serial.println("reset"); } startTime = millis(); soundTheAlarm = false; digitalWrite(POWER_LED, HIGH); } // checking once a second doorOpenTime = millis() - startTime; if (doorOpenTime % 1000 == 0 && doorOpenTime != lastDoorOpenTime) { if (SerialDebug) { Serial.println(doorOpenTime); } if (doorOpenTime > doorOpenDelay) { if (SerialDebug) { Serial.println("Awwooga Awwooga - fridge door open!"); } soundTheAlarm = true; soundAlarm(); } lastDoorOpenTime = doorOpenTime; } } void setup() { pinMode(TEST_LED, OUTPUT); digitalWrite(TEST_LED, HIGH); if (SerialDebug) { Serial.begin(9600); } delay(2000); pinMode(DOOR_LED, OUTPUT); pinMode(POWER_LED, OUTPUT); pinMode(BUZZER, OUTPUT); if (SerialDebug) { Serial.println("ready"); } digitalWrite(POWER_LED, HIGH); pinMode(FRIDGE_DOOR, INPUT); attachInterrupt(digitalPinToInterrupt(FRIDGE_DOOR), fridgeDoor, CHANGE); pinMode(RESET_BTN, INPUT); digitalWrite(TEST_LED, LOW); } void loop() { fridgeDoorOpen(); fridgeDoorOpenTimer(); } |
Links and Sources
- Practical Electronics for Inventors, Third Edition. Paul Scherz and Simon Monk, pub. McGraw Hill, 2013. ISBN 978-0-07-177133-7
- A pre-made USB LiPo Battery Charger – 3.7V Single Cell from Hobbytronics or from Sparkfun
- Battery Charger MCP3831T Datasheet
- TEPT4400 Phototransistor Datasheet