Skip to content

ESP32: PulseIn does not work (and it's weird too) #9979

Open
@0wwafa

Description

@0wwafa

CircuitPython version

Adafruit CircuitPython 9.2.3 on 2025-01-17; ESP32 Devkit V1 with ESP32

Code/REPL

#If you save this as code.py and the run it on boot or soft boot, it will only show 0 and hitting control C will end with 0 (no pulses received)
import board,pulseio,digitalio,time
sense=digitalio.DigitalInOut(board.D14)
sense.direction=digitalio.Direction.INPUT
input("PRESS RETURN TO START\n")
sense.direction=digitalio.Direction.OUTPUT
p=pulseio.PulseIn(board.D13,1024,0)
print('0',len(p))
time.sleep(1)
print('1',len(p))
p=pulseio.PulseIn(board.D13,1024,1)
time.sleep(1)
print('2',len(p))

try:
  while True:
    while p:
      print(p.popleft())
    p=pulseio.PulseIn(board.D13,1024,0)
    time.sleep(0.01)

except KeyboardInterrupt:
  sense.direction=digitalio.Direction.INPUT
  print(len(p))
  p.deinit()
  sense.deinit()
  print("program ended.")
  pass

except Exception as e:
  sense.direction=digitalio.Direction.INPUT
  p.deinit()
  sense.deinit()
  print("program ended because of ",e)



# but if you run this in the REPL, it gets pulses (a hundred in my case, even if the source is continuously sending pulses for test)

>>> p=pulseio.PulseIn(board.D13,1024,0)
>>> len(p)
100

after that p does not "grow" I have to run again p=pulseio.PulseIn(board.D13,1024,0) in the REPL
and even if I wait a long time, I get 100-120 pulses.
(the source is continuously sending pulses with variable lengths between 40 and 520 microseconds!)

Behavior

explained above.

Description

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions