├── package.json ├── scale.py ├── LICENSE ├── hxtest.py ├── hx711_spi.py ├── hx711_gpio.py ├── hx711_pio.py └── README.md /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "urls": [ 3 | ["hx711_gpio.py", "github:robert-hh/hx711/hx711_gpio.py"], 4 | ["hx711_pio.py", "github:robert-hh/hx711/hx711_pio.py"] 5 | ], 6 | "version": "1.0.0", 7 | "deps": [] 8 | } -------------------------------------------------------------------------------- /scale.py: -------------------------------------------------------------------------------- 1 | from machine import SPI, Pin 2 | from hx711 import HX711 3 | 4 | pin_OUT = Pin("P9", Pin.IN, pull=Pin.PULL_DOWN) 5 | pin_SCK = Pin("P10", Pin.OUT) 6 | 7 | # Default assignment: sck=Pin(10), mosi=Pin(11), miso=Pin(8) 8 | spi = SPI(0, baudrate=1000000) 9 | 10 | hx = HX711(pin_SCK, pin_OUT, spi) 11 | hx.set_scale(48.36) 12 | hx.tare() 13 | val = hx.get_units(5) 14 | print(val) 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /hxtest.py: -------------------------------------------------------------------------------- 1 | from machine import SPI, Pin 2 | # from hx711_spi import * 3 | from hx711_pio import * 4 | # from hx711 import * 5 | 6 | pin_OUT = Pin(7, Pin.IN, pull=Pin.PULL_DOWN) 7 | pin_SCK = Pin(6, Pin.OUT) 8 | # spi_SCK = Pin(14) 9 | 10 | # spi = SPI(0, mode=SPI.MASTER, baudrate=1000000, polarity=0, 11 | # phase=0, pins=(None, pin_SCK, pin_OUT)) 12 | # spi = SPI(1, baudrate=1000000, polarity=0, 13 | # phase=0, sck=spi_SCK, mosi=pin_SCK, miso=pin_OUT) 14 | 15 | # hx = HX711(pin_SCK, pin_OUT, spi) 16 | hx = HX711(pin_SCK, pin_OUT) 17 | 18 | from utime import ticks_ms, ticks_diff, sleep, sleep_ms 19 | from machine import Pin 20 | 21 | hx.OFFSET = 0 # -150000 22 | hx.set_gain(128) 23 | sleep_ms(50) 24 | 25 | data = [0 for _ in range(100)] 26 | 27 | def get_median(hx, num=100): 28 | for _ in range(num): 29 | data[_] = hx.read() 30 | data.sort() 31 | return data[num // 2] 32 | 33 | def run(loops = 100): 34 | start = ticks_ms() 35 | hx.set_gain(128) 36 | sleep_ms(50) 37 | resulta = get_median(hx, loops) 38 | hx.set_gain(32) 39 | sleep_ms(50) 40 | resultb = get_median(hx, loops) 41 | print(resulta, resultb) 42 | 43 | def run100(loops=100, delay = 1): 44 | for _ in range (loops): 45 | run(100) 46 | if delay: 47 | sleep(delay) 48 | 49 | def minmax(loops=10000, raw=True): 50 | hx.set_gain(128) 51 | middle = hx.read_average(min(loops, 1000)) 52 | hx.filtered = middle 53 | middle = abs(middle) - hx.OFFSET 54 | cnt00003 = 0 55 | cnt0001 = 0 56 | cnt0003 = 0 57 | cnt001 = 0 58 | cnt003 = 0 59 | cnt010 = 0 60 | cntx = 0 61 | print ("Average", middle) 62 | for _ in range(loops): 63 | if raw is True: 64 | val = abs(hx.read()) - hx.OFFSET 65 | else: 66 | val = abs(hx.read_lowpass()) - hx.OFFSET 67 | if middle * (1 - 0.000003) < val < middle * (1 + 0.000003): 68 | cnt00003 += 1 69 | elif middle * (1 - 0.00001) < val < middle * (1 + 0.00001): 70 | cnt0001 += 1 71 | elif middle * (1 - 0.00003) < val < middle * (1 + 0.00003): 72 | cnt0003 += 1 73 | elif middle * (1 - 0.0001) < val < middle * (1 + 0.0001): 74 | cnt001 += 1 75 | elif middle * (1 - 0.0003) < val < middle * (1 + 0.0003): 76 | cnt003 += 1 77 | elif middle * (1 - 0.001) < val < middle * (1 + 0.001): 78 | cnt010 += 1 79 | else: 80 | cntx += 1 81 | print("Really out of band at %d: %d %x" % (_, int(val), int(val))) 82 | 83 | print("+/- 0.0003%% %f\n+/- 0.001%% %f\n+/- 0.003%% %f\n+/- 0.01%% %f\n+/- 0.03%% %f\n+/- .1%% %f\nBeyond: %f" % 84 | (cnt00003/loops, cnt0001/loops, cnt0003/loops, cnt001/loops, cnt003/loops, cnt010/loops, cntx/loops)) 85 | 86 | 87 | # run() 88 | # minmax(10000) 89 | -------------------------------------------------------------------------------- /hx711_spi.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | # Copyright (c) 2019 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | from machine import idle 24 | import time 25 | 26 | 27 | class HX711: 28 | def __init__(self, clock, data, spi, gain=128): 29 | self.clock = clock 30 | self.data = data 31 | self.spi = spi 32 | 33 | self.clock(0) 34 | 35 | self.clock_25 = b'\xaa\xaa\xaa\xaa\xaa\xaa\x80' 36 | self.clock_26 = b'\xaa\xaa\xaa\xaa\xaa\xaa\xa0' 37 | self.clock_27 = b'\xaa\xaa\xaa\xaa\xaa\xaa\xa8' 38 | self.clock_table = [None, self.clock_25, self.clock_26, self.clock_27] 39 | self.gain_table = {128:1, 32:2, 64:3} 40 | self.lookup = (b'\x00\x01\x00\x00\x02\x03\x00\x00\x00\x00\x00\x00' 41 | b'\x00\x00\x00\x00\x04\x05\x00\x00\x06\x07\x00\x00' 42 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 43 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 44 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 45 | b'\x00\x00\x00\x00\x08\x09\x00\x00\x0a\x0b\x00\x00' 46 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x0c\x0d\x00\x00' 47 | b'\x0e\x0f') 48 | self.in_data = bytearray(7) 49 | 50 | self.OFFSET = 0 51 | self.SCALE = 1 52 | 53 | self.time_constant = 0.1 54 | self.filtered = 0 55 | 56 | self.set_gain(gain) 57 | 58 | def __call__(self): 59 | return self.read() 60 | 61 | def set_gain(self, gain): 62 | if gain in self.gain_table.keys(): 63 | self.MODE = self.gain_table[gain] 64 | else: 65 | self.MODE = 1 66 | self.read() 67 | self.filtered = self.read() 68 | 69 | def read(self): 70 | # wait for the device to get ready 71 | for _ in range(1000): 72 | if self.data() == 0: 73 | break 74 | time.sleep_ms(1) 75 | else: 76 | raise OSError("Sensor does not respond") 77 | 78 | # get the data and set channel and gain 79 | self.spi.write_readinto(self.clock_table[self.MODE], self.in_data) 80 | 81 | # pack the data into a single value 82 | result = 0 83 | for _ in range(6): 84 | result = (result << 4) + self.lookup[self.in_data[_] & 0x55] 85 | 86 | # return sign corrected result 87 | return result - ((result & 0x800000) << 1) 88 | 89 | def read_average(self, times=3): 90 | sum = 0 91 | for i in range(times): 92 | sum += self.read() 93 | return sum / times 94 | 95 | def read_lowpass(self): 96 | self.filtered += self.time_constant * (self.read() - self.filtered) 97 | return self.filtered 98 | 99 | def get_value(self): 100 | return self.read_lowpass() - self.OFFSET 101 | 102 | def get_units(self): 103 | return self.get_value() / self.SCALE 104 | 105 | def tare(self, times=15): 106 | self.set_offset(self.read_average(times)) 107 | 108 | def set_scale(self, scale): 109 | self.SCALE = scale 110 | 111 | def set_offset(self, offset): 112 | self.OFFSET = offset 113 | 114 | def set_time_constant(self, time_constant=None): 115 | if time_constant is None: 116 | return self.time_constant 117 | elif 0 < time_constant < 1.0: 118 | self.time_constant = time_constant 119 | 120 | def power_down(self): 121 | self.clock.value(False) 122 | self.clock.value(True) 123 | 124 | def power_up(self): 125 | self.clock.value(False) 126 | -------------------------------------------------------------------------------- /hx711_gpio.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | # Copyright (c) 2019 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | from machine import enable_irq, disable_irq, idle, Pin 24 | import time 25 | 26 | class HX711: 27 | def __init__(self, clock, data, gain=128): 28 | self.clock = clock 29 | self.data = data 30 | self.clock.value(False) 31 | 32 | self.GAIN = 0 33 | self.OFFSET = 0 34 | self.SCALE = 1 35 | 36 | self.time_constant = 0.25 37 | self.filtered = 0 38 | 39 | # determine the number of attempts to find the trigger pulse 40 | start = time.ticks_us() 41 | for _ in range(3): 42 | temp = self.data() 43 | spent = time.ticks_diff(time.ticks_us(), start) 44 | self.__wait_loop = 3_000_000 // spent 45 | 46 | self.set_gain(gain); 47 | 48 | def __call__(self): 49 | return self.read() 50 | 51 | def set_gain(self, gain): 52 | if gain is 128: 53 | self.GAIN = 1 54 | elif gain is 64: 55 | self.GAIN = 3 56 | elif gain is 32: 57 | self.GAIN = 2 58 | 59 | self.read() 60 | self.filtered = self.read() 61 | 62 | def conversion_done_cb(self, data): 63 | self.conversion_done = True 64 | data.irq(handler=None) 65 | 66 | def read(self): 67 | if hasattr(self.data, "irq"): 68 | self.conversion_done = False 69 | self.data.irq(trigger=Pin.IRQ_FALLING, handler=self.conversion_done_cb) 70 | # wait for the device being ready 71 | for _ in range(500): 72 | if self.conversion_done == True: 73 | break 74 | time.sleep_ms(1) 75 | else: 76 | self.data.irq(handler=None) 77 | raise OSError("Sensor does not respond") 78 | else: 79 | # wait polling for the trigger pulse 80 | for _ in range(self.__wait_loop): 81 | if self.data(): 82 | break 83 | else: 84 | raise OSError("No trigger pulse found") 85 | for _ in range(5000): 86 | if not self.data(): 87 | break 88 | time.sleep_us(100) 89 | else: 90 | raise OSError("Sensor does not respond") 91 | 92 | # shift in data, and gain & channel info 93 | result = 0 94 | for j in range(24 + self.GAIN): 95 | state = disable_irq() 96 | self.clock(True) 97 | self.clock(False) 98 | enable_irq(state) 99 | result = (result << 1) | self.data() 100 | 101 | # shift back the extra bits 102 | result >>= self.GAIN 103 | 104 | # check sign 105 | if result > 0x7fffff: 106 | result -= 0x1000000 107 | 108 | return result 109 | 110 | def read_average(self, times=3): 111 | sum = 0 112 | for i in range(times): 113 | sum += self.read() 114 | return sum / times 115 | 116 | def read_lowpass(self): 117 | self.filtered += self.time_constant * (self.read() - self.filtered) 118 | return self.filtered 119 | 120 | def get_value(self): 121 | return self.read_lowpass() - self.OFFSET 122 | 123 | def get_units(self): 124 | return self.get_value() / self.SCALE 125 | 126 | def tare(self, times=15): 127 | self.set_offset(self.read_average(times)) 128 | 129 | def set_scale(self, scale): 130 | self.SCALE = scale 131 | 132 | def set_offset(self, offset): 133 | self.OFFSET = offset 134 | 135 | def set_time_constant(self, time_constant = None): 136 | if time_constant is None: 137 | return self.time_constant 138 | elif 0 < time_constant < 1.0: 139 | self.time_constant = time_constant 140 | 141 | def power_down(self): 142 | self.clock.value(False) 143 | self.clock.value(True) 144 | 145 | def power_up(self): 146 | self.clock.value(False) 147 | -------------------------------------------------------------------------------- /hx711_pio.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | # Copyright (c) 2021 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | from machine import Pin, Timer 24 | import time 25 | import rp2 26 | 27 | class HX711: 28 | def __init__(self, clock, data, gain=128, state_machine=0): 29 | self.clock = clock 30 | self.data = data 31 | self.clock.value(False) 32 | 33 | self.GAIN = 0 34 | self.OFFSET = 0 35 | self.SCALE = 1 36 | 37 | self.time_constant = 0.25 38 | self.filtered = 0 39 | self.sm_timer = Timer() 40 | 41 | # create the state machine 42 | self.sm = rp2.StateMachine(state_machine, self.hx711_pio, freq=1_000_000, 43 | sideset_base=self.clock, in_base=self.data, 44 | set_base=self.data, jmp_pin=self.data) 45 | 46 | self.set_gain(gain); 47 | 48 | 49 | @rp2.asm_pio( 50 | sideset_init=rp2.PIO.OUT_LOW, 51 | in_shiftdir=rp2.PIO.SHIFT_LEFT, 52 | autopull=False, 53 | autopush=False, 54 | ) 55 | def hx711_pio(): 56 | label("start") 57 | pull() .side (0) # get the number of clock cycles 58 | mov(x, osr) .side (0) 59 | jmp(not_x, "power_down") .side (0) 60 | set(pindirs, 0) .side (0) # Initial set pin direction. 61 | # Wait for a high level = start of the DATA pulse 62 | wait(1, pin, 0) .side (0) 63 | # Wait for a low level = DATA signal 64 | wait(0, pin, 0) .side (0) 65 | jmp(x_dec, "bitloop").side(0) # just decrement 66 | 67 | label("bitloop") 68 | nop() .side (1)[1]# active edge 69 | in_(pins, 1) .side (0) # get the pin and shift it in 70 | jmp(x_dec, "bitloop").side (0) # test for more bits 71 | 72 | push(block) .side (0) # no, deliver data and start over 73 | jmp("start") .side (0) # And start over 74 | 75 | label("power_down") 76 | jmp("power_down") .side (1) 77 | 78 | def __call__(self): 79 | return self.read() 80 | 81 | def set_gain(self, gain): 82 | if gain is 128: 83 | self.GAIN = 1 84 | elif gain is 64: 85 | self.GAIN = 3 86 | elif gain is 32: 87 | self.GAIN = 2 88 | 89 | self.read() 90 | self.filtered = self.read() 91 | 92 | def read(self): 93 | # Feed the waiting state machine & get the data 94 | self.sm.restart() # Just in case that it is not at the start. 95 | self.sm.active(1) # start the state machine 96 | self.sm.put(self.GAIN + 24) # set pulse count 25-27, start 97 | start = time.ticks_ms() 98 | while time.ticks_diff(time.ticks_ms(), start) < 1000: 99 | # Wait for the result 100 | if self.sm.rx_fifo() > 0: 101 | break 102 | else: 103 | self.sm.active(0) # stop the state machine 104 | raise OSError("sensor timeout") 105 | 106 | result = self.sm.get(None, self.GAIN) # get the result & discard GAIN bits 107 | self.sm.active(0) # stop the state machine 108 | if result == 0x7fffffff: 109 | raise OSError("Sensor does not respond") 110 | # check sign 111 | if result > 0x7fffff: 112 | result -= 0x1000000 113 | 114 | return result 115 | 116 | def read_average(self, times=3): 117 | sum = 0 118 | for i in range(times): 119 | sum += self.read() 120 | return sum / times 121 | 122 | def read_lowpass(self): 123 | self.filtered += self.time_constant * (self.read() - self.filtered) 124 | return self.filtered 125 | 126 | def get_value(self): 127 | return self.read_lowpass() - self.OFFSET 128 | 129 | def get_units(self): 130 | return self.get_value() / self.SCALE 131 | 132 | def tare(self, times=15): 133 | self.set_offset(self.read_average(times)) 134 | 135 | def set_scale(self, scale): 136 | self.SCALE = scale 137 | 138 | def set_offset(self, offset): 139 | self.OFFSET = offset 140 | 141 | def set_time_constant(self, time_constant = None): 142 | if time_constant is None: 143 | return self.time_constant 144 | elif 0 < time_constant < 1.0: 145 | self.time_constant = time_constant 146 | 147 | def power_down(self): 148 | self.sm.restart() # Just in case that it is not at the start. 149 | self.sm.put(0) # mode power down 150 | self.sm.active(1) # start the state machine 151 | time.sleep_ms(1) 152 | self.sm.active(0) # and stop it again 153 | 154 | def power_up(self): 155 | self.sm.restart() # Just in case that it is not at the start. 156 | self.sm.active(1) # start the state machine to set clock low 157 | self.sm.active(0) # and stop it again 158 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # This repository is followed up by the HX71X repository for HX710 and HX711 2 | 3 | # HX711: Python class for the HX711 load cell 4 | 5 | This is a very short and simple class. This lib includes three variants of the 6 | module. One is using direct GPIO pin handling, the other uses the PIO 7 | module of the RPI Pico. The third variant uses SPI. 8 | Besides the class instantiation, all variants offer the same methods. 9 | The preferred variants are GPIO and PIO, because they deal properly with the conversion 10 | ready signal resulting in a more precise result. 11 | 12 | ## Constructor 13 | 14 | ### hx711 = HX711(clock_pin, data_pin, gain=128) 15 | 16 | This is the GPIO constructor. `data_pin` and `clock_pin` are the pin objects 17 | of the GPIO pins used for the communication. `clock_pin` must not be an input-only pin. 18 | `gain` is the setting of gain and channel of the load cell amplifier. 19 | The default value of 128 also selects channel A. 20 | 21 | ### hx711 = HX711(clock_pin, data_pin, gain=128, state_machine=0) 22 | 23 | This is the Raspberry Pi PIO constructor. `data_pin` and `clock_pin` are the pin objects 24 | of the GPIO pins used for the communication. 25 | `gain` is the setting of gain and channel of the load cell amplifier. 26 | The default value of 128 also selects channel A. 27 | The argument `state_machine` can be set to different values if conflicting with 28 | other uses, like if than one HX711 device is used by an application. 29 | 30 | ### hx711 = HX711(clock_pin, data_pin, spi, gain=128) 31 | 32 | This is the SPI constructor. `data_pin` is the SPI MISO, `clock_pin` the SPI MOSI. These must be 33 | Pin objects, with `data_pin` defined for input, `clock_pin` defined as output. 34 | They must be supplied in addition to the spi object, even if spi 35 | uses the same pins for miso and mosi. 36 | `spi` is the SPI object. The spi clock signal will not be be used. 37 | `gain` is the setting of gain and channel of the load cell amplifier. 38 | The default value of 128 also selects channel A. 39 | 40 | ## Methods 41 | 42 | ### hx711.set_gain(gain) 43 | 44 | Sets the gain and channel configuration which is used for the next call of hx711.read() 45 | 46 | |Gain|Channel| 47 | |:-:|:-:| 48 | |128|A| 49 | |64|A| 50 | |32|B| 51 | 52 | ### result = hx711.read() 53 | ### result = hx711() 54 | 55 | Returns the actual raw value of the load cell. Raw means: not scaled, no offset 56 | compensation. 57 | 58 | ### result = hx711.read_average(times=3) 59 | 60 | Returns the raw value of the load cell as the average of `times` readings of The 61 | raw value. 62 | 63 | ### result = hx711.read_lowpass() 64 | 65 | Returns the actual value of the load cell fed through an one stage IIR lowpass 66 | filter. The properties of the filter can be set with set_time_constant(). 67 | 68 | ### rh = hx711.set_time_constant(value=None) 69 | 70 | Set the time constant used by hx711.read_lowpass(). The range is 0-1.0. Smaller 71 | values means longer times to settle and better smoothing. 72 | If `value` is None, the actual value of the time constant is returned. 73 | 74 | ### value = hx711.get_value() 75 | 76 | Returns the difference of the filtered load cell value and the offset, as set by hx711.set_offset() or hx711.tare() 77 | 78 | ### units = hx711.get_units() 79 | 80 | Returns the value delivered by hx711.get_value() divided by the scale set by 81 | hx711.set_scale(). 82 | 83 | ### hx711.tare(times=15) 84 | 85 | Determine the tare value of the load cell by averaging `times` raw readings. 86 | 87 | ### hx711.power_down() 88 | 89 | Set the load cell to sleep mode. 90 | With the hx711_spi.py version this method may work only with SoftSPI. 91 | 92 | ### hx711.power_up() 93 | 94 | Switch the load cell on again. 95 | With the hx711_spi.py version this method may work only with SoftSPI. 96 | 97 | 98 | ## Examples 99 | 100 | ``` 101 | # Example for Pycom device, gpio mode 102 | # Connections: 103 | # Pin # | HX711 104 | # ------|----------- 105 | # P9 | data_pin 106 | # P10 | clock_pin 107 | # 108 | 109 | from hx711_gpio import HX711 110 | from machine import Pin 111 | 112 | pin_OUT = Pin("P9", Pin.IN, pull=Pin.PULL_DOWN) 113 | pin_SCK = Pin("P10", Pin.OUT) 114 | 115 | hx711 = HX711(pin_SCK, pin_OUT) 116 | 117 | hx711.tare() 118 | value = hx711.read() 119 | value = hx711.get_value() 120 | ``` 121 | 122 | ``` 123 | # Example for micropython.org device, gpio mode 124 | # Connections: 125 | # Pin # | HX711 126 | # ------|----------- 127 | # 12 | data_pin 128 | # 13 | clock_pin 129 | # 130 | 131 | from hx711_gpio import HX711 132 | from machine import Pin 133 | 134 | pin_OUT = Pin(12, Pin.IN, pull=Pin.PULL_DOWN) 135 | pin_SCK = Pin(13, Pin.OUT) 136 | 137 | hx711 = HX711(pin_SCK, pin_OUT) 138 | 139 | hx711.tare() 140 | value = hx711.read() 141 | value = hx711.get_value() 142 | ``` 143 | 144 | ``` 145 | # Example for micropython.org device, RP2040 PIO mode 146 | # Connections: 147 | # Pin # | HX711 148 | # ------|----------- 149 | # 12 | data_pin 150 | # 13 | clock_pin 151 | # 152 | 153 | from hx711_pio import HX711 154 | from machine import Pin 155 | 156 | pin_OUT = Pin(12, Pin.IN, pull=Pin.PULL_DOWN) 157 | pin_SCK = Pin(13, Pin.OUT) 158 | 159 | hx711 = HX711(pin_SCK, pin_OUT, state_machine=0) 160 | 161 | hx711.tare() 162 | value = hx711.read() 163 | value = hx711.get_value() 164 | ``` 165 | 166 | ``` 167 | # Example for Pycom device, spi mode 168 | # Connections: 169 | # Pin # | HX711 170 | # ------|----------- 171 | # P9 | data_pin 172 | # P10 | clock_pin 173 | # None | spi clock 174 | # 175 | 176 | from hx711_spi import HX711 177 | from machine import Pin, SPI 178 | 179 | pin_OUT = Pin("P9", Pin.IN, pull=Pin.PULL_DOWN) 180 | pin_SCK = Pin("P10", Pin.OUT) 181 | 182 | spi = SPI(0, mode=SPI.MASTER, baudrate=1000000, polarity=0, 183 | phase=0, pins=(None, pin_SCK, pin_OUT)) 184 | 185 | hx = HX711(pin_SCK, pin_OUT, spi) 186 | 187 | hx711.tare() 188 | value = hx711.read() 189 | value = hx711.get_value() 190 | ``` 191 | 192 | ``` 193 | # Example for micropython.org device, spi mode 194 | # Connections: 195 | # Pin # | HX711 196 | # ------|----------- 197 | # 12 | data_pin 198 | # 13 | clock_pin 199 | # 14 | spi clock 200 | 201 | from hx711_spi import HX711 202 | from machine import Pin 203 | 204 | pin_OUT = Pin(12, Pin.IN, pull=Pin.PULL_DOWN) 205 | pin_SCK = Pin(13, Pin.OUT) 206 | spi_SCK = Pin(14) 207 | 208 | spi = SPI(1, baudrate=1000000, polarity=0, 209 | phase=0, sck=spi_SCK, mosi=pin_SCK, miso=pin_OUT) 210 | 211 | hx = HX711(pin_SCK, pin_OUT, spi) 212 | 213 | hx711.tare() 214 | value = hx711.read() 215 | value = hx711.get_value() 216 | ``` 217 | --------------------------------------------------------------------------------