66 and we meet someday (Beerware license).
77
88 This example shows how to setup a scale complete with zero offset (tare),
9- and linear calibration.
9+ and linear calibration.
1010
1111 If you know the calibration and offset values you can send them directly to
1212 the library. This is useful if you want to maintain values between power cycles
13- in EEPROM or Non-volatile memory (NVM). If you don't know these values then
13+ in EEPROM or Non-volatile memory (NVM). If you don't know these values then
1414 you can go through a series of steps to calculate the offset and calibration value.
15-
15+
1616 Background: The IC merely outputs the raw data from a load cell. For example, the
1717 output may be 25776 and change to 43122 when a cup of tea is set on the scale.
1818 These values are unitless - they are not grams or ounces. Instead, it is a
4040
4141NAU7802 myScale; // Create instance of the NAU7802 class
4242
43- // EEPROM locations to store 4-byte variables
43+ // EEPROM locations to store 4-byte variables
4444#define LOCATION_CALIBRATION_FACTOR 0 // Float, requires 4 bytes of EEPROM
4545#define LOCATION_ZERO_OFFSET 10 // Must be more than 4 away from previous spot. Long, requires 4 bytes of EEPROM
4646
47+ bool settingsDetected = false ; // Used to prompt user to calibrate their scale
48+
49+ // Create an array to take average of weights. This helps smooth out jitter.
50+ #define AVG_SIZE 4
51+ float avgWeights[AVG_SIZE];
52+ byte avgWeightSpot = 0 ;
53+
4754void setup ()
4855{
4956 Serial.begin (9600 );
5057 Serial.println (" Qwiic Scale Example" );
5158
5259 Wire.begin ();
53- Wire.setClock (400000 );
60+ Wire.setClock (400000 ); // Qwiic Scale is capable of running at 400kHz if desired
5461
5562 if (myScale.begin () == false )
5663 {
@@ -63,6 +70,11 @@ void setup()
6370
6471 myScale.setSampleRate (NAU7802_SPS_320); // Increase to max sample rate
6572 myScale.calibrate ();
73+
74+ Serial.print (" Zero offset: " );
75+ Serial.println (myScale.getZeroOffset ());
76+ Serial.print (" Calibration factor: " );
77+ Serial.println (myScale.getCalibrationFactor ());
6678}
6779
6880void loop ()
@@ -71,12 +83,28 @@ void loop()
7183 {
7284 long currentReading = myScale.getReading ();
7385 float currentWeight = myScale.getWeight ();
74-
86+
7587 Serial.print (" Reading: " );
7688 Serial.print (currentReading);
7789 Serial.print (" \t Weight: " );
7890 Serial.print (currentWeight, 2 ); // Print 2 decimal places
7991
92+ avgWeights[avgWeightSpot++] = currentWeight;
93+ if (avgWeightSpot == AVG_SIZE) avgWeightSpot = 0 ;
94+
95+ float avgWeight = 0 ;
96+ for (int x = 0 ; x < AVG_SIZE ; x++)
97+ avgWeight += avgWeights[x];
98+ avgWeight /= AVG_SIZE;
99+
100+ Serial.print (" \t AvgWeight: " );
101+ Serial.print (avgWeight, 2 ); // Print 2 decimal places
102+
103+ if (settingsDetected == false )
104+ {
105+ Serial.print (" \t Scale not calibrated. Press 'c'." );
106+ }
107+
80108 Serial.println ();
81109 }
82110
@@ -104,7 +132,7 @@ void calibrateScale(void)
104132 while (Serial.available ()) Serial.read (); // Clear anything in RX buffer
105133 while (Serial.available () == 0 ) delay (10 ); // Wait for user to press key
106134
107- myScale.calculateZeroOffset (); // Zero or Tare the scale.
135+ myScale.calculateZeroOffset (64 ); // Zero or Tare the scale. Average over 64 readings .
108136 Serial.print (F (" New zero offset: " ));
109137 Serial.println (myScale.getZeroOffset ());
110138
@@ -120,7 +148,7 @@ void calibrateScale(void)
120148 float weightOnScale = Serial.parseFloat ();
121149 Serial.println ();
122150
123- myScale.calculateCalibrationFactor (weightOnScale); // Tell the library how much weight is currently on it
151+ myScale.calculateCalibrationFactor (weightOnScale, 64 ); // Tell the library how much weight is currently on it
124152 Serial.print (F (" New cal factor: " ));
125153 Serial.println (myScale.getCalibrationFactor (), 2 );
126154
@@ -157,11 +185,15 @@ void readSystemSettings(void)
157185 EEPROM.get (LOCATION_ZERO_OFFSET, settingZeroOffset);
158186 if (settingZeroOffset == 0xFFFFFFFF )
159187 {
160- settingZeroOffset = 1000 ; // Default to 1000 so we don't get inf
188+ settingZeroOffset = 1000L ; // Default to 1000 so we don't get inf
161189 EEPROM.put (LOCATION_ZERO_OFFSET, settingZeroOffset);
162190 }
163191
164192 // Pass these values to the library
165193 myScale.setCalibrationFactor (settingCalibrationFactor);
166194 myScale.setZeroOffset (settingZeroOffset);
195+
196+ settingsDetected = true ; // Assume for the moment that there are good cal values
197+ if (settingCalibrationFactor < 0.1 || settingZeroOffset == 1000 )
198+ settingsDetected = false ; // Defaults detected. Prompt user to cal scale.
167199}
0 commit comments