In this section, you will learn how to calibrate your scale to increase it's accuracy!

You will need to create a new project called ScaleCalibration and import the Phidgets Library. If you’ve forgotten how to do this, revisit the Configure section from the Getting Started Kit.
Calibrating Your Scale
When calculating weight in your previous program, you used the code below.
Not your programming language? Set your preferences so we can display relevant code examples.
//Calculate Weight (kg)
double weight = 4700 * (scale.getVoltageRatio() - offsetValue);
//Calculate Weight (kg)
double weight = 4700 * (scale.getVoltageRatio() - offsetValue);
//Calculate Weight (kg)
double weight = 4700 * (scale.getVoltageRatio() - offsetValue);
Not your programming language? Set your preferences so we can display relevant code examples.
#Calculate Weight (kg)
weight = 4700 * (scale.getVoltageRatio() - offsetValue)
Not your programming language? Set your preferences so we can display relevant code examples.
//Calculate Weight (kg)
double weight = 4700 * (scale.VoltageRatio - offsetValue);
The value 4700 is an estimate of the slope of your scale. In order to calculate the real value for your specific scale, you will need to use a known weight.
If you have access to an accurate scale, you can simply measure an everyday object and record its weight. For example, a water bottle works great:

Reference the table below when selecting a weight for calibration.
Load Cell | Recommended Weight |
---|---|
780g | ~400g |
5kg | ~2.5kg |
25kg | ~2.5kg |
Calibration Formula
The formula below can be used to find a new, more precise slope value for your scale.
Slope = Known Weight (kg) / (Measured Weight - Offset)
Where:
Known Weight (kg) is your known weight in kg.
Measurement is the voltage ratio of your known weight.
Offset is the offset value of your scale.
You can determine the slope of your scale yourself using the formula above, or you can run the program below to calculate it programmatically.
Write Code (Java)
Not your programming language? Set your preferences so we can display relevant code examples.
package scalecalibration;
//Add Phidgets Library
import com.phidget22.*;
import java.util.Scanner;
public class ScaleCalibration {
//Define
static VoltageRatioInput scale;
public static double getAverage()throws Exception{
double average = 0;
int count = 0;
System.out.println("Averaging Value...");
while(count < 64){
average += scale.getVoltageRatio();
count += 1;
Thread.sleep(20);
}
return average/count;
}
public static void main(String[] args) throws Exception{
Scanner scan = new Scanner(System.in);
//Create
scale = new VoltageRatioInput();
//Open
scale.open(1000);
//Set data interval to minimum
scale.setDataInterval(scale.getMinDataInterval());
System.out.println("Make sure nothing is on your scale and press Enter");
scan.nextLine();
double offset = getAverage();
System.out.println("Place a known weight on the scale, type the weight in kilograms and press Enter");
double knownWeight = Double.parseDouble(scan.nextLine());
double measuredWeight = getAverage();
double slope = knownWeight / (measuredWeight - offset);
System.out.println("Your new slope value is: " + Math.round(slope));
}
}
//Add Phidgets Library
import com.phidget22.*;
import java.util.Scanner;
public class ScaleCalibration {
//Define
static VoltageRatioInput scale;
public static double getAverage()throws Exception{
double average = 0;
int count = 0;
System.out.println("Averaging Value...");
while(count < 64){
average += scale.getVoltageRatio();
count += 1;
Thread.sleep(20);
}
return average/count;
}
public static void main(String[] args) throws Exception{
Scanner scan = new Scanner(System.in);
//Create
scale = new VoltageRatioInput();
//Open
scale.open(1000);
//Set data interval to minimum
scale.setDataInterval(scale.getMinDataInterval());
System.out.println("Make sure nothing is on your scale and press Enter");
scan.nextLine();
double offset = getAverage();
System.out.println("Place a known weight on the scale, type the weight in kilograms and press Enter");
double knownWeight = Double.parseDouble(scan.nextLine());
double measuredWeight = getAverage();
double slope = knownWeight / (measuredWeight - offset);
System.out.println("Your new slope value is: " + Math.round(slope));
}
}
Code is not available for Processing. Please select another Java-based IDE.
Write Code (Python)
Not your programming language? Set your preferences so we can display relevant code examples.
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
#Required for sleep statement
import time
def get_average():
average = 0
count = 0
print("Averaging Value...\n")
while(count < 64):
average += scale.getVoltageRatio()
count += 1
time.sleep(0.02)
return average/count
#Create
scale = VoltageRatioInput()
#Open
scale.openWaitForAttachment(1000)
#Set data interval to minimum
scale.setDataInterval(scale.getMinDataInterval())
input("Make sure nothing is on your scale and press Enter\n")
offset = get_average()
known_weight = input("Place a known weight on the scale, type the weight in kilograms and press Enter\n")
measured_weight = get_average()
slope = float(known_weight) / (measured_weight - offset)
print("Your new slope value is: " + str(round(slope,0)))
Write Code (C#)
Not your programming language? Set your preferences so we can display relevant code examples.
//Add Phidgets Library
using Phidget22;
namespace ScaleCalibration
{
class Program
{
static VoltageRatioInput scale;
static double getAverage()
{
double average = 0;
int count = 0;
System.Console.WriteLine("Averaging Value ...");
while(count < 64)
{
average += scale.VoltageRatio;
count += 1;
System.Threading.Thread.Sleep(20);
}
return average / count;
}
static void Main(string[] args)
{
//Create
scale = new VoltageRatioInput();
//Open
scale.Open(1000);
//Set data interval to minimum
scale.DataInterval = scale.MinDataInterval;
System.Console.WriteLine("Make sure nothing is on your scale and press Enter");
System.Console.ReadLine();
double offset = getAverage();
System.Console.WriteLine("Place a known weight on the scale, type the weight in kilograms and press Enter");
double knownWeight = System.Convert.ToDouble(System.Console.ReadLine());
double measuredWeight = getAverage();
double slope = knownWeight / (measuredWeight - offset);
System.Console.WriteLine("Your new slope value is: " + slope.ToString("F0"));
System.Console.ReadLine();
}
}
}
Write Code (Swift)
Not your programming language? Set your preferences so we can display relevant code examples.
The code below assumes you've created a label and linked it to an IBOutlet named mainLabel, a button and linked it to an IBOutlet named calibrationButton, and a text input and linked it to an IBOutlet named knownWeight.
import Cocoa
//Add Phidgets Library
import Phidget22Swift
class ViewController: NSViewController {
//Create
let scale = VoltageRatioInput()
@IBOutlet weak var mainLabel: NSTextField!
@IBOutlet weak var knownWeight: NSTextField!
@IBOutlet weak var calibrationButton: NSButton!
var offset = 0.0
var slope = 0.0
var average = 0.0
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
do{
//Subscribe to event
let _ = scale.voltageRatioChange.addHandler(scale_change)
//Open
try scale.open()
//Set data interval to minimum
try scale.setDataInterval(scale.getMinDataInterval())
}catch{
print(error)
}
}
func scale_change(sender:VoltageRatioInput, voltageRatio: Double){
}
@IBAction func calibrationButton(_ sender: Any) {
do{
average = 0
count = 0
if(calibrationButton.title == "Begin Calibration"){
while(count < 64){
try average += scale.getVoltageRatio()
count += 1
usleep(20000)
}
offset = average / Double(count)
calibrationButton.title = "Continue Calibration"
mainLabel.stringValue = "Place known weight on scale and press Continue Calibration."
}
else{
if let knownWeightVal = Double(knownWeight.stringValue){
while(count < 64){
try average += scale.getVoltageRatio()
count += 1
usleep(20000)
}
let measuredWeight = average / Double(count)
let slope = knownWeightVal / (measuredWeight - offset)
mainLabel.stringValue = "Your new slope value is: " + String(format: "%.0f",slope)
}
else{
mainLabel.stringValue = "ERROR. Please enter a correct weight in kg above. Then place known weight on scale and press Continue Calibration."
}
}
}catch{
print(error)
}
}
}
Run Your Program
After running the program above, you will have a new slope value that should be close to 4700. Replace the old value in your previous programs with the new value.