Hi, I have tried and tried but no luck getting the PI 5 to read from the load cell. The amp works fine, i got it working with an ESP32, but not the PI.
Code:
#!/usr/bin/env python3"""Raspberry Pi 5 HX711 Load Cell TestThis Python script tests the HX711 and load cell on Raspberry Pi 5Wiring:HX711 VDD → RPi 5V (Pin 2 or 4)HX711 VCC → RPi 5V (same as VDD) HX711 GND → RPi GND (Pin 6, 9, 14, 20, 25, 30, 34, or 39)HX711 DT → RPi GPIO 20 (Pin 38)HX711 SCK → RPi GPIO 21 (Pin 40)Load Cell to HX711:Red → RED (E+)Black → BLK (E-) White → WHT (A+)Green → GRN (A-)Installation requirements (Pi 5):sudo apt updatesudo apt install python3-libgpiod"""import gpiodimport timeimport sys# Pin definitions (using GPIO numbers)HX711_DT_PIN = 20 # Data pin (Physical Pin 38)HX711_SCK_PIN = 21 # Clock pin (Physical Pin 40)# Calibration - set this to your baseline readingZERO_VALUE = -155456 # Your baseline/tare valueclass HX711: def __init__(self, dt_pin, sck_pin): self.dt_pin = dt_pin self.sck_pin = sck_pin self.chip = gpiod.Chip('gpiochip0') # Request GPIO lines self.dt_line = self.chip.get_line(self.dt_pin) self.sck_line = self.chip.get_line(self.sck_pin) # Configure pins self.dt_line.request(consumer="HX711_DT", type=gpiod.LINE_REQ_DIR_IN) self.sck_line.request(consumer="HX711_SCK", type=gpiod.LINE_REQ_DIR_OUT) # Set SCK low initially self.sck_line.set_value(0) def cleanup(self): """Clean up GPIO resources""" try: self.dt_line.release() self.sck_line.release() self.chip.close() except: pass def reset(self): """Reset the HX711 by sending 25+ clock pulses""" print("Resetting HX711...") for _ in range(30): self.sck_line.set_value(1) time.sleep(0.000001) # 1 microsecond self.sck_line.set_value(0) time.sleep(0.000001) time.sleep(0.1) def is_ready(self): """Check if HX711 is ready (DT is low)""" return self.dt_line.get_value() == 0 def read_raw(self): """Read raw value from HX711""" # Wait for HX711 to be ready (DT goes low) timeout = time.time() + 1.0 # 1 second timeout while self.dt_line.get_value() == 1 and time.time() < timeout: time.sleep(0.00001) # 10 microseconds if time.time() >= timeout: print("Timeout waiting for HX711") return 0 # Read 24 bits value = 0 for i in range(24): self.sck_line.set_value(1) time.sleep(0.000001) # 1 microsecond value = (value << 1) | self.dt_line.get_value() self.sck_line.set_value(0) time.sleep(0.000001) # Send one more pulse to set gain to 128 for next reading self.sck_line.set_value(1) time.sleep(0.000001) self.sck_line.set_value(0) time.sleep(0.000001) # Convert from 24-bit unsigned to signed if value & 0x800000: value |= 0xFF000000 # Sign extend for 32-bit signed # Convert to proper signed 32-bit integer value = value - 2**32 if value >= 2**31 else value return value def print_diagnostics(self): """Print diagnostic information""" print("\n=== HX711 Diagnostics ===") # Check pin states dt_state = "HIGH" if self.dt_line.get_value() else "LOW" print(f"DT Pin State: {dt_state}") # Test SCK control print("Testing SCK control...") self.sck_line.set_value(1) time.sleep(0.01) sck_high = "HIGH" if self.sck_line.get_value() else "LOW" print(f"SCK set HIGH, reading: {sck_high}") self.sck_line.set_value(0) time.sleep(0.01) sck_low = "HIGH" if self.sck_line.get_value() else "LOW" print(f"SCK set LOW, reading: {sck_low}") print()def main(): """Main program loop""" hx711 = None try: print("Raspberry Pi 5 HX711 Load Cell Test") print("===================================") # Initialize HX711 hx711 = HX711(HX711_DT_PIN, HX711_SCK_PIN) print(f"DT Pin: GPIO {HX711_DT_PIN}") print(f"SCK Pin: GPIO {HX711_SCK_PIN}") print(f"Zero baseline: {ZERO_VALUE}") # Reset HX711 hx711.reset() time.sleep(1) print("Starting readings...") print("Apply pressure to load cell to test response") print("Press Ctrl+C to exit") print() while True: # Check if HX711 is ready if hx711.is_ready(): # Read raw value raw_value = hx711.read_raw() # Calculate zeroed value zeroed_value = raw_value - ZERO_VALUE # Print results print(f"Raw: {raw_value} → Zeroed: {zeroed_value}", end="") # Status indicators if raw_value == -1: print(" ⚠️ All bits high - check A+/A- wiring") elif raw_value == 0: print(" ⚠️ All bits low - check connections") elif abs(zeroed_value) < 100: print(" ✅ At rest") elif zeroed_value > 100: print(" 📈 Positive pressure") elif zeroed_value < -100: print(" 📉 Negative pressure") else: print() else: print("HX711 not ready (DT high) - check power and connections") time.sleep(0.5) # Read every 500ms except KeyboardInterrupt: print("\nExiting...") except Exception as e: print(f"Error: {e}") print("Make sure you have installed: sudo apt install python3-libgpiod") print("And that you're running as root: sudo python3 hx711_test.py") finally: if hx711: hx711.cleanup()if __name__ == "__main__": main() Statistics: Posted by Steve001 — Wed Sep 03, 2025 5:16 pm