1

I have a Coin Acceptor, that sends a 30ms pulse when the coin was dropped. So I tried to use many codes, but I search in the net and I think the way is use PWM to read the pulse. But this doesnt work:

import time
import RPi.GPIO as GPIO
t=30 #actuation time in ms
pin=12 #GPIO pin to use
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.IN)
p = GPIO.PWM(pin, 1/(t/1000))
p.start(1)
while True:
    if GPIO.input(pin):
        print("pulse 30ms")
GPIO.cleanup()
  • PWM has nothing to do with your coin sensor. – joan Oct 11 '15 at 20:11
  • @joan so, i have to try to use tcrt5000 or have another way to run this? – Radagast Oct 11 '15 at 20:20
  • No, none of this will help you interface to your coin sensor. If what we have already discussed does not work either the sensor or the Pi is broken. – joan Oct 12 '15 at 7:41
1

Can you try this for me?

import time
import RPi.GPIO as GPIO
t=30
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.IN)
p = GPIO.PWM(pin, 1)
p.start(1)
return = 1
while (repeat = 1):
    time.sleep(0.5)
    if GPIO.input(12):
        print("pulse 30ms")
        repeat = 1
    else:
        repeat = 1
| improve this answer | |
  • RuntimeError: You must setup() the GPIO channel as an output first – Radagast Oct 11 '15 at 20:02
  • The GPIO channel is set as an input here: GPIO.setup(12, GPIO.IN) – Kachamenus Oct 11 '15 at 20:04
  • I know, but the error still showing – Radagast Oct 11 '15 at 20:14
  • @ArnaldoBadin is correct I think - to use the GPIO pin as an input I believe you may have to set it to an output, set it to a particular logic level (cannot remember which), the library initialises it and then you set it to be an input... However I think you are barking up the wrong tree with using the PWM here - you need to run some sort of timer between detecting both edges of a low (or high) going pulse on the input pin... – SlySven Feb 8 '16 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.