1

I have square signal attached to the pin 23 and am running the following code to detect how long does the pulse width last:

import RPi.GPIO as GPIO
import time

SENSOR_PIN = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

while True:
    GPIO.wait_for_edge(SENSOR_PIN, GPIO.RISING)
    start_high = time.time()
    GPIO.wait_for_edge(SENSOR_PIN, GPIO.FALLING)
    end_high = time.time()
    high_width = end_high - start_high
    print(high_width)

The code is blocks on GPIO.wait_for_edge(SENSOR_PIN, GPIO.RISING).

The part that gets me confused is it works normally if it's run without the for loop. The code below exits normally for a same physically setup:

import RPi.GPIO as GPIO
import time

SENSOR_PIN = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.wait_for_edge(SENSOR_PIN, GPIO.RISING)

Are there some known peculiarities regarding the GPIO.wait_for_edge?

Your Answer

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

Browse other questions tagged or ask your own question.