Guest User

hx711 wiring

a guest
Jan 1st, 2026
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #include <HX711.h>
  2. #include <EEPROM.h>
  3.  
  4. const byte DOUT_PIN = 2;
  5. const byte SCK_PIN = 3;
  6. const byte TARE_BUTTON_PIN = 7;
  7.  
  8. float calibrationFactor_lbs = 7050.0;
  9. const float ZERO_BAND_LBS = 0.03;
  10.  
  11. #define EEPROM_MAGIC_ADDR 0
  12. #define EEPROM_OFFSET_ADDR 1
  13. #define EEPROM_MAGIC 0x42
  14.  
  15. long storedOffset = 0;
  16.  
  17. HX711 scale;
  18.  
  19. bool tareRequested = false;
  20. unsigned long lastTareTime = 0;
  21. const unsigned long TARE_COOLDOWN_MS = 2000;
  22.  
  23. void loadOrInitOffset();
  24. void tareAndSaveOffset();
  25. void checkTareButton();
  26. float readWeightLbs();
  27. void printWeightLbs();
  28.  
  29. void setup() {
  30. Serial.begin(115200);
  31. delay(500);
  32.  
  33. pinMode(TARE_BUTTON_PIN, INPUT_PULLUP);
  34.  
  35. scale.begin(DOUT_PIN, SCK_PIN);
  36.  
  37. Serial.println(F("Initializing HX711..."));
  38. unsigned long start = millis();
  39. while (!scale.is_ready()) {
  40. if (millis() - start > 3000) {
  41. Serial.println(F("HX711 not ready after 3s, continuing anyway..."));
  42. break;
  43. }
  44. }
  45.  
  46. scale.set_scale(calibrationFactor_lbs);
  47. loadOrInitOffset();
  48. scale.set_offset(storedOffset);
  49.  
  50. Serial.println(F("Setup complete."));
  51. Serial.println(F("Press tare button to zero the scale."));
  52. }
  53.  
  54. void loop() {
  55. checkTareButton();
  56.  
  57. if (tareRequested) {
  58. unsigned long now = millis();
  59. if (now - lastTareTime > TARE_COOLDOWN_MS) {
  60. Serial.println(F("Tare button pressed, zeroing scale..."));
  61. tareAndSaveOffset();
  62. lastTareTime = now;
  63. }
  64. tareRequested = false;
  65. }
  66.  
  67. printWeightLbs();
  68. delay(500);
  69. }
  70.  
  71. void checkTareButton() {
  72. if (digitalRead(TARE_BUTTON_PIN) == LOW) {
  73. delay(50);
  74. if (digitalRead(TARE_BUTTON_PIN) == LOW) {
  75. tareRequested = true;
  76. }
  77. }
  78. }
  79.  
  80. void loadOrInitOffset() {
  81. byte magic = EEPROM.read(EEPROM_MAGIC_ADDR);
  82.  
  83. if (magic == EEPROM_MAGIC) {
  84. EEPROM.get(EEPROM_OFFSET_ADDR, storedOffset);
  85. Serial.print(F("Loaded offset from EEPROM: "));
  86. Serial.println(storedOffset);
  87. } else {
  88. Serial.println(F("No EEPROM data, performing initial tare..."));
  89. Serial.println(F("Make sure the scale is empty!"));
  90.  
  91. scale.tare(10);
  92. storedOffset = scale.get_offset();
  93.  
  94. EEPROM.put(EEPROM_OFFSET_ADDR, storedOffset);
  95. EEPROM.update(EEPROM_MAGIC_ADDR, EEPROM_MAGIC);
  96.  
  97. Serial.print(F("Initial offset saved: "));
  98. Serial.println(storedOffset);
  99. }
  100. }
  101.  
  102. void tareAndSaveOffset() {
  103. scale.tare(10);
  104. storedOffset = scale.get_offset();
  105.  
  106. EEPROM.put(EEPROM_OFFSET_ADDR, storedOffset);
  107. EEPROM.update(EEPROM_MAGIC_ADDR, EEPROM_MAGIC);
  108.  
  109. Serial.print(F("New offset saved: "));
  110. Serial.println(storedOffset);
  111. }
  112.  
  113. float readWeightLbs() {
  114. const uint8_t samples = 20;
  115. float sum = 0.0;
  116.  
  117. for (uint8_t i = 0; i < samples; i++) {
  118. checkTareButton();
  119. sum += scale.get_units(1);
  120. delay(5);
  121. }
  122.  
  123. float avg = sum / samples;
  124.  
  125. if (avg > -ZERO_BAND_LBS && avg < ZERO_BAND_LBS) {
  126. avg = 0.0;
  127. }
  128.  
  129. return avg;
  130. }
  131.  
  132. void printWeightLbs() {
  133. float pounds = readWeightLbs();
  134.  
  135. Serial.print(F("Weight: "));
  136. Serial.print(pounds, 3);
  137. Serial.println(F(" lb"));
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment