HX711 load cell connecting to Picaxe

hax

New Member
Hi all,

I've ordered a HX711 load cell ADC for a strain gauge I wish to measure. I believe I have to use the hspiin and hspiout commands or maybe the shiftin shiftout commands, but am not quite sure where to start. Has anyone used this chip before and could you share your code?

From the manual here https://cdn.sparkfun.com/datasheets/Sensors/ForceFlex/hx711_english.pdf

it seems I can set it to 10Hz operation where it continuously sends the data without waiting for an input command. Is that the sort of thing the shiftin command is designed for or am I completely going in the wrong direction?
 

Jeremy Harris

Senior Member
I've used the HX711 boards from China with a Picaxe OK. I used them to interface a bridge-type pressure sensor, but they are easy to bit-bang from a 14M2 I found. My experience with them has been very positive, they are remarkably good for the price. Here's a sample bit of code from when I was testing one of these with an 08M2 (pretty sure I got the core code from someone here - credit goes to them for it!):

Code:
;Picaxe code to read HX711 bridge amplifier using an 08M2

;Reads 24 bits into three bytes, low, mid and high

;Best to select which pair of bytes to use dependent on sensitivity and required range

;Read this in conjunction with the HX711 data sheet

;Code here has been modified to only store the 8 data bytes, display code has been stripped out

;Uses 08M2 pin c.3 as the data input from the HX711 and pin C.2 as the clock to drive the HX711


#picaxe 08m2

setfreq m8

symbol low_byte =		b3
symbol mid_byte =		b2
symbol high_byte =		b1


symbol bitCount=		b4
symbol loop_count = 		b5

Symbol Data_pin = 		pinC.3
Symbol Clock_pin = 		C.2



main:
	
Read_sensor:
	
	for loopcount = 0 to 3
	
start:
	
		if Data_pin = 1 then start				;data pin is low when ready to send data, high when not ready, so loop back if not ready
		
		low_byte=0						;ensure data bytes are all zero
		mid_byte=0
		high_byte=0


		For bitCount = 0 To 7					;clock in the 8 bits of the high byte
  			PulsOut Clock_pin, 1				;send a 1ms clock pulse to the HX711
  			high_byte = high_byte * 2 | Data_pin		;add the data from the HX711 to the byte, shifting bits by multipying by 2 and OR'ing
		Next							;note that the "|" instruction is a bit-wise OR


		For bitCount = 0 To 7					;clock in the 8 bits of the mid byte
  			PulsOut Clock_pin, 1
  			mid_byte= mid_byte * 2 | Data_pin
		Next


		For bitCount = 0 To 7					;clock in the 8 bits of the low byte
  			PulsOut Clock_pin, 1
  			low_byte = low_byte * 2 | Data_pin
		Next

		PulsOut Clock_pin, 1					;clear the last bit from the HX711
 	
	next loop_count

	
;at this point the 24 bits are stored as three bytes ready for use


	goto main
 

hax

New Member
I've been using the above code that Jeremy found (above) in turn from another post on this forum. It's been working great for me for a while now with no issues.

I have recently changed the loadcell to a different version, and now the "mid-range" of the loadcell is way outside the range of the previous loadcell. I was just reading two of the 3 available bytes.

So now I am trying to work out a neat way to manipulate 24 bit numbers, that comprise of 3 separate bytes, so that I can "tare" the loadcell at any point and maximise the full range of measurement.

So let's say we have low_byte mid_byte and high_byte as a value in each of say b0, b1, b2. What is a neat way of adding/subtracting some fixed 24 bit value from these variables?
 

Buzby

Senior Member
In this forum there are lots of examples of how to do 32 bit maths.

Use 32 bit, and just ignore the MSB.
 

AllyCat

Senior Member
Hi,
What is a neat way of adding/subtracting some fixed 24 bit value from these variables?
Assuming you really do have data with more than 16 significant bits of data (which is roughly equivalent to weighing a person to within one gram resolution) then indeed a simple 32-bit routine is probably the best way to go. Otherwise, just shift 16 significant bits into a two-byte word and work with that.

Personally, I've mainly considered 32-bit division routines, but did find that addition and subtraction are not completely trivial, so see post #2 of this Code Snippet thread.

Cheers, Alan.
 
Top