Coin counter project for school

good day everyone..thank you for accepting me in this forum...i just need help for my project i am creating a arduino coin counter, a project dispensing coins i am using arduino uno, i2c 16x2, 3 buttons for add, subtract and start button coin hopper for dispensing coins and non inverted relay module...i created a code but the problem of my project is when i power up the counter the hopper is started dispensing even i am not pressing any button..i try to change the logic of my relay which i change it to high since the relay is non inverted but the problem is still there the hopper automatic dispensing without pressing any button..please i need your help for this project...this is the code i am using

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

#define ADD_BUTTON_PIN 5         // Pin for Add button
#define SUBTRACT_BUTTON_PIN 6    // Pin for Subtract button
#define START_BUTTON_PIN 7       // Pin for Start button
#define RELAY_PIN A0              // Pin for relay control
#define COIN_HOPPER_PIN 8        // Pin for coin hopper pulse

int coinCount = 0;               // Coin count
int stopCount = 0;               // Stop count (Starts from 0)
bool counting = false;           // State of the counter (counting or not)

// LCD setup (16x2 display with I2C address 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Debounce variables
unsigned long lastAddButtonTime = 0;
unsigned long lastSubtractButtonTime = 0;
unsigned long lastStartButtonTime = 0;
unsigned long debounceDelay = 200;  // 200 ms debounce delay

void setup() {
  // Initialize the LCD
  lcd.init();
  lcd.backlight();
  
  // Initialize button and relay pins
  pinMode(ADD_BUTTON_PIN, INPUT_PULLUP);
  pinMode(SUBTRACT_BUTTON_PIN, INPUT_PULLUP);
  pinMode(START_BUTTON_PIN, INPUT_PULLUP);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(COIN_HOPPER_PIN, INPUT_PULLUP);

  // Start with the relay off
  digitalWrite(RELAY_PIN, LOW);
  
  // Initialize LCD display
  displayLCD();
}

void loop() {
  // Button handling
  handleButtons();

  // Check if the counting has started
  if (counting) {
    // Check for coin pulses from the coin hopper
    if (digitalRead(COIN_HOPPER_PIN) == LOW) {
      delay(50);  // Debounce the coin hopper signal
      if (digitalRead(COIN_HOPPER_PIN) == LOW) {
        coinCount++;
        displayLCD();
      }
    }
    
    // Stop counting when the desired count is reached
    if (coinCount >= stopCount) {
      counting = false;
      digitalWrite(RELAY_PIN, LOW);  // Turn off the relay
    }
  }
}

// Function to handle button presses for adding, subtracting, and starting
void handleButtons() {
  unsigned long currentMillis = millis();
  
  // Add button
  if (digitalRead(ADD_BUTTON_PIN) == LOW && (currentMillis - lastAddButtonTime) > debounceDelay) {
    lastAddButtonTime = currentMillis;
    stopCount++;  // Increase stopCount by 1
    displayLCD();
  }

  // Subtract button
  if (digitalRead(SUBTRACT_BUTTON_PIN) == LOW && (currentMillis - lastSubtractButtonTime) > debounceDelay) {
    lastSubtractButtonTime = currentMillis;
    if (stopCount > 0) stopCount--;  // Decrease stopCount but never below 0
    displayLCD();
  }

  // Start button
  if (digitalRead(START_BUTTON_PIN) == LOW && (currentMillis - lastStartButtonTime) > debounceDelay) {
    lastStartButtonTime = currentMillis;
    startCounting();
  }
}

// Function to start the counting process
void startCounting() {
  counting = true;
  coinCount = 0;                   // Reset coin count
  digitalWrite(RELAY_PIN, HIGH);    // Turn on the relay
  displayLCD();
}

// Function to display the information on the LCD
void displayLCD() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Counter : ");
  lcd.print(coinCount);             // Display current coin count
  lcd.setCursor(0, 1);
  lcd.print("Stop Cnt: ");
  lcd.print(stopCount);             // Display the stop count (starts from 0)
}

this is the diagram i am using

1 Like

One thing that jumped out at me: an Arduino output pin won't source anywhere near enough current to energize a relay coil. At least, it probably won't do it twice... And if that didn't burn out the output, the reverse EMF from the coil when the magnetic field collapsed quite likely finished the job.

On your next Uno, you'll need a transistor with a base/gate resistor (depending on whether you go BJT or MOSFET) to drive the relay coil, and a reverse biased diode across the relay coil to absorb the reverse EMF.

1 Like

…or a relay module with a logic level control pin, and voltage supplied directly from the supply rail.

1 Like

how about the code?is this correct?

what kind of mosfet?

what kind of relay module?

Is there any type reference on your relay?

During startup the status of the output pins is not defined yet..
One resistor may do the trick...

1 Like

i use a non inverted relay module 5v

what kind of resistor and where to attach it?

this relay

Looks like the relay module might be opto driven.

Please supply the relay module specs.

Not sure what you mean by non-inverted relay. Most will have c/o contacts to suit whatever logic you feed in.

Yes. I used buttons for add, subtract, start and hopper.
If I press "add" four times, "Stop Cnt:" reads 4
If I press "subtract" two times, "Stop Cnt:" reads 2
If I press "start" the relay turns on and "Counter:" reads 0
If I press "hopper" the relay turns off and "Counter:" counts to the same number in "Stop Cnt:"

1 Like

good for you,,may project is too bad i cant fix the problem

I used your code. Your code works.
You need to follow Post #2, #3, #7 and #11.

1 Like

you use in wokwi?

i am starting learning this..i cant figure out what mosfet to use and resistor

Yes.

1 Like

what possible kind of mosfet i use and resistor?in the diagram i use a 10k ohm resistor connected from the pin of the coin sensor and the other side to the positive 5v

You do not need a mosfet. Your relay module already has a driver.
Try to add a 1k resistor from A0 to ground. That should hold the input low during startup.

1 Like

the 10k ohm resistor from sensor pin to 5v?is that ok?