Libery for HX711 sensor. This is for measureing the weigth of the keg
Hx711.h@0:52c805a049c4, 2016-02-21 (annotated)
- Committer:
- gert_lauritsen
- Date:
- Sun Feb 21 11:01:00 2016 +0000
- Revision:
- 0:52c805a049c4
A lib for hx711 SG sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gert_lauritsen | 0:52c805a049c4 | 1 | #ifndef HX711_h |
gert_lauritsen | 0:52c805a049c4 | 2 | #define HX711_h |
gert_lauritsen | 0:52c805a049c4 | 3 | //Translatet module from arduino to mbed https://codebender.cc/library/HX711#HX711.h |
gert_lauritsen | 0:52c805a049c4 | 4 | |
gert_lauritsen | 0:52c805a049c4 | 5 | #include "mbed.h" |
gert_lauritsen | 0:52c805a049c4 | 6 | |
gert_lauritsen | 0:52c805a049c4 | 7 | class HX711 |
gert_lauritsen | 0:52c805a049c4 | 8 | { |
gert_lauritsen | 0:52c805a049c4 | 9 | private: |
gert_lauritsen | 0:52c805a049c4 | 10 | DigitalOut _pin_slk; // Power Down and Serial Clock Input Pin |
gert_lauritsen | 0:52c805a049c4 | 11 | DigitalIn _pin_dout; // Serial Data Output Pin |
gert_lauritsen | 0:52c805a049c4 | 12 | char GAIN; // amplification factor |
gert_lauritsen | 0:52c805a049c4 | 13 | long OFFSET; // used for tare weight |
gert_lauritsen | 0:52c805a049c4 | 14 | float SCALE; // used to return weight in grams, kg, ounces, whatever |
gert_lauritsen | 0:52c805a049c4 | 15 | public: |
gert_lauritsen | 0:52c805a049c4 | 16 | // define clock and data pin, channel, and gain factor |
gert_lauritsen | 0:52c805a049c4 | 17 | // channel selection is made by passing the appropriate gain: 128 or 64 for channel A, 32 for channel B |
gert_lauritsen | 0:52c805a049c4 | 18 | // gain: 128 or 64 for channel A; channel B works with 32 gain factor only |
gert_lauritsen | 0:52c805a049c4 | 19 | HX711(PinName pin_dout, PinName pin_slk); |
gert_lauritsen | 0:52c805a049c4 | 20 | |
gert_lauritsen | 0:52c805a049c4 | 21 | virtual ~HX711(); |
gert_lauritsen | 0:52c805a049c4 | 22 | |
gert_lauritsen | 0:52c805a049c4 | 23 | // check if HX711 is ready |
gert_lauritsen | 0:52c805a049c4 | 24 | // from the datasheet: When output data is not ready for retrieval, digital output pin DOUT is high. Serial clock |
gert_lauritsen | 0:52c805a049c4 | 25 | // input PD_SCK should be low. When DOUT goes to low, it indicates data is ready for retrieval. |
gert_lauritsen | 0:52c805a049c4 | 26 | |
gert_lauritsen | 0:52c805a049c4 | 27 | |
gert_lauritsen | 0:52c805a049c4 | 28 | // set the gain factor; takes effect only after a call to read() |
gert_lauritsen | 0:52c805a049c4 | 29 | // channel A can be set for a 128 or 64 gain; channel B has a fixed 32 gain |
gert_lauritsen | 0:52c805a049c4 | 30 | // depending on the parameter, the channel is also set to either A or B |
gert_lauritsen | 0:52c805a049c4 | 31 | void set_gain(char gain = 128); |
gert_lauritsen | 0:52c805a049c4 | 32 | |
gert_lauritsen | 0:52c805a049c4 | 33 | // waits for the chip to be ready and returns a reading |
gert_lauritsen | 0:52c805a049c4 | 34 | long read(); |
gert_lauritsen | 0:52c805a049c4 | 35 | |
gert_lauritsen | 0:52c805a049c4 | 36 | // returns an average reading; times = how many times to read |
gert_lauritsen | 0:52c805a049c4 | 37 | long read_average(char times = 10); |
gert_lauritsen | 0:52c805a049c4 | 38 | |
gert_lauritsen | 0:52c805a049c4 | 39 | // returns (read_average() - OFFSET), that is the current value without the tare weight; times = how many readings to do |
gert_lauritsen | 0:52c805a049c4 | 40 | double get_value(char times = 1); |
gert_lauritsen | 0:52c805a049c4 | 41 | |
gert_lauritsen | 0:52c805a049c4 | 42 | // returns get_value() divided by SCALE, that is the raw value divided by a value obtained via calibration |
gert_lauritsen | 0:52c805a049c4 | 43 | // times = how many readings to do |
gert_lauritsen | 0:52c805a049c4 | 44 | float get_units(char times = 1); |
gert_lauritsen | 0:52c805a049c4 | 45 | |
gert_lauritsen | 0:52c805a049c4 | 46 | // set the OFFSET value for tare weight; times = how many times to read the tare value |
gert_lauritsen | 0:52c805a049c4 | 47 | void tare(char times = 10); |
gert_lauritsen | 0:52c805a049c4 | 48 | |
gert_lauritsen | 0:52c805a049c4 | 49 | // set the SCALE value; this value is used to convert the raw data to "human readable" data (measure units) |
gert_lauritsen | 0:52c805a049c4 | 50 | void set_scale(float scale = 1.f); |
gert_lauritsen | 0:52c805a049c4 | 51 | |
gert_lauritsen | 0:52c805a049c4 | 52 | // set OFFSET, the value that's subtracted from the actual reading (tare weight) |
gert_lauritsen | 0:52c805a049c4 | 53 | void set_offset(long offset = 0); |
gert_lauritsen | 0:52c805a049c4 | 54 | |
gert_lauritsen | 0:52c805a049c4 | 55 | // puts the chip into power down mode |
gert_lauritsen | 0:52c805a049c4 | 56 | void power_down(); |
gert_lauritsen | 0:52c805a049c4 | 57 | |
gert_lauritsen | 0:52c805a049c4 | 58 | // wakes up the chip after power down mode |
gert_lauritsen | 0:52c805a049c4 | 59 | void power_up(); |
gert_lauritsen | 0:52c805a049c4 | 60 | }; |
gert_lauritsen | 0:52c805a049c4 | 61 | |
gert_lauritsen | 0:52c805a049c4 | 62 | #endif /* HX711_h */ |