Skip to content

PulseIn accuracy? #4932

@dmcomm

Description

@dmcomm

Firmware

Adafruit CircuitPython 7.0.0-alpha.3-433-g5a4a80336 on 2021-06-25; Raspberry Pi Pico with rp2040

Code/REPL

import board
import pwmio
import pulseio
import time

pwmOut = pwmio.PWMOut(board.GP0, frequency=5000, duty_cycle=0x8000)
pulsesIn = pulseio.PulseIn(board.GP1, maxlen=100)
#GP0 and GP1 are connected by a resistor

results = []
while True:
    pulsesIn.clear()
    for i in range(20):
        while len(pulsesIn) == 0:
            pass
        results.append(pulsesIn.popleft())
    print(results, "during")
    results = []
    time.sleep(5)
    
    pulsesIn.clear()
    while len(pulsesIn) < 20:
        pass
    for i in range(20):
        results.append(pulsesIn.popleft())
    print(results, "after")
    results = []
    time.sleep(5)

Behavior

[99, 99, 99, 99, 99, 42, 99, 50, 86, 90, 62, 98, 70, 42, 78, 82, 54, 90, 30, 66] during
[99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99] after
[99, 61, 65, 37, 73, 45, 49, 53, 89, 93, 65, 37, 73, 45, 49, 85, 57, 61, 33, 37] during
[99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99] after
[99, 25, 61, 97, 37, 73, 77, 49, 85, 89, 29, 97, 69, 41, 77, 49, 85, 57, 93, 65] during
[99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99] after

Description

Doing some things while PulseIn is measuring degrades the accuracy. The above shows a particularly bad example. I'm unsure if there is a bug here, or whether this is just a case of "if you want microsecond accuracy, you'll have to be gentle with it". But if the latter, this would be a documentation request for "how do you know what to avoid?"

It's puzzling that the durations are always on the short side. That suggests a delay is getting inserted between checking the current pulse duration and starting the timer for the next one, which doesn't seem like it should happen.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions