HX711.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. *
  3. * HX711 library for Arduino
  4. * https://github.com/bogde/HX711
  5. *
  6. * MIT License
  7. * (c) 2018 Bogdan Necula
  8. *
  9. **/
  10. #ifndef HX711_h
  11. #define HX711_h
  12. #if ARDUINO >= 100
  13. #include "Arduino.h"
  14. #else
  15. #include "WProgram.h"
  16. #endif
  17. class HX711
  18. {
  19. private:
  20. byte PD_SCK; // Power Down and Serial Clock Input Pin
  21. byte DOUT; // Serial Data Output Pin
  22. byte GAIN; // amplification factor
  23. long OFFSET = 0; // used for tare weight
  24. float SCALE = 1; // used to return weight in grams, kg, ounces, whatever
  25. public:
  26. HX711();
  27. virtual ~HX711();
  28. // Initialize library with data output pin, clock input pin and gain factor.
  29. // Channel selection is made by passing the appropriate gain:
  30. // - With a gain factor of 64 or 128, channel A is selected
  31. // - With a gain factor of 32, channel B is selected
  32. // The library default is "128" (Channel A).
  33. void begin(byte dout, byte pd_sck, byte gain = 128);
  34. // Check if HX711 is ready
  35. // from the datasheet: When output data is not ready for retrieval, digital output pin DOUT is high. Serial clock
  36. // input PD_SCK should be low. When DOUT goes to low, it indicates data is ready for retrieval.
  37. bool is_ready();
  38. // Wait for the HX711 to become ready
  39. void wait_ready(unsigned long delay_ms = 0);
  40. bool wait_ready_retry(int retries = 3, unsigned long delay_ms = 0);
  41. bool wait_ready_timeout(unsigned long timeout = 1000, unsigned long delay_ms = 0);
  42. // set the gain factor; takes effect only after a call to read()
  43. // channel A can be set for a 128 or 64 gain; channel B has a fixed 32 gain
  44. // depending on the parameter, the channel is also set to either A or B
  45. void set_gain(byte gain = 128);
  46. // waits for the chip to be ready and returns a reading
  47. long read();
  48. // returns an average reading; times = how many times to read
  49. long read_average(byte times = 10);
  50. // returns (read_average() - OFFSET), that is the current value without the tare weight; times = how many readings to do
  51. double get_value(byte times = 1);
  52. // returns get_value() divided by SCALE, that is the raw value divided by a value obtained via calibration
  53. // times = how many readings to do
  54. float get_units(byte times = 1);
  55. // set the OFFSET value for tare weight; times = how many times to read the tare value
  56. void tare(byte times = 10);
  57. // set the SCALE value; this value is used to convert the raw data to "human readable" data (measure units)
  58. void set_scale(float scale = 1.f);
  59. // get the current SCALE
  60. float get_scale();
  61. // set OFFSET, the value that's subtracted from the actual reading (tare weight)
  62. void set_offset(long offset = 0);
  63. // get the current OFFSET
  64. long get_offset();
  65. // puts the chip into power down mode
  66. void power_down();
  67. // wakes up the chip after power down mode
  68. void power_up();
  69. };
  70. #endif /* HX711_h */