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:
#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?