Skip to content

Instantly share code, notes, and snippets.

@underdoeg

underdoeg/hx711.py

Last active Jun 28, 2020
Embed
What would you like to do?
Python port for RaspberryPI of the HX711 Breakout Board
import RPi.GPIO as GPIO
import time
def createBoolList(size=8):
ret = []
for i in range(8):
ret.append(False)
return ret
class HX711:
def __init__(self, dout, pd_sck, gain=128):
self.PD_SCK = pd_sck
self.DOUT = dout
GPIO.setup(self.PD_SCK, GPIO.OUT)
GPIO.setup(self.DOUT, GPIO.IN)
self.GAIN = 0
self.OFFSET = 0
self.SCALE = 1
self.lastVal = 0
#GPIO.output(self.PD_SCK, True)
#GPIO.output(self.PD_SCK, False)
self.set_gain(gain);
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 read(self):
while not self.is_ready():
#print("WAITING")
pass
dataBits = [createBoolList(), createBoolList(), createBoolList()]
for j in range(2, -1, -1):
for i in range(7, -1, -1):
GPIO.output(self.PD_SCK, True)
dataBits[j][i] = GPIO.input(self.DOUT)
GPIO.output(self.PD_SCK, False)
#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 == True for item in dataBits[0]):
return self.lastVal
bits = []
for i in range(2, -1, -1):
bits += dataBits[i]
self.lastVal = int(''.join(map(str, bits)), 2)
return self.lastVal
'''
data = [0,0,0]
for i in range(0,3):
#print(''.join(map(str, dataBits[i])))
data[i] = int(''.join(map(str, dataBits[i])), 2)
#print(data[i])
#data[2] ^= 0x80
return data[2] << 16 | data[1] << 8 | data[0]
'''
def read_average(self, times=3):
sum = 0
for i in range(times):
sum += self.read()
return sum / times
def get_value(self, times=3):
return self.read_average(times) - self.OFFSET
def get_units(self, times=3):
return self.get_value(times) / self.SCALE
def tare(self, times=15):
sum = self.read_average(times)
self.set_offset(sum)
def set_scale(self, scale):
self.SCALE = scale
def set_offset(self, offset):
self.OFFSET = offset
def power_down(self):
GPIO.output(self.PD_SCK, False)
GPIO.output(self.PD_SCK, True)
def power_up(self):
GPIO.output(self.PD_SCK, False)
############# EXAMPLE
hx = HX711(9, 11)
hx.set_scale(7050)
hx.tare()
while True:
try:
val = hx.get_units(3)
if val > 100:
print("OH NO")
#hx.power_down()
#time.sleep(.001)
#hx.power_up()
except (KeyboardInterrupt, SystemExit):
sys.exit()
@beelogger

This comment has been minimized.

Copy link

@beelogger beelogger commented Mar 8, 2016

Hi Underdoeg, can you please add some comments to your python script HX711.py, I am trying to understand the code but I am lost a bit, thanks, Markus

@jproehl

This comment has been minimized.

Copy link

@jproehl jproehl commented Mar 23, 2016

I, too, am puzzled by this code so comments would help immensely. For example, I do not see anywhere to specify which gpio pins the HX711 is connected. Does it connect to the SDA and SCL pins, so that this code is assuming a default configuration? If so, can I move the connection to any other pins? I'm trying to use the HX711 in parallel with a BMP180, which is connected to those two pins. Thanks!

@Wayne91

This comment has been minimized.

Copy link

@Wayne91 Wayne91 commented Mar 30, 2016

Thanks underdoeg. I got it to work. What I would like is a better understanding of how you are controlling the gain. This was not immediately clear. Does it vary it based upon the input range and set itself. Also, I noticed the code will "zero" itself out. How do you turn this off if necessary. First I thought it had to do with Taring, but now I'm not so sure.

I'm using this for a pressure transducer that uses a wheatstone bridge strain gage. I cannot have this zeroing out everytime I run the program.

@jproehl-- They can be any pins. In his example he uses pins 9 and 11 as shown on line #115. However I added a line to the code right before line #17

GPIO.setmode (GPIO.BCM)

