Using an optocoupler as a coin detector
If we take advantage of the fact that there is a small space between the emitter and receptor part of a typical optocoupler that allows a coin to pass through, we could easily use one of them to create a simple coin detector that could be used for a bigger project—who knows, perhaps a candy vending machine or your next arcade game cabinet.
From the point of view of the circuit, we will have to provide little additional circuitry—only a pair of resistors: one to limit the current through the infrared LED and another to act as a load for the phototransistor between its emitter and collector.
Regarding the reaction we will get once the coin has been detected, I'll simply turn on an LED for the sake of simplicity in this example project and to allow you to focus on the digital input side of the circuit and corresponding code.