Skip to content

Commit

Permalink
Initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
olkal committed Oct 7, 2017
1 parent d05b914 commit 79244db
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 156 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@

This is an Arduino library for the HX711 24-bit ADC for weight scales.
Data retrieval from the HX711 is done without halting the mcu, also on the 10SPS rate setting. Multiple HX711's can perform conversions simultaneously.
The is an Arduino library for the HX711 24-bit ADC for weight scales.
Data retrieval from the HX711 is done without halting the mcu, also on the 10SPS rate setting and with Multiple HX711's performing conversions simultaneously.

"Moving average" method from a rolling data set combined with removal of high/low outliers is used for filtering and smoothing the retrieved value.
Filtering and smoothing: "Moving average" method from a rolling data set combined with removal of high/low outliers is used for the retrieved value.

Selectable values in the .h file:
- Moving average data set of 4, 8, 16, 32, 64 or 128 samples (default:16).
- Ignore high outlier; one sample is added to the data set, the peak high value of all samples in the data set is ignored (default:1)
- Ignore low outlier; one sample is added to the data set, the peak low value of all samples in the data set is ignored (default:1)

Caution: using a high number of samples will smooth the output value nicely but will also increase settling and start-up/tare time (but not response time). It will also eat chunks of your memory.
Caution: using a high number of samples will smooth the output value nicely but will also increase settling time and start-up/tare time (but not response time). It will also eat some memory.

The HX711 sample rate can be set to 10SPS or 80SPS (samples per second) by pulling pin 15 high (80SPS) or low (10SPS), ref HX711 data sheet.
On fabricated modules there is usually a solder jumper on the PCB for pin 15 high/low. The rate setting can be checked by measuring the voltage on pin 15.
ADC noise is worst on the 80SPS rate. Unless very quick settling time is required, 10SPS should be the best sample rate for most applications.

Start up and tare; from start-up/reset, the tare function seems to be more accurate if called after a "pre-warm-up" period running conversions continuously for a few seconds. See example files.
Start up and tare: from start-up/reset, the tare function seems to be more accurate if called after a "pre-warm-up" period running conversions continuously for a few seconds. See example files.

Hardware and ADC noise:
Wires between HX711 and load cell should be twisted and as kept as short as possible.
Most available HX711 modules seems to follow the reference design, but be aware that some modules are poorly designed with under-sized capacitors, and very noisy readings.
Most available HX711 modules seems to follow the reference design, but be aware that some modules are poorly designed with under-sized capacitors, and noisy readings.
The Sparkfun module seems to differ from most other available modules as it has some additional components for noise reduction.

83 changes: 50 additions & 33 deletions examples/Calibrate/Calibrate.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,70 @@
// Tested with MCU : Arduino Nano
//-------------------------------------------------------------------------------------
/* This is an example sketch on how to find correct calibration factor for your HX711:
* - Power up the scale and open Arduino serial terminal
* - After stabelizing and tare is complete, put a known weight on the load cell
* - Observe values on serial terminal
* - Adjust the calibration factor until output value is same as your known weight:
* - Sending 'l' from the serial terminal decrease factor by 1.0
* - Sending 'L' from the serial terminal decrease factor by 10.0
* - Sending 'h' from the serial terminal increase factor by 1.0
* - Sending 'H' from the serial terminal increase factor by 10.0
* - Observe and note the value of the new calibration factor
* - Use this new calibration factor in your sketch
*/
- Power up the scale and open Arduino serial terminal
- After stabelizing and tare is complete, put a known weight on the load cell
- Observe values on serial terminal
- Adjust the calibration factor until output value is same as your known weight:
- Sending 'l' from the serial terminal decrease factor by 1.0
- Sending 'L' from the serial terminal decrease factor by 10.0
- Sending 'h' from the serial terminal increase factor by 1.0
- Sending 'H' from the serial terminal increase factor by 10.0
- Sending 't' from the serial terminal call tare function
- Observe and note the value of the new calibration factor
- Use this new calibration factor in your sketch
*/

#include <HX711_ADC.h>

//HX711 constructor (dout pin, sck pin, gain/port)
HX711_ADC LdCell(A0, A1, 128);
//HX711 constructor (dout pin, sck pin)
HX711_ADC LoadCell(A0, A1);

long t;

void setup() {
Serial.begin(9600);
Serial.println("Wait...");
long stabilisingtime = 4000; // tare preciscion can be improved by adding a few seconds of stabilising time
LdCell.start(stabilisingtime);
LdCell.setCalFactor(696.0); // user set calibration factor (float)
LoadCell.begin();
long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time
LoadCell.start(stabilisingtime);
LoadCell.setCalFactor(696.0); // user set calibration factor (float)
Serial.println("Startup + tare is complete");
}

