Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion examples/simple_scale/simple_scale.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <Q2HX711.h>

// 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;

Expand All @@ -10,6 +13,6 @@ void setup() {
}

void loop() {
Serial.println(hx711.read()/100.0);
Serial.println(hx711.read());
delay(500);
}
49 changes: 49 additions & 0 deletions examples/tare_and_scaling/tare_and_scaling.ino
Original file line number Diff line number Diff line change
@@ -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 <Q2HX711.h>

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]");
}
4 changes: 4 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Q2HX711 KEYWORD1 Q2HX711

readyToSend KEYWORD2
read KEYWORD2
readScaled KEYWORD2
setOffset KEYWORD2
setScale KEYWORD2
tare KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
44 changes: 44 additions & 0 deletions src/Q2HX711.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions src/Q2HX711.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */