WEIGHT_HX711.ino 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Description: Use WEIGHT Unit to read the analog value returned by the pressure sensor,
  3. convert it into intuitive weight data and send it to M5Core,Press ButtonA to calibrate
  4. */
  5. #include "HX711.h"
  6. #include<M5Stack.h>
  7. // HX711 circuit wiring
  8. const int LOADCELL_DOUT_PIN = 36;
  9. const int LOADCELL_SCK_PIN = 26;
  10. HX711 scale;
  11. const long LOADCELL_OFFSET = 50682624;
  12. const long LOADCELL_DIVIDER = 5895655;
  13. void setup() {
  14. M5.begin();
  15. M5.Power.begin();
  16. Serial.begin(115200);
  17. Serial.println("Initializing the scale");
  18. M5.Lcd.setCursor(80,100,4);
  19. M5.Lcd.print("Initializing......");
  20. // Initialize library with data output pin, clock input pin and gain factor.
  21. // Channel selection is made by passing the appropriate gain:
  22. // - With a gain factor of 64 or 128, channel A is selected
  23. // - With a gain factor of 32, channel B is selected
  24. // By omitting the gain factor parameter, the library
  25. // default "128" (Channel A) is used here.
  26. scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  27. scale.set_scale(LOADCELL_DIVIDER);
  28. scale.set_offset(LOADCELL_OFFSET);
  29. Serial.println("Before setting up the scale:");
  30. Serial.print("read: \t\t");
  31. Serial.println(scale.read()); // print a raw reading from the ADC
  32. Serial.print("read average: \t\t");
  33. Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
  34. Serial.print("get value: \t\t");
  35. Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
  36. Serial.print("get units: \t\t");
  37. Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
  38. // by the SCALE parameter (not set yet)
  39. scale.set_scale(61.2f); // this value is obtained by calibrating the scale with known weights; see the README for details
  40. scale.tare(); // reset the scale to 0
  41. Serial.println("After setting up the scale:");
  42. Serial.print("read: \t\t");
  43. Serial.println(scale.read()); // print a raw reading from the ADC
  44. Serial.print("read average: \t\t");
  45. Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
  46. Serial.print("get value: \t\t");
  47. Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
  48. Serial.print("get units: \t\t");
  49. Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
  50. // by the SCALE parameter set with set_scale
  51. Serial.println("Readings:");
  52. M5.Lcd.setCursor(55,180,2);
  53. M5.Lcd.print("pls connect the Weight Unit to PortB");
  54. M5.Lcd.setCursor(70,210,2);
  55. M5.Lcd.print("Click button A to calibration");
  56. }
  57. void loop() {
  58. if (M5.BtnA.wasPressed()) {
  59. scale.set_offset(LOADCELL_OFFSET + scale.read());
  60. scale.set_scale(61.2f);
  61. scale.tare();
  62. }
  63. M5.update();
  64. int weight = scale.get_units(5);
  65. M5.Lcd.setCursor(40,30,4);
  66. M5.Lcd.fillRect(0, 30, 320, 30, TFT_BLACK);
  67. M5.Lcd.printf("%1d g", weight);
  68. M5.Lcd.fillRect(0, 80, 320, 80, TFT_BLACK);
  69. M5.Lcd.fillRect(0, 80, weight*0.16, 80, TFT_BLUE);
  70. Serial.print("average:\t");
  71. Serial.println(weight, 1);
  72. }