I've got a device (SparkFun's 3 coin acceptor: https://www.sparkfun.com/products/11719) which outputs a signal in pulse widths. How can I read this signal in?
I comfortable using the RPi.GPIO library for simple I/O, but I'm not sure about this. Is this something that can be handled by RPi.GPIO?
-
- Posts: 7
- Joined: Sat Jun 29, 2013 11:54 pm
Re: How do I read in Pulse Widths?
On my phone so autocorrect will try to break this
start = time.time()
stop = time.time()
While GPIO.input(pin) == 0:
start = time.time()
While GPIO.input(pin) == 1
stop = time.time()
Elapsed = stop - start
Elapsed is now equal to the length of the pulse.
start = time.time()
stop = time.time()
While GPIO.input(pin) == 0:
start = time.time()
While GPIO.input(pin) == 1
stop = time.time()
Elapsed = stop - start
Elapsed is now equal to the length of the pulse.
Re: How do I read in Pulse Widths?
On my phone so autocorrect will try to break this
start = time.time()
stop = time.time()
While GPIO.input(pin) == 0:
start = time.time()
While GPIO.input(pin) == 1
stop = time.time()
Elapsed = stop - start
Elapsed is now equal to the length of the pulse.
start = time.time()
stop = time.time()
While GPIO.input(pin) == 0:
start = time.time()
While GPIO.input(pin) == 1
stop = time.time()
Elapsed = stop - start
Elapsed is now equal to the length of the pulse.
Re: How do I read in Pulse Widths?
How you measure pulses will depend on how long the pulses are, the rate ect., and how accurately do you need to measure them.
For the coin slot you don;t need to measure pulses. According to Sparkfun video the unit outputs serial at 9600 baud. You need to connect the serial to the Pi UART Rx input.
I don't see any info on the signal levels, but the presenter mentions FTDI breakout. Check carefully how to safely connect the serial to the Pi. to read the serial device. You should be able search to find info on how to do that.
For the coin slot you don;t need to measure pulses. According to Sparkfun video the unit outputs serial at 9600 baud. You need to connect the serial to the Pi UART Rx input.
I don't see any info on the signal levels, but the presenter mentions FTDI breakout. Check carefully how to safely connect the serial to the Pi. to read the serial device. You should be able search to find info on how to do that.
-
- Posts: 7
- Joined: Sat Jun 29, 2013 11:54 pm
Re: How do I read in Pulse Widths?
6677: Thanks, I guess it doesn't get much more basic than this 
PiGraham: At about 2:45 in that video (http://youtu.be/Dyun1xjKqc4?t=2m45s) the presenter does talk about a serial connection, however the video is actually talking about two different units. At 4:14 (http://youtu.be/Dyun1xjKqc4?t=4m14s) the presenter mentions that the second unit uses pulse widths.
The documentation for the device is very poor, however, there's a 5v signal coming out on one of the wires which tells you when and which type of coin has been dropped through the device. I've built a voltage divider to take the signal down to 3.3v, and I've got that hooked up to a GPIO pin, but I was a little confused about how to actually grab the signal.

PiGraham: At about 2:45 in that video (http://youtu.be/Dyun1xjKqc4?t=2m45s) the presenter does talk about a serial connection, however the video is actually talking about two different units. At 4:14 (http://youtu.be/Dyun1xjKqc4?t=4m14s) the presenter mentions that the second unit uses pulse widths.
The documentation for the device is very poor, however, there's a 5v signal coming out on one of the wires which tells you when and which type of coin has been dropped through the device. I've built a voltage divider to take the signal down to 3.3v, and I've got that hooked up to a GPIO pin, but I was a little confused about how to actually grab the signal.
Re: How do I read in Pulse Widths?
OK, I see what you mean. How long are the pulses? If the pulse is too short you may miss it.
Assuming it's slow enough to catch you may want to look at the GPIO events. This would be vital if your program has anything else to do since the program is tied up polling for the first edge.
Your program do whatever it may need to do while the event monitoring goes on in the background.
The serial output version is a better choice for the Pi if the pulse output is short. The Pi will buffer the signal.
If you miss the signal the coin is trapped.
Assuming it's slow enough to catch you may want to look at the GPIO events. This would be vital if your program has anything else to do since the program is tied up polling for the first edge.
Either latch a time in a leading edge event callback and read and compare to the latched value in a falling edge callback, or sit an poll for end of pulse in the leading edge callback.event_detected() function
The event_detected() function is designed to be used in a loop with other things, but unlike polling it is not going to miss the change in state of an input while the CPU is busy working on other things. This could be useful when using something like Pygame or PyQt where there is a main loop listening and responding to GUI events in a timely basis.
GPIO.add_event_detect(channel, GPIO.RISING) # add rising edge detection on a channel
do_something()
if GPIO.event_detected(channel):
print('Button pressed')
Note that you can detect events for GPIO.RISING, GPIO.FALLING or GPIO.BOTH.
link
Your program do whatever it may need to do while the event monitoring goes on in the background.
The serial output version is a better choice for the Pi if the pulse output is short. The Pi will buffer the signal.
If you miss the signal the coin is trapped.
-
- Posts: 7
- Joined: Sat Jun 29, 2013 11:54 pm
Re: How do I read in Pulse Widths?
hmmm...
Maybe I should be using a microcontroller in between the signal and the RaspberryPi?
I've got the MSP430 Value Line LaunchPad which comes with a couple of microcontrollers (M430G2452 & M430G2553). Perhaps I could read in the pulses on one of the MSP chips, and output a longer slower pulse to the Raspberry Pi (or maybe just toggle between high & low whenever a coin in detected?). I've only got two different types of coins to watch for, so I could have a separate pin for each.
Maybe I should be using a microcontroller in between the signal and the RaspberryPi?
I've got the MSP430 Value Line LaunchPad which comes with a couple of microcontrollers (M430G2452 & M430G2553). Perhaps I could read in the pulses on one of the MSP chips, and output a longer slower pulse to the Raspberry Pi (or maybe just toggle between high & low whenever a coin in detected?). I've only got two different types of coins to watch for, so I could have a separate pin for each.
Re: How do I read in Pulse Widths?
How wide are the pulses?
-
- Posts: 7
- Joined: Sat Jun 29, 2013 11:54 pm
Re: How do I read in Pulse Widths?
According to one of the commenters on the product page, "when set to fast speed, the pulse is 30 ms with 130 ms peak to peak time. This means that for the maximum of 50 pulses you’ll have to wait about 6.5 seconds to get all the data." I haven't confirmed this for myself.
Also, the device has a switch to select fast, medium, or slow pulses. I'm not sure if it affects only the time between pulses, or the length of the pulses. I guess I've got some experimenting to do...
I think I'll attempt using just the Raspberry Pi to detect the pulses, and if I'm having trouble, I'll try adding a microcontroller to the mix.
Also, the device has a switch to select fast, medium, or slow pulses. I'm not sure if it affects only the time between pulses, or the length of the pulses. I guess I've got some experimenting to do...
I think I'll attempt using just the Raspberry Pi to detect the pulses, and if I'm having trouble, I'll try adding a microcontroller to the mix.
Re: How do I read in Pulse Widths?
Can I ask why you want to measure the pulse width itself? From my perusal of the datasheet, it looks as though the pulse width is fixed, and the number of pulses determines what coin went through. Perhaps I'm wrong about that, but as near as I can tell, isn't a way to count pulses what you really want?
-
- Posts: 7
- Joined: Sat Jun 29, 2013 11:54 pm
Re: How do I read in Pulse Widths?
DrMag, I think you're right. I've never really worked with this sort of stuff before, and I think I confused myself. Maybe RPi.GPIO is enough, if all I need to do is count the pulses.
Re: How do I read in Pulse Widths?
It's still a bit more challenging than "just counting pulses", and will take some work. Otherwise, if say you have it set to give two pulses for a quarter and 5 pulses for a dime, how do you distinguish between a dime and three quarters done in rapid succession?
I would first worry about getting the pulse counting working. Once you have that down, you'll have to find some sort of timing that can easily distinguish between the two. You'd essentially be introducing some "dead time": a period where you can't add another coin since the software is still waiting to ensure it isn't a different coin at all. (That's where the quoted commenter's information will come in handy.) At least that's how my mind looks at it right now; there may be a much easier way.
I would first worry about getting the pulse counting working. Once you have that down, you'll have to find some sort of timing that can easily distinguish between the two. You'd essentially be introducing some "dead time": a period where you can't add another coin since the software is still waiting to ensure it isn't a different coin at all. (That's where the quoted commenter's information will come in handy.) At least that's how my mind looks at it right now; there may be a much easier way.
-
- Posts: 7
- Joined: Sat Jun 29, 2013 11:54 pm
Re: How do I read in Pulse Widths?
Actually, I think that if I can get the pulse counting working, I'll be set. I'm only using two types of coins for this project: nickels and dimes. And since you can set what you want the number of pulses to be for each coin, I'll set the number of pulses for a dime to be twice that of nickels (say one pulse for a nickel and two pulses for dime, or maybe five & ten). This way it won't matter if two nickels come right after each other and look like a dime, because I only care about the monetary value, not the denominations of coins used.
Re: How do I read in Pulse Widths?
That's some smart thinking right there.WhiteHotLoveTiger wrote:Actually, I think that if I can get the pulse counting working, I'll be set. I'm only using two types of coins for this project: nickels and dimes. And since you can set what you want the number of pulses to be for each coin, I'll set the number of pulses for a dime to be twice that of nickels (say one pulse for a nickel and two pulses for dime, or maybe five & ten). This way it won't matter if two nickels come right after each other and look like a dime, because I only care about the monetary value, not the denominations of coins used.
note: I may or may not know what I'm talking about...