I'm here today because I've some problem to get weight on my PI.
I'll firstly give the connections and informations:
Materials:
Load cell : 3kg
Chip : HX711
PI : Raspberry pi 3
Load cell to HX711:
RED: E+
BLACK:E-
WHITE : A+
GREEN: A-
HX711 to PI:
GND : PIN 25 (GROUND)
DT : PIN 21 (BCM GPIO9)
SCK : PIN 23 (BCM GPIO11)
VCC: PIN 1 (3.3V)
About scripts that i used:
Code: Select all
import RPi.GPIO as GPIO
import time
import sys
import numpy # sudo apt-get python-numpy
GPIO.setwarnings(False)
class HX711:
def __init__(self, dout, pd_sck, gain=32):
self.PD_SCK = pd_sck
self.DOUT = dout
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.PD_SCK, GPIO.OUT)
GPIO.setup(self.DOUT, GPIO.IN)
self.GAIN = 0
self.REFERENCE_UNIT = 1 # The value returned by the hx711 that corresponds to your reference unit AFTER dividing by the SCALE.
self.OFFSET = 1
self.lastVal = 0
self.LSByte = [2, -1, -1]
self.MSByte = [0, 3, 1]
self.MSBit = [0, 8, 1]
self.LSBit = [7, -1, -1]
self.byte_range_values = self.LSByte
self.bit_range_values = self.MSBit
self.set_gain(gain)
time.sleep(1)
def is_ready(self):
return GPIO.input(self.DOUT) == 0
def set_gain(self, gain):
if gain is 128:
self.GAIN = 1
elif gain is 64:
self.GAIN = 3
elif gain is 32:
self.GAIN = 2
GPIO.output(self.PD_SCK, False)
self.read()
def createBoolList(self, size=8):
ret = []
for i in range(8):
ret.append(False)
return ret
def read(self):
while not self.is_ready():
#print("WAITING")
pass
dataBits = [self.createBoolList(), self.createBoolList(), self.createBoolList()]
dataBytes = [0x0] * 4
for j in range(self.byte_range_values[0], self.byte_range_values[1], self.byte_range_values[2]):
for i in range(self.bit_range_values[0], self.bit_range_values[1], self.bit_range_values[2]):
GPIO.output(self.PD_SCK, True)
dataBits[j][i] = GPIO.input(self.DOUT)
GPIO.output(self.PD_SCK, False)
dataBytes[j] = numpy.packbits(numpy.uint8(dataBits[j]))
#set channel and gain factor for next reading
for i in range(self.GAIN):
GPIO.output(self.PD_SCK, True)
GPIO.output(self.PD_SCK, False)
#check for all 1
#if all(item is True for item in dataBits[0]):
# return long(self.lastVal)
dataBytes[2] ^= 0x80
return dataBytes
def get_binary_string(self):
binary_format = "{0:b}"
np_arr8 = self.read_np_arr8()
binary_string = ""
for i in range(4):
# binary_segment = binary_format.format(np_arr8[i])
binary_segment = format(np_arr8[i], '#010b')
binary_string += binary_segment + " "
return binary_string
def get_np_arr8_string(self):
np_arr8 = self.read_np_arr8()
np_arr8_string = "[";
comma = ", "
for i in range(4):
if i is 3:
comma = ""
np_arr8_string += str(np_arr8[i]) + comma
np_arr8_string += "]";
return np_arr8_string
def read_np_arr8(self):
dataBytes = self.read()
np_arr8 = numpy.uint8(dataBytes)
return np_arr8
def read_long(self):
np_arr8 = self.read_np_arr8()
np_arr32 = np_arr8.view('uint32')
self.lastVal = np_arr32
return self.lastVal
def read_average(self, times=3):
values = 0
for i in range(times):
values += self.read_long()
return values / times
def get_value(self, times=3):
return self.read_average(times) - self.OFFSET
def get_weight(self, times=3):
value = self.get_value(times)
value = value / self.REFERENCE_UNIT
return value
def tare(self, times=15):
# Backup REFERENCE_UNIT value
reference_unit = self.REFERENCE_UNIT
self.set_reference_unit(1)
value = self.read_average(times)
self.set_offset(value)
self.set_reference_unit(reference_unit)
def set_reading_format(self, byte_format="LSB", bit_format="MSB"):
if byte_format == "LSB":
self.byte_range_values = self.LSByte
elif byte_format == "MSB":
self.byte_range_values = self.MSByte
if bit_format == "LSB":
self.bit_range_values = self.LSBit
elif bit_format == "MSB":
self.bit_range_values = self.MSBit
def set_offset(self, offset):
self.OFFSET = offset
def set_reference_unit(self, reference_unit):
self.REFERENCE_UNIT = reference_unit
# HX711 datasheet states that setting the PDA_CLOCK pin on high for a more than 60 microseconds would power off the chip.
# I used 100 microseconds, just in case.
# I've found it is good practice to reset the hx711 if it wasn't used for more than a few seconds.
def power_down(self):
GPIO.output(self.PD_SCK, False)
GPIO.output(self.PD_SCK, True)
time.sleep(0.0001)
def power_up(self):
GPIO.output(self.PD_SCK, False)
time.sleep(0.0001)
def reset(self):
self.power_down()
self.power_up()
Code: Select all
import RPi.GPIO as GPIO
import time
import sys
from weight import HX711
def cleanAndExit():
print("Cleaning...")
GPIO.cleanup()
print("Bye!")
sys.exit()
hx = HX711(9, 11)
# I've found out that, for some reason, the order of the bytes is not always the same between versions of python, numpy and the hx711 itself.
# Still need to figure out why does it change.
# If you're experiencing super random values, change these values to MSB or LSB until to get more stable values.
# There is some code below to debug and log the order of the bits and the bytes.
# The first parameter is the order in which the bytes are used to build the "long" value.
# The second paramter is the order of the bits inside each byte.
# According to the HX711 Datasheet, the second parameter is MSB so you shouldn't need to modify it.
hx.set_reading_format("LSB", "MSB")
# HOW TO CALCULATE THE REFFERENCE UNIT
# To set the reference unit to 1. Put 1kg on your sensor or anything you have and know exactly how much it weights.
# In this case, 92 is 1 gram because, with 1 as a reference unit I got numbers near 0 without any weight
# and I got numbers around 184000 when I added 2kg. So, according to the rule of thirds:
# If 2000 grams is 184000 then 1000 grams is 184000 / 2000 = 92.
#hx.set_reference_unit(113)
hx.set_reference_unit(1)
hx.reset()
hx.tare()
while True:
try:
# These three lines are usefull to debug wether to use MSB or LSB in the reading formats
# for the first parameter of "hx.set_reading_format("LSB", "MSB")".
# Comment the two lines "val = hx.get_weight(5)" and "print val" and uncomment the three lines to see what it prints.
#np_arr8_string = hx.get_np_arr8_string()
#binary_string = hx.get_binary_string()
#print binary_string + " " + np_arr8_string
# Prints the weight. Comment if you're debbuging the MSB and LSB issue.
val = hx.get_weight(5)
print(val)
hx.power_down()
hx.power_up()
time.sleep(0.5)
except (KeyboardInterrupt, SystemExit):
cleanAndExit()
Some now here are my problems:
First of all : What mean "gain", when i change him, i have totally differents values with less or more fluctuation.
Second : I've my load cell just put on my table without any weight, but i have negative result with reference unit to 1 and gain to 32 as follow :
python3 testWeight.py
[-493.06666667]
[-489.06666667]
[-493.06666667]
[-503.06666667]
[-506.66666667]
[-1284.66666667]
[-488.46666667]
[-929.86666667]
[-544.66666667]
[-538.06666667]
[-92.66666667]
[-496.66666667]
[-611.86666667]
[-502.46666667]
[-473.66666667]
[-491.46666667]
Cleaning...
Bye!
We can see an average around -500 but there is some strange fluctuation such as "-92, -1284" so i don't understand the fluctuation and the negative value.
Thirdly : How should i calibrate the weight sensor to get the reference unit, i mean when i set an objet on the load cell, values stayed unchange..
Thank you for your help and tell me if you need more informations
Have a good day.