# How to do weigh scale zeroing (tare weight)?

I'm using the sketch below for a weighing scale. The sketch prints out the load-cell voltage just fine. I can't figure out how to let the scale run a couple seconds to stabilize, then use the current voltage reading (nothing on the scale) as the "Zero" voltage. Then subtract that "Zero" voltage from future readings to get the actual voltage for any weight placed on the scale. For example, let's say the nothing on the scale voltage is 0.5 volts. Then a 10 LB weight is added and the voltage goes up to 1.5 volts. So the actual voltage increase from the weight is 1.5 minus 0.5, or 1.0 volts.

``````#include <ads1262.h>

const float VREF = 2.50;  // Internal reference

void setup() {
// initialize the  data ready and chip select pins:
pinMode(ADS1262_CS_PIN,    OUTPUT);  // chip enable output line
pinMode(ADS1262_PWDN_PIN,  OUTPUT);  // power down output
Serial.begin(9600);

}

void loop() {
// Nothing to do unless data ready is asserted.

// Get a reading and convert to voltage.
signed long reading = (uint32_t) buffer[1] << 24
| (uint32_t) buffer[2] << 16
| (uint32_t) buffer[3] <<  8
| (uint32_t) buffer[4] <<  0;
float voltage = VREF / (1UL<<31) * reading;

Serial.println(voltage, 8);
}
``````
• So you want it to wait 2 seconds and save a tare value?, and use that for adjustment of the outputs later? Commented Mar 1, 2017 at 2:05

You start out with a variable named Tare initialized to zero `float Tare = 0.0;` put in a conditional like `if (millis() > 2000 & Tare == 0.0) Tare = voltage;` and always report `voltage - Tare`.

``````float Tare = 0;

void loop() {
// Nothing to do unless data ready is asserted.
// Get a reading and convert to voltage.
signed long reading = (uint32_t) buffer[1] << 24
| (uint32_t) buffer[2] << 16
| (uint32_t) buffer[3] <<  8
| (uint32_t) buffer[4] <<  0;
float voltage = VREF / (1UL<<31) * reading;
if (millis() > 2000 & Tare == 0.0) Tare = voltage;
Serial.println(voltage-Tare, 8);
}
``````

If you want to make it smarter, add a button for tare and maybe if you have a calibration weight, add a button for calibrating.

``````float scale= 1.0;
const float calib_weight = 500.0; // grams?
...
if(tareButton | (millis() > 2000 & Tare == 0.0)) Tare = voltage;
if(calibrateButton) scale = calib_weight / (voltage-Tare) ;
...
Serial.println((voltage - Tare)*scale,8);
...
``````

The direct answer to your question is to wait for the scale to settle and store the Tare voltage; then subtract it from future readings.

But that only works if the scale's transfer function (Voltage as a function of weight, or V(f)) is linear (weight = Voltage * CONSTANT). Otherwise you need to keep the Tare in weight units, do the full conversion of voltage to weight on subsequent readings, and subtract the Tare weight from that.

• Thank you for that. My main problem right now is the code itself. I'm simply a novice with the Arduino C++. How to store a voltage after the adc has run a little bit and then name it "Tare".
– Rico
Commented Jan 31, 2017 at 0:11
• You start out with a variable named Tare initialized to zero `float Tare = 0.0;` put in a conditional like `if (millis() > 2000 & Tare == 0.0) Tare = Voltage;` and always report `voltage - Tare`. Commented Mar 1, 2017 at 12:59