We are currently trying to connect a HX711 ADC with weight sensors to a Verdin iMX8MPlus on a Dahlia Carrier Board and read values using a Python script, but have been unsuccesful with it. The ADC was working fine with multiple microcontrollers before and because it uses a simple serial interface I assumed the GPIO on the Dahlia carrier board would work fine with it.
Does anybody now which Python/GPIO library works with it?
Hi @Jakob!
Welcome to our community! Feel free to ask us any questions.
Regarding your problem, what exact problem are you seeing? Do you see any log errors that you can share with us?
Also, which OS are you using? BSP, Torizon?
I checked here, and there is a driver for HX711 in our Linux kernel, you can check it here: hx711.c « adc « iio « drivers - linux-toradex.git - Linux kernel for Apalis, Colibri and Verdin modules
But it is not enabled by default.
If you’re using Yocto, you could make a custom kernel with this driver enabled and add it to your image.
However, if you’re using Torizon, we could ask for our development team to enable this driver in one of our nightly builds for you.
If you don’t want to use this driver or customize your kernel though, a python library with the ADC should work. Which pin are you trying to use? Please, give us more information about your setup.
Best regards,
Hiago.
We are using Torizon
This is the python script we are using.
It was meant for different hardware and it was then edited by us.
original version here: Jetson Nano 2GB Developer Kit User Guide | NVIDIA Developer
We are using GPIO_1 (pin 27) for DOUT and GPIO_2 (pin 28) for SCK
atm the script only returns zeros as data
import gpiod
import time
from typing import Dict, List
import threading
import logging
DEFAULT_GPIOD_CONSUMER = 'hx711'
class HX711:
def __init__(self, line_dout: int, line_pd_sck: int, gain: int = 128, mutex: bool = False, chip=None):
self.chip = chip
if self.chip is None:
self.chip = gpiod.chip("0", gpiod.chip.OPEN_BY_NUMBER)
self.PD_SCK = self.chip.get_line(line_pd_sck)
self.DOUT = self.chip.get_line(line_dout)
self.config_dout, self.config_pd_sck = gpiod.line_request(), gpiod.line_request()
self.config_dout.consumer = DEFAULT_GPIOD_CONSUMER
self.config_pd_sck.consumer = DEFAULT_GPIOD_CONSUMER
self.config_dout.request_type = self.config_dout.DIRECTION_INPUT
self.config_pd_sck.request_type = self.config_pd_sck.DIRECTION_OUTPUT
self.mutex_flag: bool = mutex
if self.mutex_flag:
# Mutex for reading from the HX711, in case multiple threads in client
# software try to access get values from the class at the same time.
self.readLock = threading.Lock()
# self.DOUT.set_config(direction=self.config_dout.DIRECTION_INPUT, flags=0)
# self.PD_SCK.set_config(direction=self.config_pd_sck.DIRECTION_OUTPUT, flags=0)
self.PD_SCK.request(config=self.config_pd_sck)
self.DOUT.request(config=self.config_dout)
# self.PD_SCK.request()
# self.DOUT.request()
self.GAIN: int = 0
# The value returned by the hx711 that corresponds to your reference
# unit AFTER dividing by the SCALE.
self.REFERENCE_UNIT: int = 1
self.REFERENCE_UNIT_B: int = 1
self.OFFSET: float = 1.0
self.OFFSET_B: float = 1.0
self.lastVal: float = 0.0
self.byte_format: str = 'MSB'
self.bit_format: str = 'MSB'
self.set_gain(gain)
# Think about whether this is necessary.
time.sleep(0.1)
def convertFromTwosComplement24bit(self, inputValue) -> int:
return -(inputValue & 0x800000) + (inputValue & 0x7fffff)
def is_ready(self) -> bool:
return self.DOUT.get_value() == 0
def set_gain(self, gain):
if gain == 128:
self.GAIN = 1
elif gain == 64:
self.GAIN = 3
elif gain == 32:
self.GAIN = 2
self.PD_SCK.set_value(0)
# Read out a set of raw bytes and throw it away.
self.readRawBytes()
def get_gain(self) -> int:
if self.GAIN == 1:
return 128
if self.GAIN == 3:
return 64
if self.GAIN == 2:
return 32
# Shouldn't get here.
return 0
def readNextBit(self) -> int:
# Clock HX711 Digital Serial Clock (PD_SCK). DOUT will be
# ready 1us after PD_SCK rising edge, so we sample after
# lowering PD_SCL, when we know DOUT will be stable.
self.PD_SCK.set_value(1)
self.PD_SCK.set_value(0)
return self.DOUT.get_value()
def readNextByte(self) -> int:
byteValue: int = 0
# Read bits and build the byte from top, or bottom, depending
# on whether we are in MSB or LSB bit mode.
for x in range(8):
if self.bit_format == 'MSB':
byteValue <<= 1
byteValue |= self.readNextBit()
else:
byteValue >>= 1
byteValue |= self.readNextBit() * 0x80
# Return the packed byte.
return byteValue
def readRawBytes(self) -> List[int]:
if self.mutex_flag:
# Wait for and get the Read Lock, incase another thread is already
# driving the HX711 serial interface.
self.readLock.acquire()
# Wait until HX711 is ready for us to read a sample.
while not self.is_ready():
pass
# Read three bytes of data from the HX711.
firstByte: int = self.readNextByte()
secondByte: int = self.readNextByte()
thirdByte: int = self.readNextByte()
# HX711 Channel and gain factor are set by number of bits read
# after 24 data bits.
for i in range(self.GAIN):
# Clock a bit out of the HX711 and throw it away.
self.readNextBit()
if self.mutex_flag:
# Release the Read Lock, now that we've finished driving the HX711
# serial interface.
self.readLock.release()
# Depending on how we're configured, return an orderd list of raw byte
# values.
if self.byte_format == 'LSB':
return [thirdByte, secondByte, firstByte]
else:
return [firstByte, secondByte, thirdByte]
def read_long(self) -> int:
# Get a sample from the HX711 in the form of raw bytes.
dataBytes: list[int] = self.readRawBytes()
print(dataBytes)
# Join the raw bytes into a single 24bit 2s complement value.
twosComplementValue = ((dataBytes[0] << 16) |
(dataBytes[1] << 8) |
dataBytes[2])
print(f"Twos: 0x{twosComplementValue:06x}")
# Convert from 24bit twos-complement to a signed value.
signedIntValue: int = self.convertFromTwosComplement24bit(twosComplementValue)
# Record the latest sample value we've read.
self.lastVal = signedIntValue
# Return the sample value we've read from the HX711.
return int(signedIntValue)
def read_average(self, times: int = 3) -> float:
# Make sure we've been asked to take a rational amount of samples.
if times <= 0:
raise ValueError("HX711()::read_average(): times must >= 1!!")
# If we're only average across one value, just read it and return it.
if times == 1:
return self.read_long()
# If we're averaging across a low amount of values, just take the
# median.
if times < 5:
return self.read_median(times)
# If we're taking a lot of samples, we'll collect them in a list, remove
# the outliers, then take the mean of the remaining set.
valueList: List[int] = []
for x in range(times):
valueList += [self.read_long()]
valueList.sort()
# We'll be trimming 20% of outlier samples from top and bottom of collected set.
trimAmount: int = int(len(valueList) * 0.2)
# Trim the edge case values.
valueList: list = valueList[trimAmount:-trimAmount]
# Return the mean of remaining samples.
return sum(valueList) / len(valueList)
# A median-based read method, might help when getting random value spikes
# for unknown or CPU-related reasons
def read_median(self, times: int = 3) -> float:
if times <= 0:
raise ValueError("HX711::read_median(): times must be greater than zero!")
# If times == 1, just return a single reading.
if times == 1:
return self.read_long()
valueList: List[int] = []
for x in range(times):
valueList += [self.read_long()]
valueList.sort()
# If times is odd we can just take the centre value.
if (times & 0x1) == 0x1:
return valueList[len(valueList) // 2]
else:
# If times is even we have to take the arithmetic mean of
# the two middle values.
midpoint: int = len(valueList) / 2
return sum(valueList[midpoint:midpoint + 2]) / 2.0
# Compatibility function, uses channel A version
def get_value(self, times: int = 3) -> float:
return self.get_value_A(times)
def get_value_A(self, times: int = 3) -> float:
return self.read_median(times) - self.get_offset_A()
def get_value_B(self, times: int = 3) -> float:
# for channel B, we need to set_gain(32)
g: int = self.get_gain()
self.set_gain(32)
value = self.read_median(times) - self.get_offset_B()
self.set_gain(g)
return value
# Compatibility function, uses channel A version
def get_weight(self, times: int = 3) -> float:
return self.get_weight_A(times)
def get_weight_A(self, times: int = 3) -> float:
value: float = self.get_value_A(times)
value = value / self.REFERENCE_UNIT
return value
def get_weight_B(self, times: int = 3) -> float:
value: float = self.get_value_B(times)
value = value / self.REFERENCE_UNIT_B
return value
# Sets tare for channel A for compatibility purposes
def tare(self, times: int = 15) -> float:
return self.tare_A(times)
def tare_A(self, times: int = 15) -> float:
# Backup REFERENCE_UNIT value
backupReferenceUnit: int = self.get_reference_unit_A()
self.set_reference_unit_A(1)
value: float = self.read_average(times)
print(f"Tare A value: {value}")
self.set_offset_A(value)
# Restore the reference unit, now that we've got our offset.
self.set_reference_unit_A(backupReferenceUnit)
return value
def tare_B(self, times: int = 15) -> float:
# Backup REFERENCE_UNIT value
backupReferenceUnit: int = self.get_reference_unit_B()
self.set_reference_unit_B(1)
# for channel B, we need to set_gain(32)
backupGain: int = self.get_gain()
self.set_gain(32)
value: float = self.read_average(times)
print(f"Tare B value:{value}")
self.set_offset_B(value)
# Restore gain/channel/reference unit settings.
self.set_gain(backupGain)
self.set_reference_unit_B(backupReferenceUnit)
return value
def set_reading_format(self, byte_format: str = "LSB", bit_format: str = "MSB"):
if byte_format == "LSB":
self.byte_format = byte_format
elif byte_format == "MSB":
self.byte_format = byte_format
else:
raise ValueError("Unrecognised byte_format: \"%s\"" % byte_format)
if bit_format == "LSB":
self.bit_format = bit_format
elif bit_format == "MSB":
self.bit_format = bit_format
else:
raise ValueError("Unrecognised bit format: \"%s\"" % bit_format)
# sets offset for channel A for compatibility reasons
def set_offset(self, offset: float):
self.set_offset_A(offset)
def set_offset_A(self, offset: float):
self.OFFSET = offset
def set_offset_B(self, offset: float):
self.OFFSET_B = offset
def get_offset(self) -> float:
return self.get_offset_A()
def get_offset_A(self) -> float:
return self.OFFSET
def get_offset_B(self) -> float:
return self.OFFSET_B
def set_reference_unit(self, reference_unit: int):
self.set_reference_unit_A(reference_unit)
def set_reference_unit_A(self, reference_unit: int):
# Make sure we aren't asked to use an invalid reference unit.
if reference_unit == 0:
raise ValueError("HX711::set_reference_unit_A() can't accept 0 as a reference unit!")
return
self.REFERENCE_UNIT = reference_unit
def set_reference_unit_B(self, reference_unit: int):
# Make sure we aren't asked to use an invalid reference unit.
if reference_unit == 0:
raise ValueError("HX711::set_reference_unit_A() can't accept 0 as a reference unit!")
return
self.REFERENCE_UNIT_B = reference_unit
def get_reference_unit(self) -> int:
return self.get_reference_unit_A()
def get_reference_unit_A(self) -> int:
return self.REFERENCE_UNIT
def get_reference_unit_B(self) -> int:
return self.REFERENCE_UNIT_B
def power_down(self):
if self.mutex_flag:
# Wait for and get the Read Lock, incase another thread is already
# driving the HX711 serial interface.
self.readLock.acquire()
# Cause a rising edge on HX711 Digital Serial Clock (PD_SCK). We then
# leave it held up and wait 100 us. After 60us the HX711 should be
# powered down.
self.PD_SCK.set_value(0)
self.PD_SCK.set_value(1)
time.sleep(0.0001)
if self.mutex_flag:
# Release the Read Lock, now that we've finished driving the HX711
# serial interface.
self.readLock.release()
def power_up(self):
if self.mutex_flag:
# Wait for and get the Read Lock, incase another thread is already
# driving the HX711 serial interface.
self.readLock.acquire()
# Lower the HX711 Digital Serial Clock (PD_SCK) line.
self.PD_SCK.set_value(0)
# Wait 100 us for the HX711 to power back up.
time.sleep(0.0001)
if self.mutex_flag:
# Release the Read Lock, now that we've finished driving the HX711
# serial interface.
self.readLock.release()
# HX711 will now be defaulted to Channel A with gain of 128. If this
# isn't what client software has requested from us, take a sample and
# throw it away, so that next sample from the HX711 will be from the
# correct channel/gain.
if self.get_gain() != 128:
self.readRawBytes()
def reset(self):
self.power_down()
self.power_up()
Dear @Jakob,
Thanks for the update. Could you please confirm the following information:
- Which Verdin iMX8MP Version you have?
- Which Dahlia version you have?
- Which TorizonCore version you have?
Also, did you have the time to look on the following article on our developer page: How to Use GPIO on TorizonCore | Toradex Developer Center ?
You could also check the datasheets (https://docs.toradex.com/108784-verdin_imx8m_plus_datasheet.pdf) of the carrier board (https://docs.toradex.com/109590-dahlia_datasheet_v1.1.pdf) and of the module to check for more information on it.
In addition, have you added the gpio device to the container? And gpiod to the requirements.txt file?
Finally, what’s the output of the following command on U-boot: printenv fdt_board
?
Best regards,
Verdin iMX8MP V1.0D
Dahlia V1.1C
TorizonCore
We did add gpio device to the container and also added gpiod to the requirements.txt file
Dear @Jakob,
Could you please confirm the exact TorizonCore version? We’d also need the output of the command on U-boot: printenv fdt_board
. Please share all the information you could have so that we can better understand what could be triggering your issue.
Also, do you get any logs on your application? Have you tried setting debug points?
Did you test that the desired GPIO’s are running on the command line as shown here How to Use GPIO on TorizonCore | Toradex Developer Center?
Hello,
I am Jakob’s colleague and I would like to know how to get information about TorizonCore version? The command “printenv fdt_board” doesn’t output anything into console.
Our weight collecting script is as provided by Jakob, we are testing it by running it in a loop inside Docker container on the board. We are sure we connected all the pins correctly and yet we just get 0.0 values back from the sensor.
UPD: we just looked up the TorizonCore version, it’s supposed to be 5.6.0 build 13 if I am not mistaken.
Dear @kolesnic,
Thanks for the update and also welcome to our community ! To check the
fdt_board
value you should be in U-Boot. Not in the usual console.
Could you please test the GPIO’s outside of your application like described here: How to Use GPIO on TorizonCore | Toradex Developer Center? This way, we can ensure that the GPIO’s are working properly and then the problem could be on the application side.
Also as a sanity check, when you created your application, did you mount the GPIO devices that you try to use? You could also have a look on our Python GPIO Sample for Torizon: torizon-samples/gpio/python at bullseye · toradex/torizon-samples · GitHub
Please tell me if this helps.
Thanks for the reply,
We of course mounted the ‘gpiochip’ devices we are using and for sure using correct pins by following examples you have provided. I have tried to run our application as well as getting an output from the sensor via command line and Python interpreter and so far it was only outputting zero values.
I will try out weight sensor on our previous device (RasPi) to check whether it works at all and will update the thread with status asap.
Dear @kolesnic,
Thanks for the update. Please tell me if the sensor is properly working.
Best regards,
It seems we have found the problem. We did not consider the GPIO voltage at all until now.
The Raspberry Pi we were using for early prototyping has a GPIO voltage of 3.3V. The Dahlia Carrier Board however only has 1.8V. 1.8V is too low for the HX711 to register a HIGH signal. It needs 2.7 to 5.5 V.
So my question is now: Is there a way to change the GPIO voltage of the Dahlia Carrier Board to 3.3V?
How did you guys make the HX711 ADC work with your driver?
Hello!!
We check the sensor with same code that we used in the toradex but it works fine in the raspiberry pi. If we run the code in the Toradex we just get 0 values. Below is the code we are using. Do we have change any configuration in GPIO pin to make it work?
#
import gpiod
import time
import threading
class HX711:
def __init__(self, dout, pd_sck, gain=128):
self.PD_SCK = pd_sck
self.DOUT = dout
# Mutex for reading from the HX711, in case multiple threads in client
# software try to access get values from the class at the same time.
self.readLock = threading.Lock()
self.WEIGHT_CHIP = "gpiochip0"
self.DATA_LINE_OFFSET = 6
self.CLOCK_LINE_OFFSET = 5
self.chip = gpiod.chip(self.WEIGHT_CHIP)
self.data = self.chip.get_line(self.DATA_LINE_OFFSET)
self.clock = self.chip.get_line(self.CLOCK_LINE_OFFSET)
self.config_clock = gpiod.line_request()
self.config_clock.consumer = "clock"
self.config_clock.request_type = gpiod.line_request.DIRECTION_OUTPUT
self.config_data = gpiod.line_request()
self.config_data.consumer = "data"
self.config_data.request_type = gpiod.line_request.DIRECTION_INPUT
self.data.request(self.config_data, 0)
self.clock.request(self.config_clock)
self.GAIN = 128
# The value returned by the hx711 that corresponds to your reference
# unit AFTER dividing by the SCALE.
self.REFERENCE_UNIT = 1
self.REFERENCE_UNIT_B = 1
self.OFFSET = 1
self.OFFSET_B = 1
self.lastVal = int(0)
self.DEBUG_PRINTING = False
self.byte_format = 'MSB'
self.bit_format = 'MSB'
self.set_gain(gain)
# Think about whether this is necessary.
time.sleep(1)
def convertFromTwosComplement24bit(self, inputValue):
return -(inputValue & 0x800000) + (inputValue & 0x7fffff)
def is_ready(self):
return self.data.get_value() == 0
def set_gain(self, gain):
if gain == 128:
self.GAIN = 1
elif gain == 64:
self.GAIN = 3
elif gain == 32:
self.GAIN = 2
self.clock.set_value(0)
# Read out a set of raw bytes and throw it away.
self.readRawBytes()
def get_gain(self):
if self.GAIN == 1:
return 128
if self.GAIN == 3:
return 64
if self.GAIN == 2:
return 32
# Shouldn't get here.
return 0
def readNextBit(self):
# Clock HX711 Digital Serial Clock (PD_SCK). DOUT will be
# ready 1us after PD_SCK rising edge, so we sample after
# lowering PD_SCL, when we know DOUT will be stable.
self.clock.set_value(1)
self.clock.set_value(0)
value = self.data.get_value()
# Convert Boolean to int and return it.
return int(value)
def readNextByte(self):
byteValue = 0
# Read bits and build the byte from top, or bottom, depending
# on whether we are in MSB or LSB bit mode.
for x in range(32):
if self.bit_format == 'MSB':
byteValue <<= 1
byteValue |= self.readNextBit()
else:
byteValue >>= 1
byteValue |= self.readNextBit() * 0x80
# Return the packed byte.
return byteValue
def readRawBytes(self):
# Wait for and get the Read Lock, incase another thread is already
# driving the HX711 serial interface.
self.readLock.acquire()
# Wait until HX711 is ready for us to read a sample.
while not self.is_ready():
pass
# Read three bytes of data from the HX711.
firstByte = self.readNextByte()
secondByte = self.readNextByte()
thirdByte = self.readNextByte()
# print(firstByte,secondByte,thirdByte)
# HX711 Channel and gain factor are set by number of bits read
# after 24 data bits.
for i in range(self.GAIN):
# Clock a bit out of the HX711 and throw it away.
self.readNextBit()
# Release the Read Lock, now that we've finished driving the HX711
# serial interface.
self.readLock.release()
# Depending on how we're configured, return an orderd list of raw byte
# values.
if self.byte_format == 'LSB':
return [thirdByte, secondByte, firstByte]
else:
return [firstByte, secondByte, thirdByte]
def read_long(self):
# Get a sample from the HX711 in the form of raw bytes.
dataBytes = self.readRawBytes()
if self.DEBUG_PRINTING:
print(dataBytes,)
# Join the raw bytes into a single 24bit 2s complement value.
twosComplementValue = ((dataBytes[0] << 16) |
(dataBytes[1] << 8) |
dataBytes[2])
if self.DEBUG_PRINTING:
print("Twos: 0x%06x" % twosComplementValue)
# Convert from 24bit twos-complement to a signed value.
signedIntValue = self.convertFromTwosComplement24bit(
twosComplementValue)
# Record the latest sample value we've read.
self.lastVal = signedIntValue
# Return the sample value we've read from the HX711.
return int(signedIntValue)
def read_average(self, times=3):
# Make sure we've been asked to take a rational amount of samples.
if times <= 0:
raise ValueError("HX711()::read_average(): times must >= 1!!")
# If we're only average across one value, just read it and return it.
if times == 1:
return self.read_long()
# If we're averaging across a low amount of values, just take the
# median.
if times < 5:
return self.read_median(times)
# If we're taking a lot of samples, we'll collect them in a list, remove
# the outliers, then take the mean of the remaining set.
valueList = []
for x in range(times):
valueList += [self.read_long()]
valueList.sort()
# We'll be trimming 20% of outlier samples from top and bottom of collected set.
trimAmount = int(len(valueList) * 0.2)
# Trim the edge case values.
valueList = valueList[trimAmount:-trimAmount]
# Return the mean of remaining samples.
return sum(valueList) / len(valueList)
# A median-based read method, might help when getting random value spikes
# for unknown or CPU-related reasons
def read_median(self, times=3):
if times <= 0:
raise ValueError(
"HX711::read_median(): times must be greater than zero!")
# If times == 1, just return a single reading.
if times == 1:
return self.read_long()
valueList = []
for x in range(times):
valueList += [self.read_long()]
valueList.sort()
# If times is odd we can just take the centre value.
if (times & 0x1) == 0x1:
return valueList[len(valueList) // 2]
else:
# If times is even we have to take the arithmetic mean of
# the two middle values.
midpoint = len(valueList) / 2
return sum(valueList[midpoint:midpoint+2]) / 2.0
# Compatibility function, uses channel A version
def get_value(self, times=3):
return self.get_value_A(times)
def get_value_A(self, times=3):
return self.read_median(times) - self.get_offset_A()
def get_value_B(self, times=3):
# for channel B, we need to set_gain(32)
g = self.get_gain()
self.set_gain(32)
value = self.read_median(times) - self.get_offset_B()
self.set_gain(g)
return value
# Compatibility function, uses channel A version
def get_weight(self, times=3):
return self.get_weight_A(times)
def get_weight_A(self, times=3):
value = self.get_value_A(times)
value = value / self.REFERENCE_UNIT
return value
def get_weight_B(self, times=3):
value = self.get_value_B(times)
value = value / self.REFERENCE_UNIT_B
return value
# Sets tare for channel A for compatibility purposes
def tare(self, times=15):
return self.tare_A(times)
def tare_A(self, times=15):
# Backup REFERENCE_UNIT value
backupReferenceUnit = self.get_reference_unit_A()
self.set_reference_unit_A(1)
value = self.read_average(times)
if self.DEBUG_PRINTING:
print("Tare A value:", value)
self.set_offset_A(value)
# Restore the reference unit, now that we've got our offset.
self.set_reference_unit_A(backupReferenceUnit)
return value
def tare_B(self, times=15):
# Backup REFERENCE_UNIT value
backupReferenceUnit = self.get_reference_unit_B()
self.set_reference_unit_B(1)
# for channel B, we need to set_gain(32)
backupGain = self.get_gain()
self.set_gain(32)
value = self.read_average(times)
if self.DEBUG_PRINTING:
print("Tare B value:", value)
self.set_offset_B(value)
# Restore gain/channel/reference unit settings.
self.set_gain(backupGain)
self.set_reference_unit_B(backupReferenceUnit)
return value
def set_reading_format(self, byte_format="LSB", bit_format="MSB"):
if byte_format == "LSB":
self.byte_format = byte_format
elif byte_format == "MSB":
self.byte_format = byte_format
else:
raise ValueError("Unrecognised byte_format: \"%s\"" % byte_format)
if bit_format == "LSB":
self.bit_format = bit_format
elif bit_format == "MSB":
self.bit_format = bit_format
else:
raise ValueError("Unrecognised bitformat: \"%s\"" % bit_format)
# sets offset for channel A for compatibility reasons
def set_offset(self, offset):
self.set_offset_A(offset)
def set_offset_A(self, offset):
self.OFFSET = offset
def set_offset_B(self, offset):
self.OFFSET_B = offset
def get_offset(self):
return self.get_offset_A()
def get_offset_A(self):
return self.OFFSET
def get_offset_B(self):
return self.OFFSET_B
def set_reference_unit(self, reference_unit):
self.set_reference_unit_A(reference_unit)
def set_reference_unit_A(self, reference_unit):
# Make sure we aren't asked to use an invalid reference unit.
if reference_unit == 0:
raise ValueError(
"HX711::set_reference_unit_A() can't accept 0 as a reference unit!")
return
self.REFERENCE_UNIT = reference_unit
def set_reference_unit_B(self, reference_unit):
# Make sure we aren't asked to use an invalid reference unit.
if reference_unit == 0:
raise ValueError(
"HX711::set_reference_unit_A() can't accept 0 as a reference unit!")
return
self.REFERENCE_UNIT_B = reference_unit
def get_reference_unit_A(self):
return self.REFERENCE_UNIT
def get_reference_unit(self):
return self.get_reference_unit_A()
def get_reference_unit_B(self):
return self.REFERENCE_UNIT_B
def power_down(self):
# Wait for and get the Read Lock, incase another thread is already
# driving the HX711 serial interface.
self.readLock.acquire()
# Cause a rising edge on HX711 Digital Serial Clock (PD_SCK). We then
# leave it held up and wait 100 us. After 60us the HX711 should be
# powered down.
self.clock.set_value(0)
self.clock.set_value(1)
time.sleep(0.0001)
# Release the Read Lock, now that we've finished driving the HX711
# serial interface.
self.readLock.release()
def power_up(self):
# Wait for and get the Read Lock, incase another thread is already
# driving the HX711 serial interface.
self.readLock.acquire()
# Lower the HX711 Digital Serial Clock (PD_SCK) line.
self.clock.set_value(0)
# Wait 100 us for the HX711 to power back up.
time.sleep(0.0001)
# Release the Read Lock, now that we've finished driving the HX711
# serial interface.
self.readLock.release()
# HX711 will now be defaulted to Channel A with gain of 128. If this
# isn't what client software has requested from us, take a sample and
# throw it away, so that next sample from the HX711 will be from the
# correct channel/gain.
if self.get_gain() != 128:
self.readRawBytes()
def reset(self):
self.power_down()
self.power_up()
if __name__ == "__main__":
hx = HX711(6, 5, gain=128)
# hx.reset()
value = hx.read_median(times=3)
print(value)
# EOF - hx711.py
Dear @Jakob,
As you can see on the Dahlia Carrier Board Datasheet (https://docs.toradex.com/109590-dahlia_datasheet_v1.1.pdf) you can see that GPIO’s are only 1.8V. The level shifters on this board are used for specific interfaces such as the Camera. On the Development Board (https://docs.toradex.com/109463-verdin_development_board_datasheet_v1.1.pdf), however, you can use level-shifters to have a 3.3V GPIO.
@harihk11, is this a new version from the previous code, right? If the problem is that the HX711 doesn’t work with a 1.8V GPIO, you will probably need more than a code change to make it work. The carrier board has some pins that output more than 1.8V that you could use as a source for this device but not as GPIO’s. Therefore, either using the Development Board Level Shifters, Changing the load cell amplifier you use, or designing your own Carrier Board would be suggested.
Best regards,