Advanced Garage Door Controls Part 2
So this is part 2 for my advanced garage door controls. In this part I am making it so that the garage door can autoclose, there were some minor changes to the design when it comes to the features as well as the implementation. First off I had to change over to an arduino mega because there was not quite enough space on the arduinio uno to hold the program data (I was about 5kb off from fitting). As a reminder here are the features that it will include.
- DONE: A status indicator inside the house to indicate if the garage door is open or closed.
- DONE: The ability to make the garage door automatically close after being open for so long.
- DONE: The ability to have the garage door automatically close if the garage gets too cold or too warm.
- The ability to remotely check if the garage door is open or closed
, see if the safety laser beam is blocked,and remotely open or close it.
And here is a part list and a rough cost for this project (besides the wire if you are going to wire up an LED in the house and a breadboard):
- Arduino Mega 2560 – Roughly $65
- Ethernet Shield (with microsd card slot) – Roughly $45
- Ping Ultrasonic Sensor – Roughly $30
- TMP36 Temperature Sensor – Roughly $2
- NPN Transistor (I used a TIP120) – Roughly $3 at radioshack
And below is the final schematic (in pictoral form) of how my project is setup in order for you to get an idea:
As you can see the temperature sensor is analog, which means that it will be done slightly different than reading a digital signal, however we are lucky enough that the TMP36 give great info on how it works and how simple it is to read the temperature. I would read the temp every time through the loop in order to see if it was either too warm or too cold and we need to autoclose the garage door. Put simply my code reads the temperature sensor, then using the formula given by adafruit I computed the temperature in celsius, then I converted it to farenheit as that is what I wanted, I likely could have done this in one step but wanted to make sure it was easy to read for debugging purposes. Below is the code that I used to read the temperature from the sensor.
/**
* get's the current temperature of the garage
* note that tempSensorPin is defined in my global variables as a constant which has a value of A0
* besides the conversion to farenhiet, the code below is based off of sample code from adafruit for
* using this sensor
*/
float getTemp(){
int reading = analogRead(tempSensorPin);
float volt = reading*5.0;
volt /= 1024;
float tempC = (volt - .5)*100;
return ((tempC*9.0/5.0)+32); //convert to ferinheit
}
Ok now that we are able to read the code from the temperature sensor let’s make it so we can open and close the door because the code for that is quite simple, and will be used in multiple other areas of the code that is written. If you don’t know about transistors yet I recommend that you read up on them, however I will explain what I am using one for in this case. I am connecting the ground of the garage door switch (and arduino) to the drain of the transistor. I hook the power of the garage door to the source, and pin 5 up to the gate which will act as a switch. When pin 5 has power, then the circuit for the garage door’s switch will be closed causing it to either open or close the garage door (depending on how it is at that time). That seems simple enough, so I simply made a method to do it as I know I will be doing it from a couple of sections of code. Below is that simple method, though I’m pretty sure you can figure it out, so I hit it as a spoiler, check it out if you are stuck.
Those of you who know about transistors know they get hot when used for long periods of time and so should have a heatsink, however in our application most of the time it will not be in use so it should be safe without a heatsink, however I have a feeling if I don’t mention this someone will ask about why I don’t mention a heatsink.
Now that we can open and close our garage door from a simple function call as well as check the temperature, it seems to be the perfect time to update the code we had to sense the garage door being open or close. First off we have to remove the 10 second delay, we will take care of the delay within the loop itself (which will be much shorter). The reason we will want a shorter delay is that we will be serving up websites from our arduino, so if we have a 10 second delay during a worse case scinario you will have to wait 10 seconds to even see if your garage door is open or closed on the site, and another 10 seconds after you hit the button to open or close it. I set the delay on my loop for 200 milliseconds, so it should be going through the loop around 5 times per seconds. You will also want to add a few new global variables, I have them listed below with comments on what they do (and they are assigned the values I gave them, yours don’t necessarily have to be the same).
- boolean isOpen = false; //if the door is currently open based off of the latest check
- boolean wasOpen = false; //if the door was open the check before the latest
- unsigned long openTime = 0; //the time that the door was opened according to the millis command
- unsigned long closeTime = 0; //the time that we want to close the door according to the openTime and the autoCloseTime
- unsigned long autoCloseTime = 7200000; //2 hours in miliseconds
- int lowCloseTemp = 0; //the low threshold where we will close the garage door automatically
- int highCloseTemp = 100; //the high threshold where we will close the garage door automatically
- boolean AutoCloseEnabled = true; //if we want to autoclose
If you decide to make the autoclose time or the temperatures to close at not changeable (via a config page for example), then you will want to make them const’s as the compiler can optimize better knowing that the values will not change. So the way that the autoclose works for the timer based autoclose, is that we will record when we opened the door based off of the internal clock of the adruino, and when the closeTime<millis() (the current time), then we know it’s time to close so we call changeDoorState() and set wasOpen to false to prevent it from trying to open and close over and over. The temperature sensor is similar, it just checks the current temp every time through the loop and if it is above the max temperature or below the minimum temperature then we will close it. I have my variables being configurable over the web (although it is currently bugged), so that is why mine are not const. Below is my code slightly modified from the previous tutorial to enable autoclosing. If you would like to try it yourself then don’t reveal the spoiler until you tried.
That’s it for this part, in part three I will walk you through setting up the ability to remotely open it using a web browser. To do this I will be walking you through setting up the webduino and giving you a sample of the code I used (not all of it, because it is not all necessary). If you have any questions feel free to ask them in a comment below, and if you would like to see all my code for the final product (though it’s still changing slightly to fix bugs), feel free to check it out on my github.

