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
18 changes: 12 additions & 6 deletions HX711.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
void yield(void) {};
#endif

HX711::HX711(byte dout, byte pd_sck, byte gain) {
HX711::HX711(byte dout, byte pd_sck, byte gain) :
OFFSET(0), SCALE(1.0) {
begin(dout, pd_sck, gain);
}

HX711::HX711() {
HX711::HX711() :
GAIN(1), PD_SCK(-1), DOUT(-1), OFFSET(0), SCALE(1.0) {
}

HX711::~HX711() {
Expand Down Expand Up @@ -45,7 +47,7 @@ void HX711::set_gain(byte gain) {
}

digitalWrite(PD_SCK, LOW);
read();
read_average(1 + 6); //6 times is invalid.
}

long HX711::read() {
Expand Down Expand Up @@ -95,16 +97,16 @@ long HX711::read_average(byte times) {
return sum / times;
}

double HX711::get_value(byte times) {
long HX711::get_value(byte times) {
return read_average(times) - OFFSET;
}

float HX711::get_units(byte times) {
return get_value(times) / SCALE;
return (float) get_value(times) / SCALE;
}

void HX711::tare(byte times) {
double sum = read_average(times);
long sum = read_average(times);
set_offset(sum);
}

Expand All @@ -130,5 +132,9 @@ void HX711::power_down() {
}

void HX711::power_up() {
long dump = 0;
digitalWrite(PD_SCK, LOW);
//When power up, default gain is 128, channel A.
//rate is 10Hz, so 400ms is 5 times.
dump = read_average(6); //5 times is invalid.
}
6 changes: 3 additions & 3 deletions HX711.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class HX711
byte PD_SCK; // Power Down and Serial Clock Input Pin
byte DOUT; // Serial Data Output Pin
byte GAIN; // amplification factor
long OFFSET = 0; // used for tare weight
float SCALE = 1; // used to return weight in grams, kg, ounces, whatever
long OFFSET; // used for tare weight
float SCALE; // used to return weight in grams, kg, ounces, whatever

public:
// define clock and data pin, channel, and gain factor
Expand Down Expand Up @@ -46,7 +46,7 @@ class HX711
long read_average(byte times = 10);

// returns (read_average() - OFFSET), that is the current value without the tare weight; times = how many readings to do
double get_value(byte times = 1);
long get_value(byte times = 1);

// returns get_value() divided by SCALE, that is the raw value divided by a value obtained via calibration
// times = how many readings to do
Expand Down
107 changes: 107 additions & 0 deletions examples/HX711SerialDemo/HX711SerialDemo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "HX711.h"

HX711 scale;
void loop_test_hx711() {
int which = 0;
uint32_t preMillis = 0L;
while (which < 5) {
//Power cycle takes around 400ms so only do so if our test rate is greater than 500ms
if ((millis() - preMillis) >= 5000) {
switch (which) {
case 0:
case 2:
case 4:
scale.power_down();
Serial.print(F("powerDownScale\n"));
delay(1000);
Serial.print(F("powerUpScale\n"));
scale.power_up();
break;
case 1:
scale.set_gain(128);
Serial.print(F("set_gain=128\n"));
break;
case 3:
scale.set_gain(64);
Serial.print(F("set_gain=64\n"));
break;
default:
which = 0;
break;
}
preMillis = millis();
which++;
}

float currentReading = 0;
//Print raw reading
long rawReading = scale.read();
Serial.print(millis());
Serial.print(F(","));
Serial.print(rawReading, HEX);
Serial.print(F(",\t"));
//Take average of readings with calibration and tare taken into account
currentReading = scale.get_units(1);
Serial.print(millis());
Serial.print(F(","));
//Print calibrated reading
Serial.print(currentReading, 3);
Serial.println();
}
Serial.println(F("Loop test is done!"));
}
void setup() {
Serial.begin(38400);
Serial.println("HX711 Demo");
Serial.println("Initializing the scale");
// parameter "gain" is ommited; the default value 128 is used by the library
// HX711.DOUT - pin #A1
// HX711.PD_SCK - pin #A0
scale.begin(A1, A0);
loop_test_hx711();

Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC

Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC

Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)

Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)

scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0

Serial.println("After setting up the scale:");

Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC

Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC

Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()

Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale

Serial.println("Readings:");
}

void loop() {
Serial.print("one reading:\t");
Serial.print(scale.get_units(), 1);
Serial.print("\t| average:\t");
Serial.println(scale.get_units(10), 1);

scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();
}