News:

Let's find out together what makes a PIC Tick!

Main Menu

Problem with the HX711 module code

Started by Yves, May 01, 2025, 11:58 AM

Previous topic - Next topic

Yves

Hello,
I having some issue with my code with the HX711 module. The raw data from the output is giving me only 0 without load and 1 with load.I tried a code that use sequential clocking on and off from PicBasic as below shown that the unit is working.(see below)and the module is responding as it should, but I don't think it is as elegant code using the Shin function.Unfortunatly the Positron code (attached) is not working, Where are I'm going wrong?


Picbasic (modified):
Dim Hx711Cnt As Word
 I var Byte
 GraM var Word
 GraMM var Word
 SigN var Byte
 
Symbol dataa = PORTA.5    'Symbol Dat          = PORTA.5 'pin 2
Symbol clock = PORTA.4    'Symbol Slk          = PORTA.4 'pin 3
Input dataa
Output clock
I=0
LoadCellA:
 dataa = 1:clock = 0: Hx711Cnt = 0
 While dataa = 1 : Wend
 For I = 0 To 15
   High clock
   Hx711Cnt = Hx711Cnt <<1
   Low clock
   If dataa=1 Then Hx711Cnt=Hx711Cnt+1
 Next
 High clock
 Hx711Cnt = Hx711Cnt^32768
 gram=32768-Hx711Cnt
 gram=gram-65477' zero
 If gram.15 Then  'negative values
     gram = ~gram + 1
     Sign = "-"
  Else
     Sign=" "   
  EndIf

Disable 
gram=gram*11627   'scale values
gramm=Div32 10000
  Enable
HRSOut  0xFE,0x45,0x00,"Hx711Cnt  ",Dec Hx711Cnt


DelayMS 100
GoTo LoadCellA
HX711 driver code.zip 
Yves

basparky

Hi Yves,

I have been uding the following code to get the data from my HX711 modules:
Proc ReadLoadCellLeft()
;LoadCellLeft:
    HXLDO = 1
    HXLCK = 0
    Hx711CntL = 0
    While HXLDO = 1 : Wend
    For i = 0 To 23
        High HXLCK
        Hx711CntL = Hx711CntL <<1
        Low HXLCK
        If HXLDO = 1 Then Inc Hx711CntL
    Next
    High HXLCK
    Hx711CntL = Hx711CntL^0x800000
    Low HXLCK
EndProc

    LaReading = HXLOrigin ;8327437
    LaLoad = 0 
    LbReading = 8946794 ;                                      Highest measured value
    LbLoad = 4813   'Known wheight at max load.
    LoadLeft = (((LbLoad - LaLoad) /(LbReading - LaReading)) * (Hx711CntL - LaReading) + LaLoad)

Perhaps this helps you one step further..

Pepe

demo Proteus


;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 12F1572

Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, BOREN_OFF, CLKOUTEN_OFF
Config2 WRT_OFF, PLLEN_ON, STVREN_OFF, BORV_LO, LPBOREN_OFF, LVP_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Declare Xtal = 32
Declare Optimiser_Level = 3
Declare Create_Coff On
Declare Watchdog off
Declare Float_Display_Type = Fast
Declare Dead_Code_Remove = 1        ' Remove dead code
Declare Reminders Off
Declare Hints Off
Declare Serial_Baud =9600
Declare RSOut_Pin =PORTA.5
Declare RSOut_Mode = 0
Symbol   LCD_CLR        0x01               
Symbol   LINE1          0x80               
Symbol   LINE2          0xC0               
Symbol   COMMAND        0xFE               

Symbol tarar  = PORTA.3
Symbol LED  = PORTA.2  'pin 11
Symbol gan  767.5
Symbol offset  839615

OSCCON = 0b01110010

While OSCSTAT.6 <> 0
Wend

Output LED 
Symbol ADDO  = PORTA.0
Symbol ADSK  = PORTA.1

Dim peso As SWord
Dim pesoa As SWord = -10000
Dim tara As SWord
Dim Hx711 As Float

Input tarar
Input ADDO
Output ADSK

ADDO = 1
ADSK = 0

LCD_Init()   '2X20 serial display
   
RsOut "Peso: "           

