2

I'm trying to set up a tachometer. I used a comparator IC to get a real clean signal on pin 21 but I'm getting iterrupts on the rising AND falling edges.

#!/usr/bin/python3

import time
import datetime
import RPi.GPIO as GPIO

def Int21R( channel ):
  global Count

  Count += 1
  GPIO.output( 20, GPIO.HIGH )
  time.sleep( 0.000001 )
  GPIO.output( 20, GPIO.LOW )


GPIO.setwarnings(False)
GPIO.setmode( GPIO.BCM )

GPIO.setup( 20, GPIO.OUT )
GPIO.setup( 21, GPIO.IN, pull_up_down = GPIO.PUD_OFF )
GPIO.add_event_detect( 21, GPIO.RISING, callback = Int21R )

Count = 0
while True:
  Time = datetime.datetime.now()
  print( "%s - Count = %6d  RPM = %6d" % ( Time, Count, Count * 60.0 ) )
  RPM = Count * 60.0
  Count = 0
  t = time.time( )
  Wait = 1 - ( t - int( t ) )
  time.sleep( Wait )
except KeyboardInterrupt:
  GPIO.cleanup( )

GPIO.cleanup( )

I output a short pulse on pin 20 in the event handler so i can see it on a scope. scope image It's very clear that the event handler is getting called on the rising and falling edges. I get the same results on RPi B+ and RPi Zero W.

What am I missing?

Update: I'm running Jessie with all updates installed.

Update: Same results on an RPi 3Bv1.2.

  • Odd. I can't see that you have done anything wrong. Perhaps try (my) monitor GPIO to see if you get the same effect (requires the pigpio daemon to be running). – joan Dec 27 '17 at 8:29
  • I ran your monitor.py but I don't know how to post the results here. Basically, it show the rising edge at d=~45200 and falling edge at d=~3080 but sporadically get G=21 l=1 d=5 and G=21 l=0 d=5. What does this tell me? – RachelTX Dec 28 '17 at 0:54
  • I also ran your read_RPM.py. It regularly reports the correct value of ~1200 RPM but sproradically reports 19078 RPM. When I run the motor to full speed it reports the correct speed of ~9500 RPM but sporadically reports ~160000. – RachelTX Dec 28 '17 at 1:04
  • You need to see if you can capture those sporadic events on an oscilloscope. If they are real then the signal isn't as clean as you think. You could set a glitch filter on GPIO 21. – joan Dec 28 '17 at 8:16
  • I added a few bypass capacitors that cleared up the glitches. – RachelTX Dec 29 '17 at 5:37
0

I've since changed my event handler code to

if ( GPIO.input( 21 ) ):
  Count += 1
  GPIO.output( 20, GPIO.HIGH )
  time.sleep( 0.000001 )
  GPIO.output( 20, GPIO.LOW )

This ignores the the falling edge interrupt (which happens to work in this situation). But it does leave open the possibility that a slow response in processing the event would in missing a valid event. Que sera, . . .

0

Your should really consider using a micro-controller (e.g. Arduino) instead of a Raspberry. With no other code running than your own, you will get predictable behaviour and timing in every event, which is simply not possible under Linux. Then you will be able to tell if the input signal is really glitch-free or not.

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.