From 20e058a1f29692d9c64b4172a3b92184936ab969 Mon Sep 17 00:00:00 2001 From: Jacques Supcik Date: Sun, 5 Oct 2025 19:21:26 +0200 Subject: [PATCH 1/2] Add fast reading function and refactor read_channel methods for HX711 --- adafruit_hx711/hx711.py | 55 ++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/adafruit_hx711/hx711.py b/adafruit_hx711/hx711.py index c106ff8..c97a050 100644 --- a/adafruit_hx711/hx711.py +++ b/adafruit_hx711/hx711.py @@ -26,6 +26,8 @@ import time +import microcontroller + try: from typing import Union @@ -37,6 +39,33 @@ __repo__ = "https://github.com/your-repo/Adafruit_CircuitPython_HX711.git" +def read_fast( + clock_pin: digitalio.DigitalInOut, data_pin: digitalio.DigitalInOut, size: int +): + value: int = 0 # return value + assert not clock_pin.value + assert size > 0 + # The first iteration of the while loop takes longer, so to cancel + # this problem, we skip the first clock + clock_to_skip: int = 1 + # Disable interrupts just before the loop + microcontroller.disable_interrupts() + while size > 0: + if clock_to_skip <= 0: # no more skipping + clock_pin.value = True + size -= 1 + microcontroller.delay_us(1) + if clock_to_skip > 0: + clock_to_skip -= 1 # decrement skip counter and do not read + else: + value = (value << 1) | data_pin.value + clock_pin.value = False + microcontroller.delay_us(1) + # Re-enable interrupts as soon as possible + microcontroller.enable_interrupts() + return value + + class HX711: """HX711 ADC driver""" @@ -44,7 +73,9 @@ class HX711: CHAN_A_GAIN_64 = 27 CHAN_B_GAIN_32 = 26 - def __init__(self, data_pin: digitalio.DigitalInOut, clock_pin: digitalio.DigitalInOut) -> None: + def __init__( + self, data_pin: digitalio.DigitalInOut, clock_pin: digitalio.DigitalInOut + ) -> None: """ Initialize the HX711 module. @@ -78,7 +109,8 @@ def read_channel_blocking(self, chan_gain: int) -> int: :param chan_gain: Gain and channel configuration. :return: ADC value. """ - self._read_channel(chan_gain) # First, set the desired gain and discard this read + # First, set the desired gain and discard this read + self._read_channel(chan_gain) return self._read_channel(chan_gain) # Now perform the actual read def _read_channel(self, chan_gain: int) -> int: @@ -89,7 +121,9 @@ def _read_channel(self, chan_gain: int) -> int: :return: ADC value. """ return self._read_channel_raw(chan_gain) - ( - self._tare_value_b if chan_gain == self.CHAN_B_GAIN_32 else self._tare_value_a + self._tare_value_b + if chan_gain == self.CHAN_B_GAIN_32 + else self._tare_value_a ) def _read_channel_raw(self, chan_gain: int) -> int: @@ -103,18 +137,9 @@ def _read_channel_raw(self, chan_gain: int) -> int: pass # Wait until the HX711 is ready self._clock_pin.value = False - value = 0 - for _ in range(24): # Read 24 bits from DOUT - self._clock_pin.value = True - time.sleep(0.000001) # 1 microsecond delay - value = (value << 1) | self._data_pin.value - self._clock_pin.value = False - time.sleep(0.000001) # 1 microsecond delay - - # Set gain for next reading - for _ in range(chan_gain - 24): - self._clock_pin.value = True - self._clock_pin.value = False + value = read_fast(self._clock_pin, self._data_pin, chan_gain) >> ( + chan_gain - 24 + ) # Convert to 32-bit signed integer if value & 0x80_00_00: From bb1baa0b324c23491b1520a607f395eae95fc22b Mon Sep 17 00:00:00 2001 From: Jacques Supcik Date: Mon, 6 Oct 2025 00:03:17 +0200 Subject: [PATCH 2/2] fix pre-commit error --- adafruit_hx711/hx711.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/adafruit_hx711/hx711.py b/adafruit_hx711/hx711.py index c97a050..dad02d7 100644 --- a/adafruit_hx711/hx711.py +++ b/adafruit_hx711/hx711.py @@ -39,9 +39,7 @@ __repo__ = "https://github.com/your-repo/Adafruit_CircuitPython_HX711.git" -def read_fast( - clock_pin: digitalio.DigitalInOut, data_pin: digitalio.DigitalInOut, size: int -): +def read_fast(clock_pin: digitalio.DigitalInOut, data_pin: digitalio.DigitalInOut, size: int): value: int = 0 # return value assert not clock_pin.value assert size > 0 @@ -73,9 +71,7 @@ class HX711: CHAN_A_GAIN_64 = 27 CHAN_B_GAIN_32 = 26 - def __init__( - self, data_pin: digitalio.DigitalInOut, clock_pin: digitalio.DigitalInOut - ) -> None: + def __init__(self, data_pin: digitalio.DigitalInOut, clock_pin: digitalio.DigitalInOut) -> None: """ Initialize the HX711 module. @@ -121,9 +117,7 @@ def _read_channel(self, chan_gain: int) -> int: :return: ADC value. """ return self._read_channel_raw(chan_gain) - ( - self._tare_value_b - if chan_gain == self.CHAN_B_GAIN_32 - else self._tare_value_a + self._tare_value_b if chan_gain == self.CHAN_B_GAIN_32 else self._tare_value_a ) def _read_channel_raw(self, chan_gain: int) -> int: @@ -137,9 +131,7 @@ def _read_channel_raw(self, chan_gain: int) -> int: pass # Wait until the HX711 is ready self._clock_pin.value = False - value = read_fast(self._clock_pin, self._data_pin, chan_gain) >> ( - chan_gain - 24 - ) + value = read_fast(self._clock_pin, self._data_pin, chan_gain) >> (chan_gain - 24) # Convert to 32-bit signed integer if value & 0x80_00_00: