CH-923 + Arduino Uno + PC wiring

Hi,

I'm trying to connect CH-923 coin acceptor (like this one: Coin acceptor - YouTube ) into my Arduino Uno R3 microcontroller and send information to PC when coin is inserted.
I've already set it up (3 types of coins, 30 samples for each).
Right now I cannot make it work, it looses signals or adds additional random signals.
This is my Arduino sketch:

void sendInfo(const __FlashStringHelper* head, char* data){
  Serial.print(head);
  Serial.println(data);
}

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

  attachInterrupt(0, acceptorCount, RISING); //Digital interrupt pin 2
  
  sendInfo(F("_ON"), "");
}

void loop()
{
  //
}

void acceptorCount()
{
  sendInfo(F("+1"), "");
}

My wiring, real photos of connections, settings of coin acceptor etc are in attachements. I've already tried a lot of options as pull-up resistor (500 Ohms, 1kOhms, 1k2 Ohms, 4k7 Ohms, 47k Ohms 100k Ohms etc), changing settings of coin acceptor, attaching coin acceptor to ground (I mean it's casing isn't grounded so I attached it to the wallet socket's ground).

Right now it does give me exact number of signals (1 polish zloty = 1 pulse; 2 polish zlotys = 2 pulses, 5 polish zlotys = 5 pulses), but also it gives me random pulses. Any ideas what should I do to prevent those ivalid pulses and keep correct pulses?

Have a nice day,
JK.

Bottom line is that you can not do a serial print from inside an interrupt service routine. Also note all variables used both inside and outside of one should be volatile.

You also need to connect the ground of your coin counter to the ground of the Arduino.

Thank yoy very much sir!
Indeed, that was a problem. I also moved sending over Serial into main loop, like this:

unsigned long coinsCount = 0;

void sendInfo(const __FlashStringHelper* head, char* data){
  Serial.print(head);
  Serial.println(data);
}

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

  attachInterrupt(0, acceptorCount, RISING); //Digital interrupt pin 2
  
  sendInfo(F("_ON"), "");
}

void checkCoins(){
  while (coinsCount>0){
    coinsCount--;
    sendInfo(F("+1"), "");
    delay(10);
  }
}

void loop()
{
  //

  checkCoins();
}

void acceptorCount()
{
  coinsCount++;
}

Topic may be closed :slight_smile:

Have a nice day,
JK.

Topic may be closed

Not quite.

unsigned long coinsCount = 0;

should be:-

volatile unsigned long coinsCount = 0;

Because it is used inside and outside the interrupt service routine.

Did it.

OK, right now I have different problem... When somebody touches something made from metal and then he/she touches the coin acceptor (or touch coin and try to insert it), the coin acceptor will give signal. It works everytime - this is what I do: I hold coin in metal tool, touch it with finger, then I touch coin acceptor and it gives me +1 signal, it works as many times as I repeat this... Any idea why is it like this? Is it possible coin acceptor is broken? Or should I buy plastic one (where)?

Try connecting the ground of your coin acceptor to mains ground as well. Sounds like you might have a mains leakage problem.

You mean common ground of Arduino and coin acceptor? I did and doesn`t work. Or you say about connecting something other?

@edit:
You mean - I should add ground of AC of wall socket to common ground of Arduino and coin acceptor?

@edit2:
I also add images of all connections. And here is the connection: Imgur: The magic of the Internet

@edit3:
Something intresting. Even when white cable of coin acceptor is not connected at all to Arduino (pin 2 is connected with pull-up resistor to 5V of Arduino), touching the coin acceptor will give me +1! No idea why...

You mean - I should add ground of AC of wall socket to common ground of Arduino and coin acceptor?

Yes.

Didnt work. Any ideas?

Something intresting. Even when white cable of coin acceptor is not connected at all to Arduino (pin 2 is connected with pull-up resistor to 5V of Arduino), touching the coin acceptor will give me +1! No idea why...

It is not interesting, you wire it up without a ground and it misbehaves why struggle to understand it.

Ami777:
Didnt work. Any ideas?

Get a scope and look at what the signals are doing.

We did it.

No way...

Looks like interrupts don`t work as expected... Im in my local school with Electronics Students for few hours, we tried transoptors, some condestators etc etc, but nothing worked... The only thing we did right now is this simple code in loop:

  if(digitalRead(3)==HIGH)
  {
    unsigned int temporaryValue=millis();
    while(digitalRead(3)==HIGH);
    temporaryValue=millis()-temporaryValue;
    if(temporaryValue>=50 && temporaryValue<=200) { //Not required, but added just to be sure (signal is 100ms)
      sendInfo(F("+1"), "");
    }
  }