Load cell +Hx711 +Arduino UNO

Hello. My code is for a weight balance, but the results are oscilated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//adiciona as bibliotecas ao codigo
#include <HX711.h> 
#include <Wire.h>
//#include <LiquidCrystal_I2C.h>

//LiquidCrystal_I2C lcd(0x20,16,2); //endereçamento do LCD

//configuracao dos pinos para o modulo HX711
const int PINO_DT = 3;
const int PINO_SCK = 2;
char comando;
float A,B;
float soma();
float portaA();
float portaB();

//declaracao do intervalo de espera
const int TEMPO_ESPERA = 1000;

HX711 escala; //declaracao do objeto ESCALA na classe HX711 da biblioteca

const int FATOR_CALIBRACAOA = 10800; //Fator de calibração para entrada A
const int FATOR_CALIBRACAOB = -2387; //Fator de calibração para entrada B


void setup ()
{
Serial.begin(9600);
  //mensagens do LCD
  //lcd.begin(); //inicializacao do display
  //lcd.backlight(); // ligacao do backlight do LCD
  Serial.print("    ");
  delay(TEMPO_ESPERA);


  escala.begin (PINO_DT, PINO_SCK); //inicializacao e definicao dos pinos DT e SCK dentro do objeto ESCALA
 // escala.tare(); //zera a escala
  

}

void loop ()
{
  //verifica se o modulo esta pronto para realizar leituras
  if (escala.is_ready()&& Serial.available())
  {   
        comando = Serial.read();
        switch (comando)
        {
          case 'x':
  //mensagens de leitura no monitor serial
    Serial.print(" Peso: ");
    Serial.print(soma()); //retorna a soma das leituras de cada barra com a unidade quilogramas
    Serial.print(" kg\n");
    break;
        } 
  } 
      else
    {
      Serial.print("Quando a carga estiver pronta aperte 'x'\n");
    }
  delay(TEMPO_ESPERA); //intervalo de espera para leitura
//  lcd.clear();
}

float portaA(){
    float A=0;
  escala.set_gain ( 128 ) ; //Seleciona a porta A com o ganho desta entrada
  escala.tare(); //zera a escala
  escala.set_scale(FATOR_CALIBRACAOA); //ajusta a escala para o fator de calibracao
  A=escala.get_units(10), 1;   //Retorna a média de 10 valores medidos e converte para kg
  return A;
}

float portaB (){
  float B=0;
  escala.set_gain ( 32 ) ; //Seleciona a porta B com o ganho respectivo a esta entrada
  escala.tare(); //zera a escala
  escala.set_scale(FATOR_CALIBRACAOB); //ajusta a escala para o fator de calibracao
  B=escala.get_units(10), 1;  //Retorna a média de 10 valores medidos e converte para kg
  return B;
}

float soma(){
  float valortotal=0, peso=0;
  float soma=0;
     soma=portaA()+portaB();
return soma;
}
I don't see a question.
> but the results are oscilated.
Welcome to the real world, https://en.wikipedia.org/wiki/Noise_(electronics) and https://en.wikipedia.org/wiki/Uncertainty_principle

https://cdn.sparkfun.com/datasheets/Sensors/ForceFlex/hx711_english.pdf
It's your responsibility to smooth and average the data.

For example, if you're trying to display something in Kg, then you need to throw away all the bits that represent amounts less than a gram (for example).

You have a 24-bit value.
If the MSB is 1Kg, then the 10th bit is approx 1g, and the 20th bit is approx 1mg.
The LSB is going to be wagging like a dog's tail with all the noise in your environment.

More of the same.
https://en.wikipedia.org/wiki/Switch#Contact_bounce
Averaging, range, filtering the readings, calibration and the environment the load cell is operating in are all important as the above comment.

Another possibility is to put a time delay/reduced sampling rate into the loop(). Also print out via the serial port or direct LCD plot the readings and see if you can make some sense (regular vs irregular) out of them.

Is 9600 the best rate? Try a few others.

What does the HX711 datasheet say?

Some of these test examples might be better than what you have.
https://github.com/bogde/HX711
Last edited on
Topic archived. No new replies allowed.