Good day everyone! I need help on a project I'm working on right now. I'm using this Coin Acceptor and I'm not getting pulses every time I insert a coin, I know this because the LED pin supposed to light up and Number of pulses supposed to be printed on the serial monitor If I get pulses. LED and Serial Monitor only reacts when I remove the 5V pin. this is a schematic and program of the project.. I am using a 2W 10k ohm resistor.
// Constants
const int coinpin = 2;
const int ledpin = 13;
// Variables
volatile int pulse = 0;
boolean bInserted = false;
// Setup
void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
pinMode(ledpin, OUTPUT);
}
// Main loop
void loop() {
if(coinpin == LOW) {
Serial.print("high");
}
if( bInserted ){ // for show LED ON when Inserted
bInserted = false;
digitalWrite(ledpin, HIGH);
delay(1000);
}else{
// Turn off LED
digitalWrite(ledpin, LOW);
}
}
// Interrupt
void coinInterrupt(){
// Each time a pulse is sent from the coin acceptor,
// interrupt main loop to add 1 and flip on the LED
pulse++ ;
bInserted = true;
digitalWrite(ledpin, HIGH);
Serial.println( pulse );
}
The diagram looks like it's ok, so I don't know why it'd work without the pullup.
Anyway, assuming the yellow pin is open collector and brought to GND to send a pulse, first of all you have a few issues.
First, this code: if(coinpin == LOW) {
does not check the pin value, because "coinpin" is the pin number, not its state.
Second, apart from avoiding printing within an ISR, you also must declare "volatile" all the variables changed by the ISR. Including bInserted.
Said that, I would explicitly declare the pin as INPUT, and you could even avoid the resistor if you declare the pin as INPUT_PULLUP.
I assume as a first stage you just want to check if a coin is inserted, and not the kind of it, so after you'll solve the current issue, as a next phase you will then need to count the pulses within a fixed amount of time.
So start with something like this (not tested, you must do it!):
I tried using this code. but still nothing.. The coin acceptor worked on my nodemcu with a premade code, and also didnt need to use a resistor last time so I dont think the coin acceptor is broken.
Yes, you can even connect only an LED and resistor in such a way, it will illuminate with a pulse. You can write a simple sketch to echo the digital state of a pin to the on board LED, too.
If the yellow output goes to 12V to send a pulse you could burn the LED. You need a partitor to drop the voltage to 5V (2 resistors from 12V to GND, but do you know how to get partitor resistors values?) and then another resistor to limit the current (typically 220 Ohms).