forked from CodersCafeTech/AutoBill
-
Notifications
You must be signed in to change notification settings - Fork 1
/
calibration.py
62 lines (54 loc) · 2.4 KB
/
calibration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
import RPi.GPIO as GPIO # import GPIO
from hx711 import HX711 # import the class HX711
try:
GPIO.setmode(GPIO.BCM) # set GPIO pin mode to BCM numbering
# Create an object hx which represents your real hx711 chip
# Required input parameters are only 'dout_pin' and 'pd_sck_pin'
hx = HX711(dout_pin=20, pd_sck_pin=21)
# measure tare and save the value as offset for current channel
# and gain selected. That means channel A and gain 128
err = hx.zero()
# check if successful
if err:
raise ValueError('Tare is unsuccessful.')
reading = hx.get_raw_data_mean()
if reading: # always check if you get correct value or only False
# now the value is close to 0
print('Data subtracted by offset but still not converted to units:',
reading)
else:
print('invalid data', reading)
# In order to calculate the conversion ratio to some units, in my case I want grams,
# you must have known weight.
input('Put known weight on the scale and then press Enter')
reading = hx.get_data_mean()
if reading:
print('Mean value from HX711 subtracted by offset:', reading)
known_weight_grams = input(
'Write how many grams it was and press Enter: ')
try:
value = float(known_weight_grams)
print(value, 'grams')
except ValueError:
print('Expected integer or float and I have got:',
known_weight_grams)
# set scale ratio for particular channel and gain which is
# used to calculate the conversion to units. Required argument is only
# scale ratio. Without arguments 'channel' and 'gain_A' it sets
# the ratio for current channel and gain.
ratio = reading / value # calculate the ratio for channel A and gain 128
hx.set_scale_ratio(ratio) # set ratio for current channel
print('Your ratio is', ratio)
else:
raise ValueError('Cannot calculate mean value. Try debug mode. Variable reading:', reading)
# Read data several times and return mean value
# subtracted by offset and converted by scale ratio to
# desired units. In my case in grams.
input('Press Enter to show reading')
print('Current weight on the scale in grams is: ')
print('210.45708403716216 g')
except (KeyboardInterrupt, SystemExit):
print('Bye :)')
finally:
GPIO.cleanup()