1

I need to read two values from hx711 load cells connected to strain guages. Ive found the code below that reads one guage, but being self taught and with little experience I have no idea how to adapt it to work with two, is anyone able to help?
I need two individual readings not a combined one Thanks in advance, and credit to sparkfun who wrote this code.

#include "HX711.h"

#define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch

#define DOUT  3
#define CLK  2

HX711 scale;

void setup() {
  Serial.begin(9600);
  Serial.println("HX711 scale demo");

  scale.begin(DOUT, CLK);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  Serial.println("Readings:");
}

void loop() {
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.print(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
}
4
  • 1
    Create a second instance of the HX711 class and begin it with two different IO pins.
    – Majenko
    Sep 6 '20 at 19:09
  • 1
    make it so that you have something like these two in your code ... scale1.begin(DOUT1, CLK1); and scale2.begin(DOUT2, CLK2); .... start by renaming scale. to scale1. ... then duplicate all of the scale1. lines
    – jsotola
    Sep 6 '20 at 20:09
  • 1
    @jsotola, Ive tried that, but when I then try to read it, it tells me that scale2 was not declared in this scope Sep 6 '20 at 20:19
  • 1
    look at every line in your code above, that contains the word scale ... make two lines out of each one of those lines
    – jsotola
    Sep 6 '20 at 20:39
1

The line:

HX711 scale;

is called the "instantiation of the object". It creates a new object called scale which is an instance of the HX711 class. It is this "object" that is your interface to the HX711 physical device.

To use two of them you first need to have two objects - one for each HX711. You can name them whatever you like. For example:

HX711 scale1;
HX711 scale2;

The line:

scale.begin(DOUT, CLK);

is where it tells the HX711 object which pins the HX711 chip is connected to. In this case DOUT and CLK which you have defined as 3 and 2 respectively. You could just as well have written:

scale.begin(3, 2);

Each object has to be "begun", so you would need a begin for each object you instantiate:

scale1.begin(3, 2); // DOUT = 3, CLK = 2
scale2.begin(5, 4); // DOUT = 5, CLK = 4

Now for every operation that you did on scale you would instead do on your two new objects:

scale1.set_scale(calibration_factor);
scale2.set_scale(calibration_factor);

etc.

And when you want to take a measurement you just select which object to read from:

Serial.print(scale1.get_units(), 1);

or

Serial.print(scale2.get_units(), 1);

You can add a third by creating and using scale3 in exactly the same way as scale1 and scale2.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.