I have an ongoing project which involves controlling a model train which runs backwards and forwards over a short track. The train has to stop at four places over the track. I am using the following circuit (this is a fragment of the full diagram): The IR sensors have an open-collector output and I am pulling the signal line up to the supply voltage and decoupling the line to ground with a 10 nF ceramic capacitor (not shown in the circuit).
Within the code I am using the following lines to detect the train going over the sensor (just one sensor shown:
The problem is that an edge is often detected before the train gets to the sensor I have put a scope onto the sensor output line and it is quite clean with only about 30 mV of noise (this is a 70 MHz scope). I can only assume that the Pi is seeing some very fast spikes which are riding on its inputs.
Code: Select all
sensor_b = 6 .... GPIO.setup(sensor_b, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set Sensor B as input. .... GPIO.wait_for_edge(sensor_b, GPIO.FALLING, ) # Wait for Train to pass Sensor B (entrance to Tunnel on Out leg)
I initially tried to use add_event_detect on the sensor pins, but that was causing multiple interrupts as the train passed over the sensor. I am also using wait_for_edge on the Guard Rail Button, which is used to start the sequence. That occasionally fires when unattended too, so the problem isn't confined to the sensors.
Can anyone suggest a better way to do this?