Do
 peso = pesar()
 If tarar = 0 Then
                   tara = peso
                   pesoa = -10000
 EndIf

 If pesoa <> peso Then
                       pesoa = peso
                       LCD_COMMAND(Line1+6)
                       Hx711 = (peso - tara)/10
                       RsOut SDec1 Hx711,"   "

 EndIf
 Toggle LED
 DelayMS 300
Loop

Proc LCD_Init()
    LCD_COMMAND(0xC0)   
    LCD_COMMAND(0x01)   
    DelayMS 2                                 
    LCD_COMMAND(0x06)   
    LCD_COMMAND(LCD_CLR)
    DelayMS 200       
EndProc

Proc LCD_COMMAND(cmd As Byte)
RsOut COMMAND,cmd
EndProc


Proc pesar(),Float
Dim alta As Word
Dim baja As Word
Dim datos As Dword
 
 If ADDO = 0 Then        ' if data line low it's ready, if not bypass until next try.
                 datos = Read_hx711()
                 datos = datos & $1FFFFFF
                 Result = (datos - offset)/gan
             Else   
                 DelayMS 100
                 ADSK = 1
                 DelayUS 100
                 ADSK = 0
 EndIf
EndProc

Proc Read_hx711(), Dword
    Dim ReadValue As Dword = 0
    Dim index As Byte
    DelayUS 3
    For index = 24 To 0 Step -1
     DelayUS 2
     High ADSK
     ReadValue = ReadValue << 1
      DelayUS 3
     ReadValue.0= ADDO  ' Leer bit del dato
     Low ADSK
    Next
    Result = ReadValue
EndProc

Yves

Many thanks to all. I wonder why Shin function didn't work for that unit?
Yves

Pepe

#4
I don't know if Shin doesn't work with Proteus or if there's some error in Positron that prevents it from reading the received data.Only Les can clear up our doubts.
Attached demo on Proteus using software communication that works and at the same time with shin that doesn't work

Yves

Hello
I'm pulling my hairs off when I use Basparky code a high weight gives me a lower count than a lower weight from the base line (at least the module is responding).I checked the load cell and the connections wire code all is OK.  In the past I used to you Shin function but for some reasons it is not working anymore. The code below gives me a constant count of 1. Les can you please help.

While
        SHIn Dat,Slk,MsbPost_L,[Hx711Cnt\16,rubbish\9]  'Pass data in Hx711Cnt
        Dat = 0     
        Toggle LED
        DelayMS 300
Wend
Yves

trastikata

#6
Quote from: Yves on May 03, 2025, 09:02 AMwhen I use Basparky code a high weight gives me a lower count than a lower weight from the base line (at least the module is responding).I checked the load cell and the connections wire code all is OK. 

Either row the load cell 180 degrees (if beam type) or flip the bridge output wires as general.

Pepe

Shin on pic 16f684 works but not on pics 12f

Yves

#8
I have swap the A+ and A- wires, turned the the load cell around but no avail. I'm using the code below:
While
' HX711 Basic Code Example
Proc ReadHX711()
    Hx711Cnt = 0
'For I = 0 To 23
I=0
Do
        PulseOut Slk, 1 ' Generate clock pulse
        Hx711Cnt = Hx711Cnt * 2
    If Dat = 1 Then
       Hx711Cnt = Hx711Cnt + 1
    EndIf
    Inc I
Loop Until I = 24
    PulseOut Slk, 1 ' Generate additional pulse to set gain
    Hx711Cnt = Hx711Cnt ^ 0x800000 ' Convert to signed Hx711Cnt
EndProc

While
    ReadHX711()
    HRSOutLn "DATA, TIME, TIMER,", Dec Hx711Cnt
   
    DelayMS 500

Wend
Wend

Attached the capture of raw compts
Yves

Yves

trastikata

Quote from: Yves on May 03, 2025, 05:26 PMI have swap the A+ and A- wires, turned the the load cell around but no avail. I'm using the code below:

Yves,

it is either swap the bridge output wires or roll the sensor, if you do the both, then you come back to the initial situation.

Simply swap the A+ and A- wires.


John Drew

#10
Yves,
Why not swap the values in software.
I use something like the following to change the direction of a rotary encoder.

Dim Temp as sword
Dim Newvalue as word
Dim Encoder as word

Temp = encoder - 4096 'assumes 12 bit encoder
Newvalue = abs Temp

John

PS if it's 10 bit use 1024

Pepe

In the simulator Proteus your procedure works

Frizie

Ohm sweet Ohm | www.picbasic.nl