void loop() {
//send
//update() should be called at least as often as HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS
//longer delay in scetch will reduce effective sample rate (be carefull with delay() in loop)
LoadCell.update();

//get smoothed value from data set + current calibration factor
if (millis() > t + 250) {
float i = LoadCell.getData();
float v = LoadCell.getCalFactor();
Serial.print("Load_cell output val: ");
Serial.print(i);
Serial.print(" Load_cell calFactor: ");
Serial.println(v);
t = millis();
}

//receive from serial terminal
if (Serial.available() > 0) {
float i;
char inByte = Serial.read();
if(inByte == 'l') i = -1.0;
else if(inByte == 'L') i = -10.0;
else if(inByte == 'h') i = 1.0;
else if(inByte == 'H') i = 10.0;
if(i != 0) {
float v = LdCell.getCalFactor() + i;
LdCell.setCalFactor(v);
if (inByte == 'l') i = -1.0;
else if (inByte == 'L') i = -10.0;
else if (inByte == 'h') i = 1.0;
else if (inByte == 'H') i = 10.0;
else if (inByte == 't') LoadCell.tareNoDelay();
if (i != 't') {
float v = LoadCell.getCalFactor() + i;
LoadCell.setCalFactor(v);
}
}
//.getData() should be called at least as often as HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS
float i = LdCell.getData();
float v = LdCell.getCalFactor();
Serial.print("Ld_cell output val: ");
Serial.print(i);
Serial.print(" Ld_cell calFactor: ");
Serial.println(v);
delay(5);
}

//check if last tare operation is complete
if (LoadCell.getTareStatus() == true) {
Serial.println("Tare complete");
}

}
52 changes: 36 additions & 16 deletions examples/Read_1x_load_cell/Read_1x_load_cell.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,49 @@
// Tested with : HX711 asian module on channel A and YZC-133 3kg load cell
// Tested with MCU : Arduino Nano
//-------------------------------------------------------------------------------------
// This is an example sketch on how to use the library.
// Settling time and data filtering can be adjusted in the HX711_ADC.h file
// This is an example sketch on how to use this library
// Settling time (number of samples) and data filtering can be adjusted in the HX711_ADC.h file

#include <HX711_ADC.h>

//HX711 constructor (dout pin, sck pin, gain/port)
HX711_ADC LdCell(A0, A1, 128);
//HX711 constructor (dout pin, sck pin)
HX711_ADC LoadCell(A0, A1);

long t;

void setup() {
Serial.begin(9600);
long t = millis();
long stabilisingtime = 4000; // tare precision can be improved by adding a few seconds of stabilising time
LdCell.start(stabilisingtime);
LdCell.setCalFactor(696.0); // user set calibration factor (float)
Serial.print("Startup + tare time:");
Serial.println(millis() - t);
Serial.println("Wait...");
LoadCell.begin();
long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time
LoadCell.start(stabilisingtime);
LoadCell.setCalFactor(696.0); // user set calibration factor (float)
Serial.println("Startup + tare is complete");
}

void loop() {
//.getData should be called at least as often as HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS
float i = LdCell.getData();
Serial.print("Ld_cell_1: ");
Serial.println(i);
delay(5);
}
//update() should be called at least as often as HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS
//longer delay in scetch will reduce effective sample rate (be carefull with delay() in loop)
LoadCell.update();

//get smoothed value from data set + current calibration factor
if (millis() > t + 250) {
float i = LoadCell.getData();
Serial.print("Load_cell output val: ");
Serial.println(i);
t = millis();
}

//receive from serial terminal
if (Serial.available() > 0) {
float i;
char inByte = Serial.read();
if (inByte == 't') LoadCell.tareNoDelay();
}

//check if last tare operation is complete
if (LoadCell.getTareStatus() == true) {
Serial.println("Tare complete");
}

}
81 changes: 54 additions & 27 deletions examples/Read_2x_load_cell/Read_2x_load_cell.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,70 @@
// HX711_ADC.h
// Arduino master library for HX711 24-Bit Analog-to-Digital Converter for Weigh Scales
// Olav Kallhovd sept2017
// Tested with : 2x HX711 asian module on channel A and 2x YZC-133 3kg load cell
// Tested with : HX711 asian module on channel A and YZC-133 3kg load cell
// Tested with MCU : Arduino Nano
//-------------------------------------------------------------------------------------
// This is an example sketch on how to use two HX711 modules with simultanious operation.
// Settling time and data filtering can be adjusted in the HX711_ADC.h file
// This is an example sketch on how to use this library for two ore more HX711 modules
// Settling time (number of samples) and data filtering can be adjusted in the HX711_ADC.h file

