Introduction: Easy IOT Scale

About: Interested in Phidgets!

In this project, we will get a scale from Phidgets working. You could then use this scale for a variety of tasks such as:

  • Monitor a plant, determine when to water it based on weight
  • Monitor your pet's food/water and send an alert when low
  • Variety of science experiements
  • etc etc.

Supplies

This project uses the Scale Kit from Phidgets. It includes the following:

  • 1x Scale Platform
  • 3x Load Cells (780g, 5kg, 25kg)
  • 1x Wheatstone Bridge Phidget

You will also need a VINT Hub Phidget to connect to your computer.

Notes:

  • If you're a student/teacher, you can get this whole kit for only $30USD (link)

Step 1: Understanding the Parts

There are a few main parts to this scale assembly, here we will talk about each one and what role it plays.

Load Cell

A load cell is the metal structure shown above. They come in all different shapes and sizes (examples) and they make it quite easy to measure weight/forces. The main component of a load cell is called a strain gauge which can look something like this and is embedded in the white portion of the load cell. The strain gauge is a resistor that has a variable resistance based on force. Here are a few videos that do a reasonable job of explaining this:

Luckily, when using Phidgets, you don't have to worry too much about the technical aspects of this, you can just plug it into the correct Phidget (Wheatstone Bridge Phidget) and start measuring weight.

Wheatstone Bridge Phidget

You connect the wires from your Load Cell into this Phidget. As mentioned in the videos above, the load cell produces a very small signal and that signal needs to be amplified, digitized, etc. This Phidget does all of that for you and when you connect to it in your software, it gives you a nice stable final result.

Scale Platform

The platform is a convenient way to mount your load cell. When you try to make your own scales, you often have to use multiple nuts/bolts to get things level and working properly. With this platform, you just swap in and out your load cells and go.

Step 2: Setup

Your scale will come with a 5kg load cell already pre-installed. Connect it to your Wheatstone Bridge Phidget in the following way:

  • Red wire to 5V
  • Black wire to G
  • White wire to -
  • Green wire to +

After that, just connect it to your VINT Hub and then to your computer

Step 3: Write Code - Python

You can code against Phidgets in most mainstream programming languages (Java, Python, C#, Swift, C, etc.), but for this project, we will be using Python.

Create a script and copy the following code into it:

# Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
# Required for sleep statement
import time

def zeroScale():
    print("Zeroing scale, remove all items from scale!")
    print("Starting in 3..")
    time.sleep(1)
    print("2..")
    time.sleep(1)
    print("1..")
    time.sleep(1)
    count = 0
    avg = 0
    while(count < 64):
        avg += scale.getVoltageRatio()
        count += 1
        time.sleep(scale.getDataInterval()/1000.0) #sleep until new data is ready
    scale.offsetVal = avg/count
    print("Offset val: " + str(scale.offsetVal)) #In python you can just add onto an existing class

# Create
scale = VoltageRatioInput()

#Open
scale.openWaitForAttachment(1000)

#Set Data Interval to Minimum for quicker data
scale.setDataInterval(scale.getMinDataInterval())

zeroScale()

# Use your Phidgets
while (True):
    weight = 4700 * (scale.getVoltageRatio() - scale.offsetVal)
    # Display Weight
    print("%.3f kg" % weight)
    time.sleep(0.25) #20ms is fastest we can go, but only do 250ms because it is easier to read

Hit run and you will see a weight printed out in kg.

Step 4: Code Review

Here is a quick breakdown of the Python script:

zeroScale()

This function is used to zero/tare your scale. It averages the output from the scale (64 samples) when it is empty and saves the value as an offset. It subtracts this offset from all future calculations. You can use this at the beginning of your program to "zero out" the scale, and you could also implement a button that allows you to zero the scale periodically, or when you want to subtract a weight.

main loop

There is a main loop that runs every 0.25 seconds. It gets a sample from the scale, subtracts the offset, and converts the value from a voltage ratio to weight by multiplying by a provided slope (units: kg/voltage ratio).

This slope is an estimate for the load cell and in step 6 you can optionally calculate it for yourself.

Step 5: Result

You can see the result in the video above. In that example a Graphic LCD Phidget was used to display the weight information, however, it is not required. That video tests the low end of the scale (the weight was only 12g), however, you can increase the capacity up to 25kg (55lbs) with this scale.

Step 6: Improving Accuracy

In order to improve the accuracy of your scale, you should perform a two-point calibration. The two points should be as follows:

  • point 1: no weight
  • point 2: about 25-50% of the full scale

This program does all the hard work for you, you just need to follow the directions:

#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)))
  


When you get your new slope value, you can replace the previous slope value of 4700:

weight = 4700 * (scale.getVoltageRatio() - offsetValue)

With the new value the program calculated

Step 7: Next Steps

You now have a scale that you can code against in any mainstream programming language. The IOT possibilities are endless. Leave a comment below if you have any questions and check back on this account for some scale projects.