-
Notifications
You must be signed in to change notification settings - Fork 28
/
hx711.py
244 lines (176 loc) · 6.3 KB
/
hx711.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# -*- coding: utf-8 -*-
# (c) 2017-2018 David Gerber <https://github.com/geda>
# HX711 library for the LoPy.
# https://github.com/geda/hx711-lopy
import utime
from terkin import logging
from machine import Pin, enable_irq, disable_irq, idle
from terkin.util import get_platform_info
platform_info = get_platform_info()
log = logging.getLogger(__name__)
class HX711:
"""Baseline driver for the HX711 by David Gerber, with modifications.
https://cdn.sparkfun.com/datasheets/Sensors/ForceFlex/hx711_english.pdf
"""
def __init__(self, dout, pd_sck, gain=128):
# Define two pins for clock and data.
if platform_info.vendor == platform_info.MICROPYTHON.Vanilla:
self.pSCK = Pin(int(pd_sck[1:]), mode=Pin.OUT)
self.pOUT = Pin(int(dout[1:]), mode=Pin.IN, pull=Pin.PULL_UP)
elif platform_info.vendor == platform_info.MICROPYTHON.Pycom:
self.pSCK = Pin(pd_sck, mode=Pin.OUT)
self.pOUT = Pin(dout, mode=Pin.IN, pull=Pin.PULL_UP)
else:
raise NotImplementedError('HX711 is '
'not implemented on this platform')
self.initialized = False
self.GAIN = 0
self.OFFSET = 0
self.SCALE = 1
self.time_constant = 0.1
self.filtered = None
self.set_gain(gain)
def set_gain(self, gain):
"""
:param gain:
"""
if gain is 128:
self.GAIN = 1
elif gain is 64:
self.GAIN = 3
elif gain is 32:
self.GAIN = 2
def is_ready(self):
""" """
return self.pOUT() == 0
def initialize(self):
"""Power and initialize the chip.
Wait for becoming ready.
"""
if not self.initialized:
# Wake up the HX711.
self.power_up()
# Wait for device to become ready.
log.info('Initialization started')
self.wait_ready()
self.initialized = True
log.info('Initialization succeeded')
return True
def wait_ready(self):
""" """
retries = 10000
#log.info('Waiting for device to become ready')
while not self.is_ready():
idle()
retries -= 1
if retries == 0:
raise DeviceNotFound('HX711 not ready')
#log.info('Device ready')
def read(self):
"""This chip has a non-standard serial protocol.
Serial Interface
----------------
Pin PD_SCK and DOUT are used for data retrieval, input selection,
gain selection and power down controls.
When output data is not ready for retrieval, digital output pin DOUT
is high. Serial clock input PD_SCK should be low. When DOUT goes to
low, it indicates data is ready for retrieval.
By applying 25~27 positive clock pulses at the PD_SCK pin, data is
shifted out from the DOUT output pin. Each PD_SCK pulse shifts out
one bit, starting with the MSB bit first, until all 24 bits are
shifted out. The 25th pulse at PD_SCK input will pull DOUT pin back
to high.
"""
# Initialize the hardware once.
# Otherwise, croak with ``DeviceNotFound('HX711 not available')``.
if not self.initialize():
# Wait for the device becoming ready.
self.wait_ready()
# Shift in data, gain & channel info.
result = 0
for j in range(24 + self.GAIN):
state = disable_irq()
self.pSCK(True)
self.pSCK(False)
result = (result << 1) | self.pOUT()
enable_irq(state)
# Shift back the extra bits.
result >>= self.GAIN
# Check sign.
if result > 0x7fffff:
result -= 0x1000000
return result
def read_average(self, times=3):
"""
:param times: (Default value = 3)
"""
sum = 0
for i in range(times):
sum += self.read()
return sum / times
def read_lowpass(self):
""" """
if self.filtered is None:
self.filtered = self.read()
self.filtered += self.time_constant * (self.read() - self.filtered)
return self.filtered
def get_value(self, times=3):
"""
:param times: (Default value = 3)
"""
return self.read_average(times) - self.OFFSET
def get_units(self, times=3):
"""
:param times: (Default value = 3)
"""
return self.get_value(times) / self.SCALE
def tare(self, times=15):
"""
:param times: (Default value = 15)
"""
sum = self.read_average(times)
self.set_offset(sum)
def set_scale(self, scale):
"""
:param scale:
"""
self.SCALE = scale
def set_offset(self, offset):
"""
:param offset:
"""
self.OFFSET = offset
def set_time_constant(self, time_constant = None):
"""
:param time_constant: (Default value = None)
"""
if time_constant is None:
return self.time_constant
elif 0 < time_constant < 1.0:
self.time_constant = time_constant
def power_up(self):
"""When PD_SCK Input is low, chip is in normal working mode."""
# Unfreeze pin hold when coming from deep sleep.
# https://community.hiveeyes.org/t/strom-sparen-beim-einsatz-der-micropython-firmware-im-batteriebetrieb/2055/72
if platform_info.vendor == platform_info.MICROPYTHON.Pycom:
self.pSCK.hold(False)
log.info('HX711 power up')
self.pSCK.value(False)
#utime.sleep_us(80)
#self.initialize()
def power_down(self):
"""When PD_SCK pin changes from low to high and stays at
high for longer than 60µs, HX711 enters power down mode.
"""
log.info('HX711 power down')
state = disable_irq()
self.pSCK.value(False)
self.pSCK.value(True)
utime.sleep_us(80)
enable_irq(state)
# Hold level to HIGH, even during deep sleep.
# https://community.hiveeyes.org/t/strom-sparen-beim-einsatz-der-micropython-firmware-im-batteriebetrieb/2055/72
self.pSCK.hold(True)
class DeviceNotFound(Exception):
""" """
pass