#include <HX711_ADC.h>

//HX711 constructor (dout pin, sck pin, gain/port)
HX711_ADC LdCell_1(A0, A1, 128);
HX711_ADC LdCell_2(A2, A3, 128);
//HX711 constructor (dout pin, sck pin)
HX711_ADC LoadCell_1(A2, A3); //HX711 1
HX711_ADC LoadCell_2(A0, A1); //HX711 2

long t;

void setup() {
Serial.begin(9600);
long t = millis();
byte ldcells = 2; //number of load cells
byte ldcell_1_rdy = 0;
byte ldcell_2_rdy = 0;
long stabilisingtime = 4000; // tare preciscion can be improved by adding a few seconds of stabilising time
while((ldcell_1_rdy + ldcell_2_rdy) < ldcells) { //run startup, stabilization and tare, both modules
ldcell_1_rdy = LdCell_1.startMulti(stabilisingtime);
ldcell_2_rdy = LdCell_2.startMulti(stabilisingtime);
Serial.println("Wait...");
LoadCell_1.begin();
LoadCell_2.begin();
long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time
byte loadcell_1_rdy = 0;
byte loadcell_2_rdy = 0;
while ((loadcell_1_rdy + loadcell_2_rdy) < 2) { //run startup, stabilization and tare, both modules simultaniously
if (!loadcell_1_rdy) loadcell_1_rdy = LoadCell_1.startMultiple(stabilisingtime);
if (!loadcell_2_rdy) loadcell_2_rdy = LoadCell_2.startMultiple(stabilisingtime);
}
LdCell_1.setCalFactor(696.0); // user set calibration factor (float)
LdCell_2.setCalFactor(792.0); // user set calibration factor (float)
Serial.print("Startup + tare time:");
Serial.println(millis() - t);
LoadCell_1.setCalFactor(696.0); // user set calibration factor (float)
LoadCell_2.setCalFactor(733.0); // user set calibration factor (float)
Serial.println("Startup + tare is complete");
}

void loop() {
//.getData should be called at least as often as HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS
float i = LdCell_1.getData();
float k = LdCell_2.getData();
Serial.print("Ld_cell_1:");
Serial.print(i);
Serial.print(" Ld_cell_2:");
Serial.println(k);
delay(5);
}
//update() should be called at least as often as HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS
//longer delay in scetch will reduce effective sample rate (be carefull with delay() in loop)
LoadCell_1.update();
LoadCell_2.update();

//get smoothed value from data set + current calibration factor
if (millis() > t + 250) {
float a = LoadCell_1.getData();
float b = LoadCell_2.getData();
Serial.print("Load_cell 1 output val: ");
Serial.print(a);
Serial.print(" Load_cell 2 output val: ");
Serial.println(b);
t = millis();
}

//receive from serial terminal
if (Serial.available() > 0) {
float i;
char inByte = Serial.read();
if (inByte == 't') {
LoadCell_1.tareNoDelay();
LoadCell_2.tareNoDelay();
}
}

//check if last tare operation is complete
if (LoadCell_1.getTareStatus() == true) {
Serial.println("Tare load cell 1 complete");
}
if (LoadCell_2.getTareStatus() == true) {
Serial.println("Tare load cell 2 complete");
}

}
9 changes: 6 additions & 3 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ HX711_ADC KEYWORD1
# Methods and Functions (KEYWORD2)
#######################################

setGain KEYWORD2
begin KEYWORD2
start KEYWORD2
startMultiple KEYWORD2
setGain KEYWORD2
tare KEYWORD2
tareF KEYWORD2
startMulti KEYWORD2
taretareNoDelay KEYWORD2
setCalFactor KEYWORD2
getCalFactor KEYWORD2
getData KEYWORD2
getSingleConversion KEYWORD2
powerDown KEYWORD2
powerUp KEYWORD2
getTareOffset KEYWORD2
getTareStatus KEYWORD2


#######################################
# Constants (LITERAL1)
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=HX711_ADC
version=0.1.0
version=1.0.0
author=Olav Kallhovd
maintainer=Olav Kallhovd
sentence=Arduino library for the HX711 24-bit ADC for weight scales
Expand Down
Loading

0 comments on commit 79244db

Please sign in to comment.