From 827b4f231148974edda6207574e71dfbd101ceec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Thing=20Andersen?= Date: Sat, 12 Nov 2016 18:50:44 +0100 Subject: [PATCH 1/2] Code to set offset and scaling, and return (averaged) values corrected for offset and scaling. --- examples/simple_scale/simple_scale.ino | 5 +- .../tare_and_scaling/tare_and_scaling.ino | 49 +++++++++++++++++++ src/Q2HX711.cpp | 44 +++++++++++++++++ src/Q2HX711.h | 6 +++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 examples/tare_and_scaling/tare_and_scaling.ino diff --git a/examples/simple_scale/simple_scale.ino b/examples/simple_scale/simple_scale.ino index f5e2e6e..a4a3a00 100644 --- a/examples/simple_scale/simple_scale.ino +++ b/examples/simple_scale/simple_scale.ino @@ -1,5 +1,8 @@ #include +// Simple example: Returns raw values from the HX711 chip. +// See the example tare_and_scaling for a more complete example. + const byte hx711_data_pin = A2; const byte hx711_clock_pin = A3; @@ -10,6 +13,6 @@ void setup() { } void loop() { - Serial.println(hx711.read()/100.0); + Serial.println(hx711.read()); delay(500); } diff --git a/examples/tare_and_scaling/tare_and_scaling.ino b/examples/tare_and_scaling/tare_and_scaling.ino new file mode 100644 index 0000000..f36d622 --- /dev/null +++ b/examples/tare_and_scaling/tare_and_scaling.ino @@ -0,0 +1,49 @@ +// Example on how to use tare(), setScale() and readScaled() to get human readable values in eg gram. +// Please see the comments in Q2HX711.cpp for further details. + +#include + +const byte hx711_data_pin = A2; +const byte hx711_clock_pin = A3; + +Q2HX711 hx711(hx711_data_pin, hx711_clock_pin); + +void setup() { + Serial.begin(9600); + + Serial.println("Waiting for chip to stabilize (1s)"); + delay(1000); + Serial.println("Taking 10 samples to set tare..."); + hx711.tare(10); + // Alternatively call setOffset(); + //hx711.setOffset(-31370); + + // Set scaling factor from raw values to wanted unit (eg gram) + // You can use findScale() to see an average of the raw values. + hx711.setScale(943.00); +} + +void loop() { + Serial.println(hx711.readScaled(), 2); + delay(500); +} + +void findScale() { + hx711.setScale(1); + Serial.println("Waiting for chip to stabilize (1s)"); + delay(1000); + Serial.println("Place a known weight on the scale."); + Serial.println("Now sampling 10x240 times..."); + for (byte count = 0; count < 10; count++) { + float sum = 0; + for (byte i = 0; i < 240; i++) { + sum += hx711.readScaled(); + } + sum /= 240; + Serial.print("Value #"); + Serial.print(count); + Serial.print(" is "); + Serial.println(sum); + } + Serial.println("Now you can call setScale([found value] / [known weight]"); +} diff --git a/src/Q2HX711.cpp b/src/Q2HX711.cpp index 1c82bb5..5f81c8c 100644 --- a/src/Q2HX711.cpp +++ b/src/Q2HX711.cpp @@ -5,6 +5,8 @@ Q2HX711::Q2HX711(byte output_pin, byte clock_pin) { CLOCK_PIN = clock_pin; OUT_PIN = output_pin; GAIN = 1; + OFFSET = 0; + SCALE = 1; pinMode(CLOCK_PIN, OUTPUT); pinMode(OUT_PIN, INPUT); } @@ -51,3 +53,45 @@ long Q2HX711::read() { data[2] ^= 0x80; return ((uint32_t) data[2] << 16) | ((uint32_t) data[1] << 8) | (uint32_t) data[0]; } + +/** + * Reads a number of values from the chip and calculates the average. + * The value is adjusted for offset and scale before it is returned. + */ +float Q2HX711::readScaled(byte samples) { + long sum = 0; + for (byte i = 0; i < samples; i++) { + sum += read(); + } + return (((long) sum / samples) - OFFSET) / SCALE; +} + +/** + * Sets the raw offset from the average of a number of current reading. + */ +void Q2HX711::tare(byte samples) { + long sum = 0; + for (byte i = 0; i < samples; i++) { + sum += read(); + } + OFFSET = sum / samples; +} + +/** + * Sets a new raw offset from 0. + * + * Example: If read() returns -43263 with no load on the scale, you should call setOffset(-43263). + */ +void Q2HX711::setOffset(const long offset) { + OFFSET = offset; +} + +/** + * Sets a new scaling factor for readScaled. + * + * Example: If read() returns 6500123 (after offset correction) with a 100kg load on the scale, + * you could call setScale(6500.123) to make readScaled() return the weight in kilograms. + */ +void Q2HX711::setScale(const float scale) { + SCALE = scale; +} diff --git a/src/Q2HX711.h b/src/Q2HX711.h index a8a4c4b..c4cdbf8 100644 --- a/src/Q2HX711.h +++ b/src/Q2HX711.h @@ -8,12 +8,18 @@ class Q2HX711 byte CLOCK_PIN; byte OUT_PIN; byte GAIN; + long OFFSET; + float SCALE; void setGain(byte gain = 128); public: Q2HX711(byte output_pin, byte clock_pin); virtual ~Q2HX711(); bool readyToSend(); long read(); + float readScaled(byte samples = 1); + void tare(byte samples = 1); + void setOffset(const long offset); + void setScale(const float scale); }; #endif /* Q2HX711_h */ From 782356338930361a86c5fa02e7f88483b617715e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Thing=20Andersen?= Date: Sat, 12 Nov 2016 19:58:33 +0100 Subject: [PATCH 2/2] Updated documentationZ --- README.md | 4 ++++ keywords.txt | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/README.md b/README.md index 477dbfc..d356e0c 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ Function | Description ------------- | ------------- **read** | Returns a long integer that is the current value of the HX711 **readyToSend** | Returns a boolean indicating if the HX711 is prepared to send data. +**readScaled** | Returns a float, the current value adjusted for offset and scale. +**setOffset** | Sets a new raw offset to the specified value. +**tare** | Sets the raw offset from the average of a number of current reading. +**setScale** | Sets a new scaling factor for readScaled. ## Example diff --git a/keywords.txt b/keywords.txt index 794c227..498bd45 100644 --- a/keywords.txt +++ b/keywords.txt @@ -14,6 +14,10 @@ Q2HX711 KEYWORD1 Q2HX711 readyToSend KEYWORD2 read KEYWORD2 +readScaled KEYWORD2 +setOffset KEYWORD2 +setScale KEYWORD2 +tare KEYWORD2 ####################################### # Constants (LITERAL1)