I have a garage door opener which has a operating range of about 10 feet. It uses RF signals to trigger the gate, and it poses a couple of problems.
- Range is very limited
- You have to wait and verify that the door closes before you leave.
- Sometimes you are unsure if you even closed the door.
So, i decided to get a new app controlled door opener. That was until i checked the prices. Its 2000nok for a commercial one, with 1 door sensor. To me that sounds like robbery, so i decided to make one myself.
I had one of the first Raspberry PI lying around, so that was to be my base.
I ordered a couple of things from AliExpress :
- A relay with 5v on the switching side, and up to 220v on the hot side.
- A ultrasonic sonar
- Wifi dongle
- 1k and 470ohm resistors
- some cable
All links to these products can be found on the bottom of this page.
So, lets look at the hardware side first.
The ultrasonic sensor outputs 5v on the echo pin, so this needs to be taken down to around 3.3v, so we don’t blow out the RPI. This is done using a 470k and 1Ohm resistor, connected as shown in the diagram. The 470 connects to the echo wire, and the 1Ohm goes between the GND and echo wire.
The relay will be connected to the garage door opener motor, which has inputs for connecting a physical opener inside the garage. The button just closes a loop, and this is what the relay will be doing aswell. When RPI receives a signal it will send a GPIO command switching the relay on and off. This will close the wire loop, and open/close the door.
Besides having to drop the voltage alittle, the rest should be fairly easy. The diagram also described which GPIO pins where used.
The RPI as loaded with the latest Raspbian, and booted.
If you plan on just using ssh and not connecting a screen for setup, add a empty file called “ssh” to the boot sector. This activates the ssh-daemon.
The wifi dongle was detected nicely, and the board was connected to the wifi network.
The software side is currently running python scripts for the GPIO communication, and PHP/MQTT for connecting it to the real world. I will not go into details on the PHP/MQTT part, but should there be any questions please ask.
The python scripts are fairly straight forward. I had some problems making the relay work, sending hi and low commands, so i ended up just switching modes on it, which works just fine for me.
So, here are the code for the relay :
import RPi.GPIO as GPIO
#pin = int(sys.argv)
pin = 18
Sonar code :
import RPi.GPIO as GPIO #Import GPIO library
import time #Import time library
TRIG = 16 #int(sys.argv) #Associate pin 23 to TRIG
ECHO = 15 #int(sys.argv) #Associate pin 24 to ECHO
time.sleep(0.00001) #Delay of 0.00001 seconds
while GPIO.input(ECHO)==0: #Check whether the ECHO is LOW
pulse_start = time.time() #Saves the last known time of LOW pulse
pulse_end = time.time()
pulse_duration = pulse_end – pulse_start #Get pulse duration
distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance
distance = round(distance, 2) #Round
if distance > 2 and distance < 400: #Check whether the distance is within range
This was all packaged into a small IP65 housing ( which i had to modify to fit the 1.gen RPI ). Had i gone with a 2. gen or newer with a microsd i would’nt had to modify the case any. Since its sitting in a dry place i did’nt care to much.
After using the opener for a few days i’m very satisfied with how it works. Its been rock stable, doing what its made for. I will do a writeup on how the opener is using MQTT to communicate with the real world.
Next IOT project in line is making a self sustained temperature and humidity monitor with a tiny footprint. It will be based on the small arduino compatible board called ESP8266.
Ultrasonic sensor : https://www.aliexpress.com/item/Free-shipping-Ultrasonic-Module-HC-SR04-Distance-Measuring-Transducer-Sensor-for-arduino-Samples-Best-prices/1941377431.html?spm=2114.13010608.0.0.8wZdTT