Slotted Optical Switch

Hello.
I have purchased a slotted optical switch.

I want to
A) Measure the difference in Voltage between 2 different coins that are slotted through it (both have different diameters)
B) Have my Arduino recognise which coin has been inserted/slotted and count the number of times that type of coin has been inserted.

The optical switch I have purchased is http://docs-europe.electrocomponents.com/webdocs/02f1/0900766b802f1cd8.pdf
and I have built a coin track for this sensor. All I need to know is how I must approach this project, and perhaps a little help with the coding.

Many thanks,

James

A) Measure the difference in Voltage between 2 different coins that are slotted through it (both have different diameters)
B) Have my Arduino recognise which coin has been inserted/slotted...

The optical switch is an on-off switch, so it's not going to help you with that. :frowning: The infrared light from the built-in LED is either blocked or not-blocked, and obviously zero light is passing-through either coin. :wink:

Sometimes your coin mechanism has separate paths for different coins (and in that case you might need two or more optical switches).

... and count the number of times that type of coin has been inserted.

I think I'd start with simple pushbutton and the [u]Button Example[/u]. Then you can wire-up the optical switch and try it out.

Then, look at some of the other examples and the [u]Language Reference[/u] to figure-out how to make it count.

If you are new to programming here's a hint: The command X++; will increment the variable X by one. Or you can use X= X+1; which doesn't make any sense in algebra, but in C++ it means "Make X equal to the old value of X plus 1".

You'll also have to figure-out what you are going to do with the count once it's in the Arduino's memory. :wink: During development and debugging, you can use the [u]Serial Monitor[/u] to send the "count" (or any other variables/information) to your computer screen.

You'll have to "debounce" the switch (both the mechanical & optical switches). That can be done in hardware or software, but it should be easier to do in software. Basically, that involves "slowing-down" the count so if there is electrical "noise" as the switch turns on-and-off, you don't count the coin more than once in a few microseconds or milliseconds.

Your datasheet for the optical switch doesn't show any example circuits... It's basically an opto-isolator with a slot in it to block the light from the LED. I found something [u]here[/u] for a regular opto-isolator (figure 16), but it's not very complete. Maybe, you can find some opto-isolator application examples. You need a current-limiting resistor in series with power to the LED (not shown on the 4n25 datasheet), and you need a pull-up resistor on the output transistor. Assuming the thing is running off the Arduino's 5V power supply, a 150 Ohm resistor in series with the LED will give you a little more than 20mA. The the pull-up resistor value is not critical at all (because of the Arduino's very-high input impedance) and the easiest thing to do is to configure the Arduino's [u]Internal Pull-Up Resistor[/u].

DVDdoug:
... obviously zero light is passing-through either coin.

Not at all obvious, as India used to (and may still?) have coins with a hole in the middle, like a decorated washer. He doesn't state which currency he's using.

Thank you DVDdoug for your very detailed reply. I appreciate the time it took you to write that..

I am using 1p and 2p coins (uk).

How do I swap the push button for the optical switch?

Do I use the same resistors?

So far I have tried to set it up this way: How to Build a Robot Tutorials - Society of Robots (first diagram) using R1 = 80R and R2 = 800R. I have run standard firmata to test the outputs and the A1 pin reads 1021, and is not effected by covering the emitter with a coin.

What have I done wrong?

DVDdoug:

You need a current-limiting resistor in series with power to the LED (not shown on the 4n25 datasheet), and you need a pull-up resistor on the output transistor. Assuming the thing is running off the Arduino's 5V power supply, a 150 Ohm resistor in series with the LED will give you a little more than 20mA. The the pull-up resistor value is not critical at all (because of the Arduino's very-high input impedance) and the easiest thing to do is to configure the Arduino's [u]Internal Pull-Up Resistor[/u].

I'll try this :slight_smile: