#include "HX711.h"
float val = 0;
float wht = 0;
int Wready = 3;
int Wprcnt = 7;
int Wsel = 9;
int Wsels = 0;
int RST = 11;
float prc = 0;
HX711 scale(A1, A2, 32); // set gain to 32 so loadcell connected to channel B
void setup() {
pinMode(Wsel, OUTPUT);
pinMode(Wready, OUTPUT);
pinMode(Wprcnt, OUTPUT);
pinMode(A4, INPUT);
Serial.begin(9600);
Serial.println("Init.");
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read());
Serial.print("read average: \t\t");
Serial.println(scale.read_average(4));
Serial.print("get value: \t\t");
Serial.println(scale.get_value(4));
Serial.print("get units: \t\t");
Serial.println(scale.get_units(4), 1);
scale.set_scale(77);
scale.tare();
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read());
Serial.print("read average: \t\t");
Serial.println(scale.read_average(4));
Serial.print("get value: \t\t");
Serial.println(scale.get_value(4));
Serial.print("get units: \t\t");
Serial.println(scale.get_units(4), 1);
Serial.println("Readings:");
}
void loop() {
// Check if weighing hopper closed, then reset the loadcell reading
if(analogRead(A4)>650){
scale.tare();
}
wht = (scale.get_units(2));
Serial.print("\t");
Serial.println((wht));
// Weight selection button
Wsels = digitalRead(Wsel);
if(Wsels == HIGH){
val = 462;
prc = 0.3;
}else{
val = 955;
prc = 0.5;
}
if((wht <= (val)) && (wht > (val*prc))){ // close the feeding hopper gate by 50% when weighing hopper filled with prc% of required weight (to get higher accurate fill)
digitalWrite(Wready, LOW);
digitalWrite(Wprcnt, HIGH);
}else if((wht < (val))){
digitalWrite(Wready, LOW);
digitalWrite(Wprcnt, LOW);
}else{
digitalWrite(Wready, HIGH);
digitalWrite(Wprcnt, HIGH);
}
}
after recording data from loadcell, I got this chart:
I just need to smooth data received from load cell to get more accurate weighing as least error I could get now is +-10grams and this is too much error.
Also after searching, found some techniques like moving-average filters and analog adaptive filters that should help me get more smoothed readings, but I've no idea how to implement any of them.
Regarding your original question (about averaging), you are currently averaging a block of four consecutive values in the get_value and get_units functions. Why not average more values; for example, what happens if you use get_units(128) instead of your get_units(4)....or some other number larger than 4...
Also, presumably you've done this, but googling arduino moving average brings up lots of examples of how smoothing, moving, etc can be implemented, if you want or need to get more complicated than just averaging blocks of consecutive values.
PS: It is interesting that most of the largest "noise" occurs immediately before changes in the slope of the line in your graph. Are you sure that the filling machinery or something else isn't causing some vibrations that the load cell picks up?
For increasing the average number to be more than 4, it will take more time to get the result for every loop - reading - so error will be increased too much, as in filling applications every milliseconds of flow the weight will change, so I need to read the live values as fast as possible.
Also tried to not take average - just get the current reading - the variation got increased in crazy way.
I already searched and found libraries for Arduino averaging but also it will take time to process within every loop which causes error increase so I need it in hardware - I think will be faster than SW [please correct me if wrong].
I'm sure the big issue is mechanical vibrations so already we are using some kind of leather washers trying to eliminate that vibrations effects. For feeding gate opening/closing, I'm using stepper NEMA23 [driver TB6560 V1] with 20cm arm.
Smoothing (sumx + reading(1-x)) is too slow? Maybe you need a higher speed ADC. Or, how about a low-pass filter on the load cell output. Better mechanical isolation sounds like the best solution.
I'll add these capacitors but please let me know where from HX711 ? power pins ?
Dave, I found some libraries for averaging, so I'll test with this one ( Microsmooth ) then return back to you with results. Also LPF should be with which cutoff frequency ?