HX711 is not working while trying setup i2c

I try tu set up 3 load cells and accelerometer and if I use command wire.begin() and then try to get measurement from scale2 the arduino crashes end stops doing anything. I'm using arduino leonardo and if I coment wire.begin() everything works normaly. Can someone help me please

#include <Wire.h>
#include <SparkFun_KX13X.h> // Click here to get the library: http://librarymanager/All#SparkFun_KX13X
#include <string.h>
#include <HX711.h>
#include <LiquidCrystal.h>


const int buttonPin = 6;
int buttonState = 0;
const int rs = 8, en = 13, d4 = 9, d5 = 10, d6 = 11, d7 = 12;

#define calibration_factor -199000.00
#define dout1 0
#define clk1 1
#define dout2 2
#define clk2 3
#define dout3 4
#define clk3 5
//SparkFun_KX132 kxAccel;
SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above

outputData myData; // Struct for the accelerometer's data

HX711 scale1(dout1,clk1);
HX711 scale2(dout2,clk2);
HX711 scale3(dout3,clk3);
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
  //set up belek tensometrycznych

  Serial.begin(9600);
  // Wait for the Serial monitor to be opened.
  while (!Serial)
    delay(50);
  lcd.begin(16, 2);
  Serial.println("test wag");
  
  scale1.set_scale(calibration_factor);
  scale1.tare();
  scale2.set_scale(calibration_factor);
  scale2.tare();
  scale3.set_scale(calibration_factor);
  scale3.tare();
  //Serial.println("reading");
  if(scale1.is_ready()){
    Serial.println("scale1 good");
  }
  if(scale2.is_ready()){
    Serial.println("scale2 good");
  }
  if(scale3.is_ready()){
    Serial.println("scale3 good");
  }
  //Serial.println(scale1.is_ready());
  //set up akcelerometrow
  pinMode(buttonPin,INPUT);
  delay(1000);
  Wire.begin();
  while (!Serial)
    delay(50);

  if (!kxAccel.begin())
  {
    Serial.println("Could not communicate with the the KX13X. Freezing.");
    while (1)
      ;
  }
  Serial.println("Ready.");
  if (kxAccel.softwareReset())
    Serial.println("Reset.");
  // Give some time for the accelerometer to reset.
  // It needs two, but give it five for good measure.
  delay(5);

  kxAccel.enableAccel(false);
  kxAccel.setRange(SFE_KX132_RANGE16G); // 16g Range
  // kxAccel.setRange(SFE_KX134_RANGE16G);         // 16g for the KX134
  kxAccel.enableDataEngine(); // Enables the bit that indicates data is ready.
  // kxAccel.setOutputDataRate(); // Default is 50Hz
  kxAccel.enableAccel();
}

void loop() {
  //Serial.println("hujuhujuujuh");
  //pomiar masy i srodka ciezkosc wykonuje sie do czasu kiedy nie kliknie sie przycisku
  float masa=0.0;
  bool t=false;
  buttonState=0;
  //Serial.println("aalala");
  while (!t) {
    //Serial.println("skdfjh");
    lcd.setCursor(0, 0);
    float masa1;
    masa1=scale1.get_units();
    Serial.println("scale1good");
    float masa2;
    masa2=scale2.get_units();
    Serial.println("scale2good");
    float masa3;
    masa3=scale3.get_units();
    Serial.println("scale3good");
    

you seem to be using pin 0 and pin1 which are the Serial port pins .
Assuming your board is an UNO.

1 Like

if I use command wire.begin()

That is expected, since on the Leonardo, D2 and D3 are SDA and SCL for the Wire library.

You cannot use both functions at the same time.

3 Likes

There is also a "HX711-multi" library, where you can share the clock pins.
So you only need four pins for three HX711 boards.
Leo..

2 Likes

As the CLOCK is used to set gain, selection of channel too, and power down too.
So it will only work if the three HX711's use the same configuration.
Did not verify this multi behavior however.
See page 4 and 5 of the datasheet for the other CLOCK functions.

1 Like

I did not write the library, but I don't see how that couldn't work.
Sure, the clock sets gain/ch/sleep of all, but you can listen to the data of only one.
Leo..

1 Like

@Wawa
Yes I agree with you, it will work.
My remark was only to say there are some constraints one should be aware of, and within those constrains I see no problems why it should not work.
(I've written a HX711 library and this use case is described in the readme.md)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.