Hello everyone,
I'm facing a challenge on my Raspberry Pi 5 and would appreciate some expert advice on the best practice for accurate signal generation.
My Core Requirement:
I need to generate a precise number of square waves (n) on a GPIO pin. Both the number of waves and their period are variable. The period needs to change dynamically, typically within a range of 20ms to 120ms.
What I Have Tried:
time.sleep() Method: My first approach was to manually toggle a GPIO pin in a loop using time.sleep() to control the high and low phases of the wave. As expected, this method proved to be highly inaccurate. The timing jitter from the OS scheduler was significant, making it completely unsuitable for my needs.
lgpio Library Waveforms: I then moved to what I believe is the correct approach: using the lgpio library's hardware-timed waveform feature (tx_wave). This seems perfect for offloading the timing to the hardware. However, I've become stuck on a crucial point: how to reliably determine when the waveform transmission has completed.
My attempts to use sbc.tx_busy() have been problematic. I discovered that constants like sbc.LG_TX_WAVE don't exist in my library version, and even when I tried using its supposed integer equivalent (2), I received a 'bad tx type' error, likely due to library conflicts I have since resolved. My main concern is that without a reliable, hardware-based method to check for completion, I risk freeing the GPIO pin prematurely or having to rely on an inaccurate time.sleep(calculated_duration), which defeats the purpose of using hardware timing in the first place.
My Question:
What is the most robust and hardware-accurate method on a Raspberry Pi 5 to generate a stream of 'n' square waves, where the period of each wave is a variable set at runtime (between 20ms and 120ms)?
Is lgpio's tx_wave the right tool, and if so, what is the definitive, version-agnostic way to block or wait until the transmission is finished? Or is there a better, more direct hardware-level approach for this specific task?
Thank you for any guidance you can provide!
I'm facing a challenge on my Raspberry Pi 5 and would appreciate some expert advice on the best practice for accurate signal generation.
My Core Requirement:
I need to generate a precise number of square waves (n) on a GPIO pin. Both the number of waves and their period are variable. The period needs to change dynamically, typically within a range of 20ms to 120ms.
What I Have Tried:
time.sleep() Method: My first approach was to manually toggle a GPIO pin in a loop using time.sleep() to control the high and low phases of the wave. As expected, this method proved to be highly inaccurate. The timing jitter from the OS scheduler was significant, making it completely unsuitable for my needs.
lgpio Library Waveforms: I then moved to what I believe is the correct approach: using the lgpio library's hardware-timed waveform feature (tx_wave). This seems perfect for offloading the timing to the hardware. However, I've become stuck on a crucial point: how to reliably determine when the waveform transmission has completed.
My attempts to use sbc.tx_busy() have been problematic. I discovered that constants like sbc.LG_TX_WAVE don't exist in my library version, and even when I tried using its supposed integer equivalent (2), I received a 'bad tx type' error, likely due to library conflicts I have since resolved. My main concern is that without a reliable, hardware-based method to check for completion, I risk freeing the GPIO pin prematurely or having to rely on an inaccurate time.sleep(calculated_duration), which defeats the purpose of using hardware timing in the first place.
My Question:
What is the most robust and hardware-accurate method on a Raspberry Pi 5 to generate a stream of 'n' square waves, where the period of each wave is a variable set at runtime (between 20ms and 120ms)?
Is lgpio's tx_wave the right tool, and if so, what is the definitive, version-agnostic way to block or wait until the transmission is finished? Or is there a better, more direct hardware-level approach for this specific task?
Thank you for any guidance you can provide!
Statistics: Posted by wanghuisust — Thu Jun 19, 2025 3:06 am