I am using PMS sensor with the environment of micropython with thonny and using the RP pico W as my controller and I am just trying to receive the data from the sensor and so when I disconnect the sensor, it still logs the data for like 7-8 iterations and this happens depending upon the sleep time as well.

For example, when every iteration runs after a delay of 1 s, it disconnects and the terminal shows no data as soon as I switch the power off from the sensor but when I change it to a delay of 5 or greater than 10 sec, the iterations start to increase as soon as the sensor is disconnected. I am a beginner working with RP PICO W So any help would be appreciated.

Tried with a delay of 1s it works fine but as I increase the delay, the terminal starts showing data for 7-8 iterations depending upon the delay using time.sleep() function although the sensor is disconnected.

Here is how I am reading data after validation checks for frame header and checksum match:

    while True:
            if uart.any():
                data = uart.read(32)
                if data:
                    pmData, isValid = parseData(data)
                    if isValid:
                        print(f"PM 1.0: {pmData['pm1_0']}, PM 2.5: {pmData['pm2_5']}, PM 10: {pmData['pm10']}")
1

There are 1 answers

0
Clifford On

With reference to https://docs.micropython.org/en/latest/library/machine.UART.html#machine-uart

UART.init has an argument to set the length of the Rx buffer, it is not stated what the default is. The UART will be serviced asynchronously (in interrupt) and received data will be placed in a FIFO buffer. The buffer is what you read with UART.read, it does not read the UART directly.

UART.any tests whether the buffer is not empty.

UART.read( 32 ) will read upto 32 characters from the buffer. If there are fewer than 32 characters, it will return after a timeout (also defined by UART.init).

If the sensor outputs asynchronously, then during any delay, data will be buffered. A delay is probably ill-advised for asynchronous (rather than polled) sensor data. You should retrieve data as it becomes available in real-time.