That sets the Pi to recognize the BCM pins, not just the Pi board. You have to make sure you know the difference. At the terminal prompt use the command gpio readall and it will show you the layout of all the pins.

@Wayne91

This comment has been minimized.

Copy link

@Wayne91 Wayne91 commented Mar 30, 2016

So maybe I spoke too soon. I guess I should simplify this as I cannot correlate the returned values with real voltages.

My supply voltage coming off the Pi2 is 5.2V that feeds the HX711 board.
The excitation voltage going to the strain gage is 4.2 V
My gage has a built in calibration shunt which gives me exactly 80% of full value.
In this case the wheatstone bridge gage is 3mv/V at full scale.

Thus if I excite with 4.2 V, I get an actual calibration measurement across the A+ / A- terminals (with voltmeter in hand) of 3mV/V x 4.2V x 0.8 = 10.1mV as expected.

I set my SCALE to 1, and with the assumption that I properly set the gain to 128, I'm getting back a value of 16066200 on average.
I was expecting to get a decimal value val back something like 10.1mV x 128 gain / 4.2V x 1677216 = 5164187

UPDATE: 4/2/16
The behavior is totally erratic jumping all over the place. I thought it might have something to do with the collection of data in the array dataBits[] so I even tried to flatten it out to read 24 bit straight on and then add one more pulse to PD_SCK for the 128 gain. However the data I get back from DOUT is usually all 1's (24 or them). DOUT needs to be low before it will start sending data during from it's stack. But somewhere I think it's either the poor timing control of pulsing the pins, or that I'm not initializing the first pulse correctly. Maybe I need to run this in C instead of Python? I don't know. Other forums mention that the HX711 works on the Arduino... maybe because it has good timing control.

Is there a better way to control the serial timing with the Pi2? Python does have some serial API's but I don't know enough whether that would apply in this situation. Has anyone else out there has been able to make this code work on a Pi that is not a weight scale. I'm almost at the point of dumping the HX711 and getting another 24 bit ADC from TI or Analog Devices that will run SPI or I2C. Wasted way to much time trying to debug this.

Thanks for any help you can provide.

@lupitamga

This comment has been minimized.

Copy link

@lupitamga lupitamga commented Apr 17, 2016

Hi, I need some help.

