Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. //-------------------------------------------------------------------------------------
  2. // HX711_ADC.h
  3. // Arduino master library for HX711 24-Bit Analog-to-Digital Converter for Weigh Scales
  4. // Olav Kallhovd sept2017
  5. // Tested with : HX711 asian module on channel A and YZC-133 3kg load cell
  6. // Tested with MCU : Arduino Nano, ESP8266
  7. //-------------------------------------------------------------------------------------
  8. // This is an example sketch on how to use this library
  9. // Settling time (number of samples) and data filtering can be adjusted in the config.h file
  10.  
  11. // This example shows how to calibrate the load cell and optionally save the calibration
  12. // value to EEPROM, and also how to change the value.
  13. // The value can later be fetched from EEPROM in your project sketch.
  14.  
  15. #include <HX711_ADC.h>
  16. #include <EEPROM.h>
  17.  
  18. //HX711 constructor (dout pin, sck pin):
  19. HX711_ADC LoadCell(4, 5);
  20.  
  21. int eepromAdress = 0;
  22.  
  23. long t;
  24.  
  25. void calibrate() {
  26. Serial.println("***");
  27. Serial.println("Start calibration:");
  28. Serial.println("It is assumed that the mcu was started with no load applied to the load cell.");
  29. Serial.println("Now, place your known mass on the loadcell,");
  30. Serial.println("then send the weight of this mass (i.e. 100.0) from serial monitor.");
  31. float m = 0;
  32. boolean f = 0;
  33. while (f == 0) {
  34. LoadCell.update();
  35. if (Serial.available() > 0) {
  36. m = Serial.parseFloat();
  37. if (m != 0) {
  38. Serial.print("Known mass is: ");
  39. Serial.println(m);
  40. f = 1;
  41. }
  42. else {
  43. Serial.println("Invalid value");
  44. }
  45. }
  46. }
  47. float c = LoadCell.getData() / m;
  48. LoadCell.setCalFactor(c);
  49. Serial.print("Calculated calibration value is: ");
  50. Serial.print(c);
  51. Serial.println(", use this in your project sketch");
  52. f = 0;
  53. Serial.print("Save this value to EEPROM adress ");
  54. Serial.print(eepromAdress);
  55. Serial.println("? y/n");
  56. while (f == 0) {
  57. if (Serial.available() > 0) {
  58. char inByte = Serial.read();
  59. if (inByte == 'y') {
  60. #if defined(ESP8266)
  61. EEPROM.begin(512);
  62. #endif
  63. EEPROM.put(eepromAdress, c);
  64. #if defined(ESP8266)
  65. EEPROM.commit();
  66. #endif
  67. EEPROM.get(eepromAdress, c);
  68. Serial.print("Value ");
  69. Serial.print(c);
  70. Serial.print(" saved to EEPROM address: ");
  71. Serial.println(eepromAdress);
  72. f = 1;
  73.  
  74. }
  75. else if (inByte == 'n') {
  76. Serial.println("Value not saved to EEPROM");
  77. f = 1;
  78. }
  79. }
  80. }
  81. Serial.println("End calibration");
  82. Serial.println("For manual edit, send 'c' from serial monitor");
  83. Serial.println("***");
  84. }
  85.  
  86. void changeSavedCalFactor() {
  87. float c = LoadCell.getCalFactor();
  88. boolean f = 0;
  89. Serial.println("***");
  90. Serial.print("Current value is: ");
  91. Serial.println(c);
  92. Serial.println("Now, send the new value from serial monitor, i.e. 696.0");
  93. while (f == 0) {
  94. if (Serial.available() > 0) {
  95. c = Serial.parseFloat();
  96. if (c != 0) {
  97. Serial.print("New calibration value is: ");
  98. Serial.println(c);
  99. LoadCell.setCalFactor(c);
  100. f = 1;
  101. }
  102. else {
  103. Serial.println("Invalid value, exit");
  104. return;
  105. }
  106. }
  107. }
  108. f = 0;
  109. Serial.print("Save this value to EEPROM adress ");
  110. Serial.print(eepromAdress);
  111. Serial.println("? y/n");
  112. while (f == 0) {
  113. if (Serial.available() > 0) {
  114. char inByte = Serial.read();
  115. if (inByte == 'y') {
  116. #if defined(ESP8266)
  117. EEPROM.begin(512);
  118. #endif
  119. EEPROM.put(eepromAdress, c);
  120. #if defined(ESP8266)
  121. EEPROM.commit();
  122. #endif
  123. EEPROM.get(eepromAdress, c);
  124. Serial.print("Value ");
  125. Serial.print(c);
  126. Serial.print(" saved to EEPROM address: ");
  127. Serial.println(eepromAdress);
  128. f = 1;
  129. }
  130. else if (inByte == 'n') {
  131. Serial.println("Value not saved to EEPROM");
  132. f = 1;
  133. }
  134. }
  135. }
  136. Serial.println("End change calibration value");
  137. Serial.println("***");
  138. }
  139.  
  140. void setup() {
  141. Serial.begin(9600); delay(10);
  142. Serial.println();
  143. Serial.println("Starting...");
  144. LoadCell.begin();
  145. long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time
  146. LoadCell.start(stabilisingtime);
  147. if (LoadCell.getTareTimeoutFlag()) {
  148. Serial.println("Tare timeout, check MCU>HX711 wiring and pin designations");
  149. }
  150. else {
  151. LoadCell.setCalFactor(1.0); // user set calibration value (float)
  152. Serial.println("Startup + tare is complete");
  153. }
  154. while (!LoadCell.update());
  155. calibrate();
  156. }
  157. void loop() {
  158. //update() should be called at least as often as HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS
  159. //longer delay in sketch will reduce effective sample rate (be carefull with delay() in the loop)
  160. LoadCell.update();
  161.  
  162. //get smoothed value from the data set
  163. if (millis() > t + 250) {
  164. float i = LoadCell.getData();
  165. Serial.print("Load_cell output val: ");
  166. Serial.println(i);
  167. t = millis();
  168. }
  169.  
  170. //receive from serial terminal
  171. if (Serial.available() > 0) {
  172. float i;
  173. char inByte = Serial.read();
  174. if (inByte == 't') LoadCell.tareNoDelay();
  175. else if (inByte == 'c') changeSavedCalFactor();
  176. }
  177.  
  178. //check if last tare operation is complete
  179. if (LoadCell.getTareStatus() == true) {
  180. Serial.println("Tare complete");
  181. }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement