Go Down

Topic: Making an Scale using Arduino,HX711 and loadcell - Data is constantly increasing (Read 176 times) previous topic - next topic

AliKHalili

Hello, I made a scale using HX711, an Arduino UNO and a loadcell with a code that I found and changed it a little bit to best fit my case and it was working great. Now I am trying to build another one but there is some problem with the outputs. They get constantly increased. For example when I connect the wires let's say I get a number like 120 g. It must show 120 (maybe one or two more or less) without any change. But it doesn't and the number gets increased constantly. I have checked the wires multiple times and I know they are correct. What I think is that either my loadcell or hx711 is corrupted cause I have used my arduino in other cases before so I know that it works fine.

Here is my sketch:
Code: [Select]
#define DT A0
#define SCK A1
#define sw 2

long sample = 0;
float val = 0;
long count = 0;
int counter = 0;

unsigned long readCount(void)
{
    unsigned long Count;
    unsigned char i;
    pinMode(DT, OUTPUT);
    digitalWrite(DT,HIGH);
    digitalWrite(SCK,LOW);
    Count = 0;
    pinMode(DT, INPUT);
    while(digitalRead(DT));
    for (i  =0; i < 24; i++)
    {
      digitalWrite(SCK,HIGH);
      Count = Count << 1;
      digitalWrite(SCK,LOW);
      if(digitalRead(DT))
      Count++;
    }
    digitalWrite(SCK,HIGH);
    Count = Count^0x800000;
    digitalWrite(SCK,LOW);
    return(Count);
}

void setup()
{
    Serial.begin(9600);
    pinMode(SCK, OUTPUT);
    pinMode(sw, INPUT_PULLUP);
    delay(1000);
    calibrate();

}

void loop()
{
    count = readCount();
    int w =(((count - sample) / val) - 2 *((count - sample) / val));
    if(counter < 1)
    {
        Serial.println("");
    }
    Serial.print("weight:   ");
    Serial.print((int)w);
    Serial.println("g");
    if(digitalRead(sw) == 0)
    {
        val = 0;
        sample = 0;
        w = 0;
        count = 0;
        calibrate();
    }
    counter++;
}

void calibrate()
{
    Serial.println("Calibration initialized!");
    delay(3000);
    Serial.println("Please Wait till the Process is finished!");
    Serial.println("");
    delay(4000);
    for(int i = 0; i < 100; i++)
    {
      count = readCount();
      sample += count;
      Serial.println(count);
    }
    sample /= 100;
    Serial.println("");
    Serial.println("Please put a 100g weight and wait!");
    Serial.println("");
    delay(5000);
    count = 0;
    while(count<1000)
    {
      count = readCount();
      count = sample - count;
      Serial.println(count);
    }
    Serial.println("");
    Serial.println("Weight loaded, please wait a little more...!");
    Serial.println("");
    delay(4000);
    for(int i = 0; i < 100; i++)
    {
      count = readCount();
      val += sample - count;
      Serial.println(sample - count);
    }
    val = val / 100.0;
    val = val / 100.0;        // put here your calibrating weight
}

Any thoughts about what the problem can be? Or any way to determine which part is corrupted?

pert

Please post your full sketch.

If possible, you should always post code directly in the forum thread as text using code tags:
  • Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code. This will make it easier for you to spot bugs and make it easier for us to read.
  • In the Arduino IDE or Arduino Web Editor, click on the window that contains your sketch code.
  • Press "Ctrl + A". This will select all the text.
  • Press "Ctrl + C". This will copy the selected text to the clipboard.
  • In a forum reply here, click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum's code tags markup to your reply.
  • Press "Ctrl + V". This will paste the sketch between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.
  • Repeat the above process if your sketch has multiple tabs.


This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

If the sketch is longer than the 9000 characters maximum allowed by the forum, then it's OK to add it as an attachment. After clicking the "Reply" button, you will see an "Attachments and other settings" link.

When your code requires a library that's not included with the Arduino IDE please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

wvmarle

Now I am trying to build another one

[...]

Any thoughts about what the problem can be? Or any way to determine which part is corrupted?
So you have two setups: one working, one not. That makes trouble shooting easy. Start swapping out parts between the good one (known good parts) and the troublesome one.

The load cell itself would be my primary suspect, so start by swapping out that one part, see what happens: if that's faulty, the original faulty setup should now read correct, and the original good setup should now read faulty.

If that's not it, continue with the rest of the parts.

I'm assuming you run the same code and use the same wiring for both, which would eliminate that as potential causes of the problem.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.


Go Up