Hi!
I have automated my (very old) gate for which I could not find any kind of existing remote. The gate itself works using a push button that switches the state as follows (arrow indicating a push on the button).
CLOSED -> NEUTRAL -> OPEN -> NEUTRAL -> CLOSED
So that means that normally you would have to push the button twice to get the gate to to open or close. (No idea how common this type of garage motor is).
Hardware used:
- Arduino MKR WiFi 1010
- HL-52S 2 channel relay module
- GP2-1080K distance sensor
The principle is quite simple, the distance sensor is mounted in a way that lets me check if the gate is open, and the relay controls the garage open button. If everything is normal the double blip command will open or close the gate.
Some peculiarities:
- During the boot phase the relay gets engaged once, so that tends to mess things up, hence there is a sequence that sends three more blips to get the status back to "normal".
- The loop that checks the distanceSensor does not always work for some reason, when looking at my dashboard the status of the gate is not always correct. However, the raw value of the sensor always seems to be correct.
Now, the problem I'm experiencing is that the gate will open randomly approximately once every week. I have no clue why this is happening and how I could prevent this from happening in the future.
One theory would be that the system reboots for some reason and that that causes some signals to the relay. However this should be captured by the boot sequence.
Also, one way of "fixing" this would be to check if the state of the gate has changed without a button push. However, this would mean that I can no longer open te gate with the physical push button. So that is not really a good option here.
All tips on how to debug this are very much appreciated!
Below you can find an photo of how this thing is wired, as well as the code. Both of which are so simple as to be trivial to the audience on this forum - I would assume.
/*
Light version for the garage door opener, not using the Carrier
(Since what is done on the carrier is not the most useful part here)
Sketch for Arduino IOT - smart garage door opener
More information on the relay used to connect to the gate can be found here:
https://arduinogetstarted.com/tutorials/arduino-relay
https://howtomechatronics.com/tutorials/arduino/control-high-voltage-devices-arduino-relay-tutorial/
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
bool gateButton; // Sends an open or close signal (two blips)
bool singleBlip; // Sends a single blip to the relay
int distanceSensor; // The actual value of the sensor
bool garageDoorOpen; // Status indicator based on distance sensor
int doorOpenInteger; // Status indicator for line chart
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
// Relay control pin connected to garage door
int garageRelayPin = 7;
int distanceSensorPin = 0;
void setup() {
// Initialize serial and wait for port to open:
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
// Serial.begin(9600);
// delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
// Get Cloud Info/errors , 0 (only errors) up to 4
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
// Wait to get cloud connection to init the carrier
while (ArduinoCloud.connected() != 1) {
ArduinoCloud.update();
delay(500);
}
// Define the output pin for the relay
pinMode(garageRelayPin, OUTPUT);
digitalWrite(garageRelayPin, HIGH);
// For some weird reason the setup causes a single blip,
// fix this by sending 3 more and keeping the status constant
// Blip 1
digitalWrite(garageRelayPin, LOW);
delay(250);
digitalWrite(garageRelayPin, HIGH);
delay(250);
// Blip 2
digitalWrite(garageRelayPin, LOW);
delay(250);
digitalWrite(garageRelayPin, HIGH);
delay(250);
// Blip 3
digitalWrite(garageRelayPin, LOW);
delay(250);
digitalWrite(garageRelayPin, HIGH);
delay(250);
}
void relayClose(){
// Close the cirquit
digitalWrite(garageRelayPin, LOW);
}
void relayOpen(){
// Open the cirquit
digitalWrite(garageRelayPin, HIGH);
}
void sendSignal(){
relayClose();
delay(500);
relayOpen();
delay(250);
}
void sendDoubleSignal(){
// A double push is needed to open or close the gate
relayClose();
delay(500);
relayOpen();
delay(250);
relayClose();
delay(500);
relayOpen();
}
void loop() {
ArduinoCloud.update();
// Program code starts here
Serial.println("I'm working!");
// read the sensor values
// temperature = carrier.Env.readTemperature();
// humidity = carrier.Env.readHumidity();
// read distance sensor pin
distanceSensor = analogRead(distanceSensorPin);
Serial.print("Sensor value: ");
Serial.println(distanceSensor);
// determine if gate is open or closed
if (distanceSensor > 500){
garageDoorOpen = false;
doorOpenInteger = 0;
}else{
garageDoorOpen = true;
doorOpenInteger = 1;
}
// wait between iterations
delay(300);
}
void onGateButtonChange() {
if (gateButton == true) {
Serial.println("Dashboard button has been pressed");
sendDoubleSignal();
}
}
void onSingleBlipChange() {
if (singleBlip == true) {
Serial.println("Single blip button has been pressed");
sendSignal();
}
}