Now that you've calculated your offset value, you will use it to convert your scale's output to a weight in kilograms.

You can continue working from your PhidgetScale project in this step. Replace your previous code with the following code.
Write Code (Java)
Not your programming language? Set your preferences so we can display relevant code examples.
package phidgetscale;
//Add Phidgets Library
import com.phidget22.*;
public class PhidgetScale {
public static void main(String[] args) throws Exception{
//Create
VoltageRatioInput scale = new VoltageRatioInput();
//Open
scale.open(1000);
//Use your Offset Value
double offsetValue = INSERT VALUE HERE
//Use your Phidgets
while(true){
//Calculate Weight (kg)
double weight = 4700 * (scale.getVoltageRatio() - offsetValue);
//Display Weight
System.out.println(String.format("%.3f kg", weight));
Thread.sleep(250);
}
}
}
//Add Phidgets Library
import com.phidget22.*;
public class PhidgetScale {
public static void main(String[] args) throws Exception{
//Create
VoltageRatioInput scale = new VoltageRatioInput();
//Open
scale.open(1000);
//Use your Offset Value
double offsetValue = INSERT VALUE HERE
//Use your Phidgets
while(true){
//Calculate Weight (kg)
double weight = 4700 * (scale.getVoltageRatio() - offsetValue);
//Display Weight
System.out.println(String.format("%.3f kg", weight));
Thread.sleep(250);
}
}
}
//Add Phidgets Library
import com.phidget22.*;
//Define
VoltageRatioInput scale;
//Use your Offset Value
double offsetValue = INSERT VALUE HERE
void setup(){
try{
//Create
scale = new VoltageRatioInput();
//Open
scale.open(1000);
}catch(Exception e){
//Handle Exceptions
e.printStackTrace();
}
}
void draw(){
try{
//Use your Phidgets
//Calculate Weight (kg)
double weight = 4700 * (scale.getVoltageRatio() - offsetValue);
//Display Weight
println(String.format("%.3f kg", weight));
delay(250);
}catch(Exception e){
//Handle Exceptions
e.printStackTrace();
}
}
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
#Create
scale = VoltageRatioInput()
#Open
scale.openWaitForAttachment(1000)
#Use your Offset Value
offsetValue = INSERT VALUE HERE
#Use your Phidgets
while(True):
#Calculate Weight (kg)
weight = 4700 * (scale.getVoltageRatio() - offsetValue)
#Display Weight
print("%.3f kg" % weight)
time.sleep(0.25)
Write Code (C#)
Not your programming language? Set your preferences so we can display relevant code examples.
//Add Phidgets Library
using Phidget22;
namespace PhidgetScale
{
class Program
{
static void Main(string[] args)
{
//Create
VoltageRatioInput scale = new VoltageRatioInput();
//Open
scale.Open(1000);
//Use your Offset Value
double offsetValue = INSERT VALUE HERE
//Use your Phidgets
while (true)
{
//Calculate Weight (kg)
double weight = 4700 * (scale.VoltageRatio - offsetValue);
//Display Weight
System.Console.WriteLine(weight.ToString("F3") + " kg");
System.Threading.Thread.Sleep(250);
}
}
}
}
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 single label and linked it to an IBOutlet named weightLabel
import Cocoa
//Add Phidgets Library
import Phidget22Swift
class ViewController: NSViewController {
@IBOutlet weak var weightLabel: NSTextField!
//Create
let scale = VoltageRatioInput()
let offsetValue = INSERT VALUE HERE
override func viewDidLoad() {
super.viewDidLoad()
do{
//Subscribe to event
let _ = scale.voltageRatioChange.addHandler(scale_change)
//Open
try scale.open()
}catch{
print(error)
}
}
func scale_change(sender:VoltageRatioInput, voltageRatio: Double){
DispatchQueue.main.async {
//Use your Phidget
//Calculate Weight (kg)
let weight = 4700 * (voltageRatio - self.offsetValue)
//Display Weight
self.weightLabel.stringValue = String(format:"%.3f kg",weight)
}
}
}
Run Your Program
You should see 0.000 kg when nothing is on the scale. Try placing your phone on the scale to determine if the scale is accurate (you can easily find your phone’s weight online).
What is 4700?
The value 4700 is used to translate the voltage readings from your scale into kilograms. The value is an estimate and can be refined through calibration in the Increasing Accuracy section. If you calibrate your scale, it will be much more accurate.
Practice
- Convert the scale output to grams instead of kg. (Hint: 1kg = 1000g)
- Convert the scale output to pounds instead of kg. (Hint: 1kg = 2.205lbs)