Error Relay Auto Looping to Solenoid Door Lock

Hello everyone, I'm facing some issues with a project I'm working on.

Problem: When a coin is inserted into the Coin Acceptor, the program should detect it once and the Solenoid Door Lock should open for 5 seconds. However, instead, the Solenoid Door Lock is looping on its own and also randomly activating. I suspect this is caused by the 5V Relay even though I haven't touched it.

Question: What could be the solution to this issue?

Suggested Solution from My Teacher: Connect the VCC on the Relay directly to the 5V Adapter and connect the GND on the Relay to the GND of the Relay, Adapter, and Arduino Uno Board. It's will work ?

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const int coinPin = 2; // Pin to detect coins
const int relayPin = 11; // Pin for relay
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD with I2C address and size 16x2

volatile boolean coinInserted = false; // Status if a coin is inserted
unsigned long balance = 0; // Initial balance
unsigned long lastInterruptTime = 0; // For debouncing
unsigned long unlockTime = 0; // Time when the lock is opened
const unsigned long unlockDuration = 5000; // Duration the lock stays open (5 seconds)

void setup() {
  Serial.begin(9600);

  lcd.begin(16, 2); // Initialize LCD
  lcd.backlight(); // Turn on the LCD backlight

  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW); // Ensure relay is off (lock is locked)
  pinMode(coinPin, INPUT_PULLUP); // Coin pin uses internal pull-up

  attachInterrupt(digitalPinToInterrupt(coinPin), coinInterrupt, RISING);

  lcd.setCursor(0, 0);
  lcd.print("Waiting for coin...");
}

void loop() {
  if (coinInserted) {
    noInterrupts(); // Avoid data conflicts with interrupt
    coinInserted = false; // Reset flag
    interrupts();

    balance += 1; // Add to balance
    Serial.print("Coin accepted! Balance: ");
    Serial.println(balance);

    // Display coin accepted message
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Coin Accepted!");
    lcd.setCursor(0, 1);
    lcd.print("Total Coins: ");
    lcd.print(balance);

    // Unlock (activate relay)
    digitalWrite(relayPin, HIGH);
    unlockTime = millis(); // Record the time when the lock is opened

    delay(2000); // Wait a moment to ensure the display is visible
  }

  if (millis() - unlockTime < unlockDuration && unlockTime != 0) {
    // If the lock is open
    lcd.setCursor(0, 1);
    lcd.print("Door Open");
  } else if (millis() - unlockTime >= unlockDuration && unlockTime != 0) {
    // If the lock open time has elapsed
    digitalWrite(relayPin, LOW); // Turn off relay (lock is locked)
    unlockTime = 0; // Reset the open time
    Serial.println("Door locked again.");

    // Display lock message
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Door Locked");
    delay(1000); // Wait before returning to the initial state
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Waiting for coin...");
  }
}

void coinInterrupt() {
  unsigned long interruptTime = millis(); // Get current time
  if (interruptTime - lastInterruptTime > 500) { // Debouncing (500 ms)
    coinInserted = true; // Set flag when coin is detected
    Serial.println("Interrupt detected, coin inserted!");
  }
  lastInterruptTime = interruptTime; // Save the last interrupt time
}

Please post real schematics. Pen and paper usually works well. Remember to incorporat power supplies.
The datasheet of the coinacceptor is also useful.

She's on the right track.

Replace the relay with a proxy. Here we would temporarily substitute a simpler test load, like a series resistor and LED.

This will show that you indeed have a hardware issue. Or not, in which case you can confidently spend time getting the LED to work, then worry about the relay.

It's part of "divide and conquer", something that has worked for millennia.

a7

Like this bro for the real schematics.


Please read #2 again, from the first letter, to the last.

Hi, @dannif
Welcome to the forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

The image you have posted does not show pin names and has wires going on and off screen.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Thank you for all respond. Now I've finished my project! :pray: :+1: :smiley:

A proper schematic will help others who use this thread to solve similar problems.

Thanks Tom..... :smiley: :+1: :coffee: :australia:

Tell us what happened and post your final sketch.

a7