HX711 readings change when SD library is included !?

I am reading raw values from HX711 board using Teensy 3.2.
For some reason the value I receive depends on whether I #include <SD.h> library in the code.
when I comment out the SD.h I receive 925,000, when I include SD.h I receive 800,000.

Any ideas what is causing this discrepancy?

the code is here:

//#include <SD.h> //SD library changes the value read from HX711!!!

byte DOUT = 7; //orange
byte SCLK = 9; //white

// *************Library for reading weight from HX711 A/D amplifier
void clk() {
digitalWrite(SCLK, HIGH);
digitalWrite(SCLK, LOW);
}

int32_t readRawHX711() {
int32_t hxValue = 0;
digitalWrite(SCLK, LOW); //SCK is made LL
while (digitalRead(DOUT) != LOW) ; //wait until Data are ready - DOUT goes LOW
for (int i = 0; i < 24; i++) { //read 24-bit data from HX711
clk(); //generate CLK pulse to get MSB-it at A1-pin
hxValue = hxValue << 1; //shift bits one up (x=x*2)
bitWrite(hxValue, 0, digitalRead(DOUT)); //write the last bit of X
}
clk();
if (hxValue > 9000000) hxValue = hxValue-16777216; // converts unsigned value to a signed one
return hxValue;
}

void setup() {
Serial.begin(9600);
delay(1000);
pinMode(DOUT, INPUT); //DOUT data line
pinMode(SCLK, OUTPUT); //SCK CLICK line
Serial.println(readRawHX711());
}

void loop() {
//Serial.println(readRawHX711());
}

readRawHX711.ino (1.15 KB)

How do you power the HX711. The analogue part of a HX711 breakout board needs 5volt to make a stable 4.25volt excitation for the load cell. If you must use a 3.3volt processor, then use the Sparkfun version of the HX711. That's the only one that has split analogue/digital supply voltages. Or... hack a board yourself. Leo..

Leo,

good idea that it might be caused by a drop in the excitation voltage - I checked that today, but it does not seem to be the case:

HX711 is powered from USB Vin (5.08V) E+E- potential at the load cell reads 4.25V

these values are the same regardless including or excluding the SD.h library.

I also tried to power the HX711 from 3.3V and it works fine for my small 100g load cell (E+E- is 2.53V).

I looked further at the readings coming back from HX711 and it turns out that the readings make sense and are linear only when the SD.h library IS INCLUDED. When the library is not included, the readings are non linear and actually 10g reads more than 20g!!!

so somehow not loading the SD.h messes up something - probably at the level of reading the value from the HX711 chip, but what changes in the way Teensy reads the value is not clear to me.

It would be interesting to see if anyone else can reproduce the behavior.

Best

Hynek

I figured it out:
without SD library included, the time between Serial clock signal (CLK) and reading the value of the DOUT pin is too short.

Including one extra line of code between CLK and bitWrite solves the problem and HX711 works fine with or without SD library included.

here is the corrected code to read values from HX711 board without relying on a library (just pasting in, as I did not figure out how to insert the code window):

byte DOUT = 7; // DOUT/MISO pin to read data from HX711 (orange)
byte SCLK = 9; // serial clock pin (white)

int32_t tareBase = 0;
float scalingFactor = 6150; //scaling factor to convert raw HX711 value to grams

// *************Library for reading weight from HX711 A/D amplifier
void clk() {
digitalWrite(SCLK, HIGH);
digitalWrite(SCLK, LOW);
}

int32_t readRawHX711() {
int32_t hxValue = 0;
digitalWrite(SCLK, LOW); //SCK is made LL
while (digitalRead(DOUT) != LOW) ; //wait until Data are ready - DOUT goes LOW
for (int i = 0; i < 24; i++) { //read 24-bit data from HX711
clk(); //generate serial clock pulse to read the next bit
hxValue = hxValue << 1; //shift bits one up (i.e. hxValue=hxValue*2)
digitalWrite(SCLK, LOW); // slows down the code so DOUT data are ready
bitWrite(hxValue, 0, digitalRead(DOUT)); //write the last bit
}
clk(); //generate CLK pulse to finish reading
if (hxValue > 8388608) hxValue = hxValue-16777216; // converts unsigned value to a signed one
return hxValue;
}

void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(DOUT, INPUT); //DOUT data line
pinMode(SCLK, OUTPUT); //SCK serail CLOCK line
delay(2000); //2s delay to stabilize the load cell before zeroing
tareBase = readRawHX711();
Serial.print(“TareBase: “);
Serial.println(tareBase);
Serial.println(”*********************”);
}

void loop() {
int32_t hxValue = readRawHX711();
Serial.print("Raw: “);
Serial.print(hxValue);
Serial.print(” Zeroed: “);
Serial.print(hxValue-tareBase);
Serial.print(” Scaling factor based on 50g: “);
Serial.print((hxValue-tareBase)/50.00);
Serial.print(” Scaled: ");
Serial.println((hxValue-tareBase)/scalingFactor);
delay(1000);
}