Coin hopper + arduino

Hi, I am fairly new to arduino and just wondering if someone can help me with configuring this coin hopper into arduino. There are no data sheets provided so I don't know how to connect it to the arduino uno.

A picture is not enough, and there's no stardard for this kind of device, so please post also a link to the specific item you bought (and always do it when asking for help).

PS: no datasheet? Well, typical for AliExpress, right? I don't know why people use it (except for saving a few bucks) instead of a local dealer...

I saw a related discussion but im still in the dark Coin counter project for school - Using Arduino / Project Guidance - Arduino Forum

In my testing I managed to make it run but it just keeps on running and not dispensing the right amount.

  #define sensorPin 3
  #define motorPin 4

  byte amountToDispense = 0;

  volatile byte amountDispensed = 0;

  void setup() {
    Serial.begin(9600);
    attachInterrupt(digitalPinToInterrupt(sensorPin), coinPulse, FALLING);
    pinMode(sensorPin, INPUT_PULLUP);
    pinMode(motorPin, OUTPUT);
    digitalWrite(motorPin, HIGH);
  }

  void loop() {
    if (Serial.available())
    {
      amountToDispense = Serial.read() - 48;
      digitalWrite(motorPin, LOW);

    }

  }

  void coinPulse()
  {
    ++amountDispensed;
    if (amountDispensed == amountToDispense)
    {
      Serial.print("Completed");
      digitalWrite(motorPin, HIGH);
      amountDispensed = 0;
    }
  }

I hate to say this, but with no information except the picture, the only resource you could use is ChatGPT, uploading the picture and asking "How can I connect this coin hopper to Arduino?".

This is the result, but take it with a grain of salt:

To connect this coin hopper to an Arduino, you will typically need to follow these steps:

1. Identify the Pins

Most coin hoppers, like the one in your image, have a set of pins or wires for:

  • Power (VCC/GND): Typically 12V or 24V for the motor.
  • Control (Signal/Input): Usually used to start and stop the motor.
  • Output/Signal (Coin detection): Provides feedback when a coin is dispensed.

You may need to refer to a datasheet or inspect the PCB for labeled pins to determine their exact functions.

2. Power Supply

  • Power Requirements: These hoppers typically run on 12V or 24V. You will need an external power source, as the Arduino cannot supply that voltage.
  • Grounding: Connect the ground (GND) of the power supply to the GND of the Arduino to have a common ground.

3. Connecting to Arduino

To control the hopper with an Arduino:

  • Use a MOSFET or Relay Module: The Arduino's output pins usually operate at 5V and provide limited current, insufficient for directly controlling a 12V motor. A MOSFET or a relay module will allow you to use the Arduino's 5V signal to switch the higher voltage needed for the hopper.
  • Control Pin: Connect one of the Arduino's digital pins to the control input of the MOSFET/Relay.

Example Wiring:

  1. Coin Hopper Power: Connect the hopper's VCC to 12V power supply and GND to the ground of both the power supply and Arduino.
  2. Signal Input: Connect the control pin of the hopper to a MOSFET or relay switch, and then to one of the Arduino's digital output pins.
  3. Coin Detection Output (if applicable): If there's an output signal for coin detection, connect it to an Arduino input pin with a pull-up resistor.

Arduino Code:

Here's an example code snippet to control the hopper:

int hopperPin = 7;  // Digital pin connected to the relay/MOSFET controlling the hopper

void setup() {
  pinMode(hopperPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // Start the coin hopper
  digitalWrite(hopperPin, HIGH);
  delay(1000);  // Run hopper for 1 second

  // Stop the coin hopper
  digitalWrite(hopperPin, LOW);
  delay(1000);  // Pause for 1 second
}

Additional Considerations:

  • Flyback Diode: When using a relay or MOSFET, place a flyback diode across the motor terminals to protect against voltage spikes when switching off the motor.
  • Optocoupler (if needed): For additional protection, an optocoupler can be used between the Arduino and the hopper's control line.

I managed to make it run now, made it so that it counts how many coins but there are still inaccuracy. Do you think it's cause I am currently not using any resistor to help smooth out the signal? This is my current setup and code

![hop|666x500](upload://3YZsquG3zRRG6ODepdqvQkeg6R5.png)



#define sensorPin 3  // Pin for the coin sensor
#define motorPin 4   // Pin for controlling the motor (hopper)
int coinCount = 0;  // Variable to keep track of the number of coins dispensed
bool motorRunning = false;  // Flag to check if motor is running

unsigned long lastPulseTime = 0;
const unsigned long debounceDelay = 200;

void setup() {
  Serial.begin(9600);  // Start serial communication
  pinMode(sensorPin, INPUT_PULLUP);  // Set the sensor pin as input
  pinMode(motorPin, OUTPUT);  // Set the motor pin as output
  digitalWrite(motorPin, HIGH);  // Turn off the motor initially
  attachInterrupt(digitalPinToInterrupt(sensorPin), checkForCoins, FALLING);
}

void loop() {
  if (Serial.available() > 0) {  // Check if data is available from serial
    String command = Serial.readString();  // Read the command
    command.trim();  // Remove any leading/trailing whitespace

    if (command == "go" && !motorRunning) {
      startMotor();  // Start the motor when "go" is received
    } 
    else if (command == "stop" && motorRunning) {
      stopMotor();  // Stop the motor when "stop" is received
    }
  }

  if (motorRunning) {
    checkForCoins();  // Continuously check for coins while motor is running
  }
}

void startMotor() {
  motorRunning = true;  // Set motorRunning flag to true
  coinCount = 0;  // Reset coin count when motor starts
  Serial.println("Motor started.");
  digitalWrite(motorPin, LOW);  // Turn on the motor
}

void stopMotor() {
  motorRunning = false;  // Set motorRunning flag to false
  digitalWrite(motorPin, HIGH);  // Turn off the motor
  Serial.println("Motor stopped.");
  Serial.print("Total coins dispensed: ");
  Serial.println(coinCount);  // Print the total coins dispensed
  coinCount = 0;  // Reset the coin count after stopping
}

void checkForCoins() {
  unsigned long currentTime = millis();
  if (currentTime - lastPulseTime > debounceDelay){
    if (digitalRead(sensorPin) == LOW) {  // Check if the sensor detects a coin
    if (digitalRead(sensorPin) == LOW) {  // Confirm the coin detection
      coinCount++;  // Increment the coin count
      Serial.print("Coins dispensed: ");
      Serial.println(coinCount);  // Print the current coin count
      lastPulseTime = currentTime;
    }
  }
  }
}

The image is not visible, you must edit the post and insert it again

@docdoc - how to resurrect an image from "code" blocks...

  1. copy the obfuscated title and extension
  2. upload a temporary image
  3. paste the title and extension over the temporary image
  4. repair abnormalities (original image was larger than the temporary image)
  5. upload repaired image
  6. Profit.

ORIGINAL

REPAIRED