Friday, January 31, 2014

Interrupt-based sketch for Adafruit coin acceptor

Some time ago I bought this coin acceptor. Programming it to accept 4 different types of coins is convoluted, but quite ingenious, and works exactly as the spec sheet indicates, without glitches. Surprising for such a complex task, considering that the user interface consists of 3 buttons and two 7-segment LED displays.
After setting it up, the mechanism identifies each coin type reliably, even providing user-friendly feedback (showing on the LED display the number of impulses sent over the COIN/white wire), very helpful for debugging.

Now onto actually using it. Adafruit's graciously provided the "Piggy bank" sample project,  including the Arduino sketch as well. The sketch however, counts the impulses simply in the loop() function. That may work fine in that particular setup, with just the coin acceptor (1 coin type nonetheless) and the LCD display. If you want to add more hardware (printer, buttons, bluetooth, SD card, LEDs etc), you need a smarter, hardware-independent, way to count the impulses from the coin acceptor. Naturally, that involves interrupts.

I present below a snapshot of my interrupt-based sketch, tested in a more complex hardware setup. The COIN wire is connected to D2, attached to interrupt INT0. Function coinISR() gets called for each impulse from the coin acceptor. A coin is recognized when a train of expected number of impulses is received in a sequence.

#define COIN_PIN 2

void setup()
  // Debugging output

  // set up the LCD's number of rows and columns:
  lcd.begin(16, 2);

  pinMode(COIN_PIN, INPUT);
  attachInterrupt(0, coinISR, RISING);  // COIN wire connected to D2;

// total amount of money collected;
float money = 0.0;

// gets incremented by the ISR;
// gets reset when coin was recognized (after train of pulses ends);
volatile int pulses = 0;
volatile long timeLastPulse = 0;

// executed for every pulse;
void coinISR()
  timeLastPulse = millis();

void loop()
  lcd.print("Please put coins");
  lcd.print("PAID $");

  long timeFromLastPulse = millis() - timeLastPulse;
  if (pulses > 0 && timeFromLastPulse > 200)
    // sequence of pulses stopped; determine the coin type;
    if (pulses == 2)
      Serial.println("Received dime (2 pulses)");
      money += .1;
    else if (pulses == 5)
      Serial.println("Received quarter (5 pulses)");
      money += .25;
    else if (pulses == 10)
      Serial.println("Received looney (10 pulses)");
      money += 1.0;
    else if (pulses == 15)
      Serial.println("Received tooney (15 pulses)");
      money += 2.0;
      Serial.print("Unknown coin: ");
      Serial.println(" pulses");

    pulses = 0;


  1. Thanks for this. Adafruit featured it, but didn't link to it. Hit them up for a link.

    1. Thank you too.
      I included the link to the comments on their posting.

  2. Thank you bro it works very nice!!!!

  3. Hi, thanks for the code but I came across issues when quickly inserting coins: the system can be fooled because the pulse train does not pause between coins.
    I analyzed the CH926 further and made a post with details here :