I am using a cell load with the HX711, I run the program but I don`t understand well the value it returns, could you please help me or explain me the code :)

@Jameo

This comment has been minimized.

Copy link

@Jameo Jameo commented Apr 25, 2016

Hi, I am trying to work out why the scaling is in reverse i.e. the bigger the weight I put on the lower the value reading I get?

@MikeMmmm

This comment has been minimized.

Copy link

@MikeMmmm MikeMmmm commented Apr 25, 2016

Has anyone gotten this to work in Python on Raspberry Pi? My HX711 board (Sparkfun HX711 Load Cell Amp breakout) works flawlessly with Arduino, but makes a big mess on the Pi. I've tried debugging it, getting it to spit out the bit string, etc., and it doesn't make any sense... Is there a clock timing issue? Problem interpreting the 2's complement?

@tatobari

This comment has been minimized.

Copy link

@tatobari tatobari commented Jun 5, 2016

I made it work. I've made a few modifications on this code on the way the bits are processed after making a few tests using Python's Numpy library. The main problem was that the final int value was not being built with the bytes in the correct order. This caused that with very low differences (noise, etc) the values would jump from one extreme to another because the "less significant byte" was in the "most significant byte" position when building the 24 bit integer.

One note about the HX711 is that just after it is plugged to power it kind of needs a few minutes to "warm up". Before digging in this code por Raspberry Pi, I used an Arduino to test the HX711 using the Serial Plotter because I thought my mine might have been broken (I have 20 of these and tested 2 of them). After I plugged the Vcc, the signal just keeps growing with very little increments for about 2 or 3 minutes until it reaches a stable value. This could lead to bad "taring" or, if you're aiming for big precision, well, you know...

I've never contributed to any repository before so I'm not sure what are the steps here. Should I fork it? Should I clone? I've only worked on my own repositories before.

@eddie4

This comment has been minimized.

Copy link

@eddie4 eddie4 commented Jun 6, 2016

I would be very interested in the modifications you made. Am not completely sure about git educate on how to do this best. But if you could send it to me that would be great.
eddiebijnen@hotmail.com

@tatobari

This comment has been minimized.

Copy link

@tatobari tatobari commented Jun 6, 2016

I've uploaded the code to the repo on the URL below but I will give it to "underdoeg" as soon as he contacts me. It's his code after all.

https://github.com/tatobari/hx711py/

@arthurmoises

This comment has been minimized.

Copy link

@arthurmoises arthurmoises commented Jun 18, 2016

Basically, you clone it. After that, branch it and make a pull request to the owner. If he accepts it, he can merge it with the original code.

Thanks for the debbuging! I will be looking forward to test it on Monday, so after that I can post the results and contribute to it too.

@tibloop

This comment has been minimized.

Copy link

@tibloop tibloop commented Jun 23, 2016

hello, I am trying to run this code in Beaglebone Black, however I only get 0.0 all the time . I only modified the GPIO reading and writing to fit beaglebone. Is there any timing difference between RaspberryPI and beaglebone that I should consider? Thank you very much

(self.read_average(times) always gives me 8388607 )

@underdoeg

This comment has been minimized.

Copy link
Owner Author

@underdoeg underdoeg commented Sep 11, 2016

I am sorry for not reacting to this thread sooner. I have somehow not seen the notifications. Please issue any PRs if you changed something. thanks

@ahmetuludag

This comment has been minimized.

Copy link

@ahmetuludag ahmetuludag commented Sep 23, 2016

@tibloop may I ask if you find any solution ? I'm also experiencing the same issue.

@dcrystalj

This comment has been minimized.

Copy link

@dcrystalj dcrystalj commented Nov 15, 2016

@ahmetuludag I ported project to py3 and did some improvements. https://github.com/dcrystalj/hx711py3

@Richard-Major

This comment has been minimized.

Copy link

@Richard-Major Richard-Major commented Dec 23, 2016

@underdoeg - there seems to be some problems with the bit order in this code - I've updated a version at https://gist.github.com/Richard-Major/64e94338c2d08eb1221c2eca9e014362

@lucian0112

This comment has been minimized.

Copy link

@lucian0112 lucian0112 commented Mar 3, 2017

Hi all,

I tried use all codes from this page but seems my main issue is i get totally random values from hx711... where i can be wrong?

Thanks

@panditjiNE

This comment has been minimized.

Copy link

@panditjiNE panditjiNE commented Apr 4, 2017

@tibloop @tatobari - I am having same issue, I have beaglebone black rev c board and getting zeros, is that any solution for that

@mertmzk

This comment has been minimized.

Copy link

@mertmzk mertmzk commented Jun 15, 2017

Hi all,
I was also having same problem (getting all 0 value) with a different SoM (not raspi) and finally got the solution. As I was writing / reading GPIOs from linux sys interface with my own function which has some problems and not reading properly. I changed it with python gpio library which is here it starts working like a charm! Please check that your gpio fucntion reading/writing properly.

Thanks.

@getunified

This comment has been minimized.

Copy link

@getunified getunified commented Jul 8, 2017

Hi Mertmzk, do you mean include your code in the example.py or run separately?

Thanks
Paul

@duc99hust99

This comment has been minimized.

Copy link

@duc99hust99 duc99hust99 commented Sep 21, 2018

Hi tatobari i have a problem with time to read hx711 . it's too slow . How can i control time to read hx711

@Abinaya2597

This comment has been minimized.

Copy link

@Abinaya2597 Abinaya2597 commented Dec 11, 2018

can anyone tell me how to calibrate???

@LBG1

This comment has been minimized.

Copy link

@LBG1 LBG1 commented Mar 12, 2019

Hi tatobari I am using your example.py and getting -8388607 repeatedly as output, always the same whether or not I put any weight on the load gauge. I notice that tibloop also got this same number (23 Jun 2016). Can anyone help me? I'm a complete novice, this is my first project with a Raspberry Pi and load gauge, learning as I go along...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.