Automated gate opens randomly

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:

  1. 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".
  2. 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();
  }
}

Does the problem appear on a specific day? (I know you said approximately).
Just wondering if its a source of interference causing it. (The mobile sort).

Good luck..........

why do you use inverted logic?
in the setup there is a point that the you do not have any power to the relay (the relay pin on the arduino), then you get the blip
if the relay is on NO setting then reverse it to NC or vice versa and flip the logic in the code
It makes more sense to use more power when idle than in operation.
Granted that you do not probably care about a few mA energizing the relay coils....

Thanks for your reply!

As of now I can detect no real regularity in it, currently it has happened once at 6AM, 10PM and once at 5PM. The only other things that are operating nearby are a pump to evacuate rainwater and a 12v car battery charger.

The low frequency makes it very hard to debug, if only it happened every 15 minutes :sweat_smile:

Hi!

As you can probably make out I'm no expert by any means... Thank you for this comment, always happy to learn the right way of doing things!

I'll update my wiring and do an update.

LP

I looked at the relay module to see why reversed logic was used.
the arduino does not have enough current to drive the relay directly from the pins
but the module is by design active low, this means the module will turn on the power until you send it a HIGH signal
You should not use active LOW module for this project. unless you want to connect the reverse way and connect normally closed and ground on the relay.
this has still the disaadvance of sending a signal to the gate if the module powers down for some reason and the relay will be constantly on and consuming power
this module will be more appropriate:

it has jumper for active hi or active low

If the Arduino malfunctions are somehow related to the operation of the pump, you need to analyze well the entire circuit of your project for possibile inerference, especially the circuits that relate to the power supply and the connection circuit of the distance sensor. Pay attention to the length of the wires connecting the sensor.
Show a diagram of your project.
If you fail to find the reason, you will need to make a logger that will record the distance sensor signals when the gate is opened.
Could animals or children trigger the distance sensor?

Hi

Thanks for your analysis, had no idea that this was something to look out for - I've ordered a couple of the relays you linked (other website since shipping to Belgium was a bit extreme: Relais | OTRONIC Elektronica).

Would make sense that this will be less sensitive in case of a reboot, and will hopefully solve this problem! As soon as I've tried it I'll report back, you never know someone else might run into the same issue.

Thanks for your help, much appreciated!

Hi Boffin

Thanks for your input on this, this will be useful to troubleshoot my second issue! I must admit that interference was not something that I thought of in this context, but I've read up on the topic now and this might indeed be an issue.

As of now I must admit that the wiring is quite a botch-job - so as I'm going to dismantle it to install another relay I'm also going to do some actual soldering and eliminate the mess of wiring.

Kids or animals are unlikely to trigger the sensor as it is placed now - save maybe a sizeable and malevolent spider.

I'll do a redesign and share my updated code and diagram, might help someone else in the future.

Thank you for your reply, Post #4
I was thinking of a vehicle rather than a tarantula .
Like poorly suppressed ignition or alternator.
I haven't read all the replies, Have you ruled out the pump? When it switches off, it will put a spike on the mains (AC supply).
Apologies if this has been discussed.

Good luck............

Hi Notrauma

Luck would have it that today was an exceptionally rainy day - which means that the pump gets activated quite a lot (it is used to evacuate rainwater, checking if that continues to go well is another project on the to-do list).

But no unsolicited garage door opens today, so I'm assuming that the pump is not actively causing issues... But, as is mentioned above I should have been using relays that require no output signal to remain open - which might cause the issue.

The relay being active-low should not be a problem, the relay driver is opto-isolated and the low from the arduino effectively provides a ground connection for the LED in the opto-isolator. You can likely eliminate the relay blip in setup by writing a HIGH to the relay pin before setting the pinMode instead of afterwards.

It is generally not a good idea to power a relay board from the arduino 5V output. That board has a jumper on the 3-pin connector labeled GND/VCC/JD-VCC, the jumper can be removed and a separate 5v supply connected to GND and JD-VCC to power the relay board. The arduino is then connected to the VCC and IN1/IN2 pins on the 4-pin connector, WITHOUT making any GND connection between the relay board and the arduino.

Sounds sensible, I'll see if I have another 5v power supply lying around here - while I'm at it :slight_smile: Thanks for your advice!