For what it's worth, here is the version of the module that I'm using. It's the most stable and precise of all the versions I've tested so far. It uses the SPI clock to send 24 ticks, and it uses hardware SPI1, which appears to be twice as fast as software SPI. It doesn't allow changing of the mode, as that requires a nonmultiple of 8 ticks. Here it is:
/**
Options should contain:
{
sck : PD_SCK pin
miso : DOUT pin
lsbGrams : grams per LSB
*/
function HX711(options) {
options = options||{};
this.sck = options.sck;
if (!this.sck) throw "Expecting sck";
this.miso = options.miso;
if (!this.miso) throw "Expecting miso";
//this.mode = options.mode||"A128"; //changing the mode isn't supported yet
this.lsbGrams = options.lsbGrams||1;
/* The spec sheet specifies sending 24 ticks followed by 1-3 ticks for the gain.
However, we are using the SPI clock which can only send multiples of 8 ticks.
The HX711 seems to be fine with only getting 24 ticks.
*/
this.spi = SPI1;//new SPI(); // use hardware SPI instead of software SPI
this.spi.setup({miso:this.miso, sck:this.sck, baud: 1000000});
//this.sck.write(0); // idle, but on
this.zero = 0;
}
HX711.prototype.readRaw = function() {
var d = this.spi.send([0,0,0]);
var val = d[0]*65536 + d[1]*256 + d[2];
return val;
};
/// Set the current reading to be the zero
HX711.prototype.tare = function() {
this.zero = this.readRaw();
};
/// Calibrate the lsbGrams value by putting a known weight on the scale
/// then call this function passing in the grams of the known weight.
HX711.prototype.calibrateLSBGrams = function( knownGrams ) {
if(!this.zero){
throw "Must call tare() with zero weight first before calibrating.";
}
if(!knownGrams){
throw "Must supply a non-zero value for the known weight in grams.";
}
this.lsbGrams = knownGrams/this.readRaw();
};
/// Read the ADC and return the result in grams (based on options.lsbGrams)
HX711.prototype.readGrams = function() {
return (this.readRaw() - this.zero) * this.lsbGrams;
};
/// Is data ready for retrieval?
HX711.prototype.isReady = function() {
return !this.miso.read();
};
/// Set whether the ADC is in standby mode or not
HX711.prototype.setStandby = function(isStandby) {
this.sck.write(isStandby);
};
exports.connect = function(options) {
return new HX711(options);
};
Below is an example of using it where I call tare() after 1 second and then spit out the weight measurement every second after that. Note that if you tare() too quickly, it'll not be accurate. Sometimes I need to tare again because the zero was off. This appears to be normal behavior since my kitchen scale is the same way (requiring multiple tares to get a stable zero). You can't do reads too close together in time or it'll introduce variability. This might be because the ADC needs to time out since I'm only sending 24 ticks or something... It appears that if you wait at least 30ms between reads, you'll be fine. Here I'm waiting 1000ms:
If you round to the nearest 0.1 gram, it's quite stable. Rounding to the nearest quarter gram is extremely stable. Since you can sample every 30ms, one could easily increase sampling rate and return an average over multiple reads to reduce noise even more. That's probably what I'll do for my current application.
Hope this helps others who want to use an HX711.
Gordon, I'm available to investigate things or do tests, or otherwise help in any way.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
For what it's worth, here is the version of the module that I'm using. It's the most stable and precise of all the versions I've tested so far. It uses the SPI clock to send 24 ticks, and it uses hardware SPI1, which appears to be twice as fast as software SPI. It doesn't allow changing of the mode, as that requires a nonmultiple of 8 ticks. Here it is:
Below is an example of using it where I call tare() after 1 second and then spit out the weight measurement every second after that. Note that if you tare() too quickly, it'll not be accurate. Sometimes I need to tare again because the zero was off. This appears to be normal behavior since my kitchen scale is the same way (requiring multiple tares to get a stable zero). You can't do reads too close together in time or it'll introduce variability. This might be because the ADC needs to time out since I'm only sending 24 ticks or something... It appears that if you wait at least 30ms between reads, you'll be fine. Here I'm waiting 1000ms:
Here are some readings with nothing on the scale:
And here are some sample readings with a 33 gram weight:
If you round to the nearest 0.1 gram, it's quite stable. Rounding to the nearest quarter gram is extremely stable. Since you can sample every 30ms, one could easily increase sampling rate and return an average over multiple reads to reduce noise even more. That's probably what I'll do for my current application.
Hope this helps others who want to use an HX711.
Gordon, I'm available to investigate things or do tests, or otherwise help in any way.