Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

HX711
Goto page 1, 2  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
odemmo

Bascom Member



Joined: 26 Jun 2007
Posts: 19

norway.gif
PostPosted: Tue Jan 28, 2020 1:40 pm    Post subject: HX711 Reply with quote

Does anyone have a code to read data from HX711, I just need to read the weight from the load cell and write them to serial, I have searched a lot on the internet without finding anything, please.

(BASCOM-AVR version : 2.0.8.2 )
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5915
Location: Holland

blank.gif
PostPosted: Tue Jan 28, 2020 2:36 pm    Post subject: Reply with quote

the data sheet is reasonable clear.
you can use SHIFTIN or a simple BASIC for next.
The number of pulses selects which channel, and the gain.
I have no code but it looks simple to me from a quick peek in the datasheet.

_________________
Mark
Back to top
View user's profile Visit poster's website
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Tue Jan 28, 2020 3:10 pm    Post subject: Reply with quote

Hi,
I tested it and wrote an article.
It should be helpful.

Japanese site.
http://www.ne.jp/asahi/shared/o-family/ElecRoom/AVRMCOM/HX711Test/HX711Test.html

English translation site.
http://translate.google.com/translate?hl=ja&sl=ja&tl=en&u=http%3A%2F%2Fwww.ne.jp%2Fasahi%2Fshared%2Fo-family%2FElecRoom%2FAVRMCOM%2FHX711Test%2FHX711Test.html
http://www.translatetheweb.com/?from=&to=en&ref=TVert&refd=www.bing.com&dl=ja&rr=DC&a=http%3a%2f%2fwww.ne.jp%2fasahi%2fshared%2fo-family%2fElecRoom%2fAVRMCOM%2fHX711Test%2fHX711Test.html


Last edited by O-Family on Fri Jan 15, 2021 3:37 pm; edited 1 time in total
Back to top
View user's profile Visit poster's website
odemmo

Bascom Member



Joined: 26 Jun 2007
Posts: 19

norway.gif
PostPosted: Thu Jan 30, 2020 9:52 am    Post subject: Reply with quote

O-Family wrote:
Hi,
I tested it and wrote an article.
It should be helpful.

Japanese site.
http://www.ne.jp/asahi/shared/o-family/ElecRoom/AVRMCOM/HX711Test/HX711Test.html

English translation site.
http://translate.google.com/translate?hl=ja&sl=ja&tl=en&u=http%3A%2F%2Fwww.ne.jp%2Fasahi%2Fshared%2Fo-family%2FElecRoom%2FAVRMCOM%2FHX711Test%2FHX711Test.html



thanks, this solves my "problem", just lots of stripping before i'm done here.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5915
Location: Holland

blank.gif
PostPosted: Thu Jan 30, 2020 2:26 pm    Post subject: Reply with quote

well done O-Family !
_________________
Mark
Back to top
View user's profile Visit poster's website
odemmo

Bascom Member



Joined: 26 Jun 2007
Posts: 19

norway.gif
PostPosted: Thu Jan 30, 2020 8:11 pm    Post subject: Reply with quote

so far this work for me


$regfile = "m16def.dat"

$crystal = 3686400

$baud = 9600

$hwstack = 64
$swstack = 10
$framesize = 24

Hx711_clk Alias Portd.2
Hx711_dat Alias Pind.3

Dim Hx711ad As Long
Dim Hx711avg As Long
Dim Hx711count As Byte
Dim Average(16) As Long
Dim Avgpoi As Byte
Dim Avgsum As Long
Dim Zeroadj As Long
Dim Stablecount As Byte
Dim Lastvalue As Long
Dim Sensorcal As Long
'
Dim Temp1 As Byte
Dim Temp2 As Byte
Dim Templ1 As Long
Dim Tempstr As String * 40
'
Dim Dummy As Eram Long
Dim Eepprgid As Eram Long
Dim Eepsensorcal As Eram Long


Config Hx711_clk = Output
Reset Hx711_clk
Config Hx711_dat = Input

If Eepprgid <> &H31375848 Then
Eepprgid = &H31375848
Eepsensorcal = 30065
End If
Sensorcal = Eepsensorcal

For Temp1 = 1 To 20
Do

Loop Until Hx711_dat = 0
'
Gosub Hx711get
Gosub Hx711movavg
Locate 2 , 1
Print Hex(hx711avg)
Next Temp1

Zeroadj = 0 - Hx711avg
Stablecount = 0
Lastvalue = 0

Temp2 = 0

Main:
If Hx711_dat = 0 Then
Gosub Hx711get
Gosub Hx711movavg
Templ1 = Hx711avg + Zeroadj

Templ1 = Templ1 * 100
Templ1 = Templ1 / Sensorcal
'
Temp2 = Temp2 + 1
If Temp2 > 10 Then
Temp2 = 0
End If


waitms 100
Tempstr = Str(templ1)
If Templ1 < 0 Then
print Format(tempstr , " 0")
If Temp2 = 0 Then
Print Format(tempstr , " 0")
End If
Else
print Format(tempstr , " 0")
If Temp2 = 0 Then
Print Format(tempstr , " 0")
End If
End If

If Templ1 = Lastvalue Then
Stablecount = Stablecount + 1
If Stablecount > 5 Then
Stablecount = 5

End If
Else
Lastvalue = Templ1
Stablecount = 0

End If
End If
Goto Main



Hx711get:
Hx711ad = 0
For Hx711count = 23 To 0 Step -1
Set Hx711_clk
Waitus 1
Hx711ad.hx711count = Hx711_dat
Reset Hx711_clk
Waitus 1
Next Hx711count

Set Hx711_clk
Waitus 1
Reset Hx711_clk
Waitus 1
'
If Hx711ad.23 = 1 Then
Hx711ad = Hx711ad Or &HFF000000
End If
Return


Hx711movavg:
Avgsum = Avgsum - Average(avgpoi)
Average(avgpoi) = Hx711ad
Avgsum = Avgsum + Hx711ad
'
Avgpoi = Avgpoi + 1
If Avgpoi > 16 Then
Avgpoi = 1
End If
'
Hx711avg = Avgsum
Shift Hx711avg , Right , 4 , Signed
Return

eid
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Fri Jan 31, 2020 2:31 am    Post subject: Reply with quote

I'm glad I could help you!

Calibration and zero adjustment are required depending on the type of load cell, so please devise it.
Back to top
View user's profile Visit poster's website
odemmo

Bascom Member



Joined: 26 Jun 2007
Posts: 19

norway.gif
PostPosted: Sat Feb 01, 2020 10:51 am    Post subject: Reply with quote

O-Family wrote:
I'm glad I could help you!

Calibration and zero adjustment are required depending on the type of load cell, so please devise it.


Thanks, the app in my Mac take care of calibration and zero adjustment but I will look at it later.
Back to top
View user's profile
odemmo

Bascom Member



Joined: 26 Jun 2007
Posts: 19

norway.gif
PostPosted: Thu Nov 26, 2020 10:31 am    Post subject: Reply with quote

Next, how can I read data from 4 pcs. hx711 with 1 Atmega 8?
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Fri Nov 27, 2020 1:29 pm    Post subject: Reply with quote

All you have to do is assign [PD_SCK] and [DOUT] for 4 circuits to different ports and execute a program to extract A/D data for each port.
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Sat Nov 28, 2020 7:34 am    Post subject: Reply with quote

Hello. Im also using HX711 in some of my projects but I cant wait for the measure (SERIALS/DOS,ETH/temperatures etc.). So I handle it similarly like DS18B20.

I cut this code from one of bigger projects.
Because HX711 is temperature sensitive then one TARE is not enough. I add simple Autotare that walk thru chip is gettin warm from rest of electronic.
Maybe this can be handy for someone.

Code:
$regfile = "m2560def.dat"                                   '8K SRAM | 4K EEPROM
$crystal = 16000000
$hwstack = 256
$swstack = 128
$framesize = 256
$baud = 115200

Config Submode = New

'*****************************************************************
'*                  PROGRAM CONSTANTS/SETTINGS                   *
'*****************************************************************
' HX711
   Const Number_of_calibrations = 5
   Const Number_of_readings = 5

'***********************************************
'*           ALPHANUMERIC LCD                  *
'***********************************************

Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.5 , Db6 = Portc.7 , Db7 = Porta.6 , E = Portl.5 , Rs = Portl.1       ' , Wr = Porta.1
Config Lcd = 20x4
 Initlcd : Cursor Off , Noblink : Cls

'*****************************************************************
'*                  TIMER2 FOR TIME BASE                         *
'*****************************************************************
Config Timer2 = Timer , Prescale = 1024 , Compare_a = Disconnect , Compare_b = Disconnect , Clear_timer = 1
 Compare2a = 155                                            '10ms @16MHz/1024

  Dim 100ms As Byte

'*****************************************************************
'*                          VARIABLES                            *
'*****************************************************************
 'multipurpose
   Dim N As Byte , Helpstr As String * 20 , Helpb As Byte , Helpl As Long , Tempb As Byte , Helps As Single
   Dim Helpw As Word
 'HX711
   Dim Hx711 As Long , Gramm As Single , Gramy As Long , Hx711_state As Byte , Prev_cnt As Byte
   Dim Finalstr As String * 20 , C_niuton_str As String * 20 , Niutony As Single , Prev_hx As Long
   Dim Niuton_str As String * 20 , Number_of_reads As Byte , Offset As Long , Temp_sum As Long
'*****************************************************************
'*                            HX711                              *
'*****************************************************************

 'When on CLK pin state is HI then after 600us chip goes to sleep mode
 'It wake up after first LO state on the CLK
 'Then HX711 starts to measure and set its DATA pin HI
 'When measure is ready then chip reset DATA pin into LO and we can shiftin data
 'Data is 24 bits long, but additional pulse on the CLK we set config for new measure
 'Thats we shiftin 25 bits and then cutof that unwanted bit in the data.

   Config Portf.6 = Input : Datapin Alias Pinf.6 : Set Datapin
   Config Portf.7 = Output : Clockpin Alias Portf.7         ': Set Clockpin


 '-Function reads 25 bits, then shift last one and return 24 bits-
 Function Read_raw()as Long

   Local Watch As Word : Watch = 62767                      'if chip is not present
   Local Scale As Long

    Do
     Decr Watch
      If Datapin = 0 Then Watch = 0
    Loop Until Watch = 0
     Shiftin Datapin , Clockpin , Scale , 1 , 25            '24for data + 1clk for 128gain canal A
    Shift Scale , Right , 1 , Signed

     'Debug Scale

   Read_raw = Scale

 End Function


 Do

 '--[ TIMER2 10ms ]----------------------------
 If Tifr2.ocf2a = 1 Then
  Tifr2.ocf2a = 1
  'every10ms



         '##################### HX711 ###############
         '--[HX711 data rate is set into 10Hz (100ms)]--

          'Reset Arduino_led                                'reset 10ms blink about new measure

         Select Case Hx711_state
          Case 0                                            'only after boot
            Locate 4 , 1 : Lcd "Calib "
            Number_of_reads = Number_of_calibrations
            Incr Hx711_state

          Case 1                                            '--[calibrating 5x100ms]--
            'Debug "Cal " ;
            Temp_sum = Temp_sum + Read_raw()
            Decr Number_of_reads

            If Number_of_reads = 0 Then
             Temp_sum = Temp_sum / Number_of_calibrations
              Offset = Temp_sum
              Temp_sum = 0
             Incr Hx711_state
             Locate 4 , 1 : Lcd Spc(9)
              'Set Arduino_led
            Else
             Locate 4 , 7 : Lcd Number_of_reads ; " "
            End If

          Case 2                                            '--[normal work after calibrating 5x100ms]--
            'Debug "Raw " ;
            Temp_sum = Temp_sum + Read_raw()
            Incr Number_of_reads


            If Number_of_reads = Number_of_readings Then
             Number_of_reads = 0
             Temp_sum = Temp_sum / Number_of_readings
             Hx711 = Temp_sum - Offset
             Temp_sum = 0

             Shift Hx711 , Right , 12 , Signed              'scale down 24->12 bits  (for MCP7426)


              'Gramm = Hx711 * 0.002326                           'tensometr 5kg BARTek
               Gramm = Hx711 * 0.09045                      'bigger tensometr

               Gramy = Gramm                                'Single to Long casting, in dkg
               If Gramy < 0 Then Gramy = 0                  'never below zero

               Helpl = Gramy                                'copy
               Helpstr = Str(helpl)
               Helpstr = Format(helpstr , "0.0") + "kg"
               Helpb = Len(helpstr)
               If Helpb < 9 Then
                Helpb = 9 - Helpb
                 Finalstr = Space(helpb)
               Else
                Finalstr = ""
               End If
               Finalstr = Finalstr + Helpstr
               Finalstr = Right(finalstr , 9)
               Locate 3 , 1 : Lcd Finalstr



               '---[CALCULATE FORCE]----
               '1kg=9,80665 N   100g = 0,980665
               Helps = Gramy * 9.80665
               Niutony = Helps / 10                         'dKg->Kg
               C_niuton_str = Fusing(niutony , "#.#")
               Niuton_str = C_niuton_str + "Nm"
               Helpb = Len(niuton_str)
               If Helpb < 9 Then
                Helpb = 9 - Helpb
                 Finalstr = Space(helpb)
               Else
                Finalstr = ""
               End If
               Finalstr = Finalstr + Niuton_str
               Finalstr = Right(finalstr , 9)
               Locate 4 , 1 : Lcd Finalstr


             'Set Arduino_led                                      'blink about new measure


             '-----[ AUTOTARE ]-----
               Select Case Gramy
                Case -5 To 5

                 If Prev_hx = Gramy Then
                  If Prev_cnt < 5 Then
                   Incr Prev_cnt
                  Else
                   Prev_cnt = 0
                    Shift Prev_hx , Left , 12 , Signed
                    Offset = Offset + Prev_hx
                    'Debug "Cor " ; Prev_hx     'correction= (how many offset is changed)
                  End If
                 Else
                  Prev_cnt = 0
                 End If
                 Prev_hx = Gramy                            'always remember prev

                Case Else
                 Prev_cnt = 0

               End Select
             '-------------[AUTOTARE END]

            End If



         End Select
         '--[ HX711 End ]--


 End If


Loop
 
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5915
Location: Holland

blank.gif
PostPosted: Mon Nov 30, 2020 3:41 pm    Post subject: Reply with quote

thanks for sharing. indeed good practice to calibrate.
_________________
Mark
Back to top
View user's profile Visit poster's website
odemmo

Bascom Member



Joined: 26 Jun 2007
Posts: 19

norway.gif
PostPosted: Thu Jan 14, 2021 1:36 pm    Post subject: Reply with quote

O-Family wrote:
All you have to do is assign [PD_SCK] and [DOUT] for 4 circuits to different ports and execute a program to extract A/D data for each port.


This became a little too difficult for an amateur like me, how can I get someone to make the code for me, it is important to me since there is no finished equipment for this. What I need is to connect 4 pcs. HX711 to a microcontroller which then reads in the data in weight1, weight2, weight3, weight4 and then prints as,
Print weight1; weight2; weight3; weight4
Can I pay anything for it, Paypal?
Oddvar
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Thu Jan 14, 2021 2:15 pm    Post subject: Reply with quote

You can create four types of ports for the program you copied and worked on, and call each of them.
Code:
   Hx711_clk Alias Portd.2
   Hx711_dat Alias Pind.3

   '
   '  ******************************************************************************
   '  * Subroutine to retrieve A/D data from channel A of HX711 with a gain of 128 *  (Hx711ad = 2's complement format 24bit)
   '  ******************************************************************************
   '
Hx711get:
   Hx711ad = 0
   For Hx711count = 23 To 0 Step -1                         'Extract 24-bit data.
      Set Hx711_clk                                         'Set the [PD_SCK] pin of the HX711 to [H].
      Waitus 1                                              'Period of [H]. (T3 = 1uS)
      Hx711ad.hx711count = Hx711_dat                        'Place the data of the [DOUT] pin of HX711 in each bit.
      Reset Hx711_clk                                       'Set the [PD_SCK] pin of the HX711 to [L].
      Waitus 1                                              'Period of [L]. (T4 = 1uS)
   Next Hx711count
   '
   '  * Send the 25th clock pulse (set channel A and gain to 128 for the next conversion) *
   '
   Set Hx711_clk                                            'Set the [PD_SCK] pin of the HX711 to [H].
   Waitus 1                                                 'Period of [H]. (T3 = 1uS)
   Reset Hx711_clk                                          'Set the [PD_SCK] pin of the HX711 to [L].
   Waitus 1                                                 'Period of [L]. (T4 = 1uS)
   '
   If Hx711ad.23 = 1 Then                                   'Is the 24-bit value of A/D a negative value?
      Hx711ad = Hx711ad Or &HFF000000                       'Set to a negative value of 32 bits.
   End If
   Return

Assign 4 types of ports directly to 4 types of subroutines as shown below.
Code:
Hx711get1:
   Hx711ad = 0
   For Hx711count = 23 To 0 Step -1                         'Extract 24-bit data.
      Set Portd.2                                           'Set the [PD_SCK] pin of the HX711 to [H].
      Waitus 1                                              'Period of [H]. (T3 = 1uS)
      Hx711ad.hx711count = Pind.3                           'Place the data of the [DOUT] pin of HX711 in each bit.
      Reset Portd.2                                         'Set the [PD_SCK] pin of the HX711 to [L].
      Waitus 1                                              'Period of [L]. (T4 = 1uS)
   Next Hx711count
   '
   '  * Send the 25th clock pulse (set channel A and gain to 128 for the next conversion) *
   '
   Set Portd.2                                              'Set the [PD_SCK] pin of the HX711 to [H].
   Waitus 1                                                 'Period of [H]. (T3 = 1uS)
   Reset Portd.2                                            'Set the [PD_SCK] pin of the HX711 to [L].
   Waitus 1                                                 'Period of [L]. (T4 = 1uS)
   '
   If Hx711ad.23 = 1 Then                                   'Is the 24-bit value of A/D a negative value?
      Hx711ad = Hx711ad Or &HFF000000                       'Set to a negative value of 32 bits.
   End If
   Return

'---------------------------------------------------------------

Hx711get2:
   Hx711ad = 0
   For Hx711count = 23 To 0 Step -1                         'Extract 24-bit data.
      Set Portd.4                                           'Set the [PD_SCK] pin of the HX711 to [H].
      Waitus 1                                              'Period of [H]. (T3 = 1uS)
      Hx711ad.hx711count = Pind.5                           'Place the data of the [DOUT] pin of HX711 in each bit.
      Reset Portd.4                                         'Set the [PD_SCK] pin of the HX711 to [L].
      Waitus 1                                              'Period of [L]. (T4 = 1uS)
   Next Hx711count
   '
   '  * Send the 25th clock pulse (set channel A and gain to 128 for the next conversion) *
   '
   Set Portd.4                                              'Set the [PD_SCK] pin of the HX711 to [H].
   Waitus 1                                                 'Period of [H]. (T3 = 1uS)
   Reset Portd.4                                            'Set the [PD_SCK] pin of the HX711 to [L].
   Waitus 1                                                 'Period of [L]. (T4 = 1uS)
   '
   If Hx711ad.23 = 1 Then                                   'Is the 24-bit value of A/D a negative value?
      Hx711ad = Hx711ad Or &HFF000000                       'Set to a negative value of 32 bits.
   End If
   Return

'---------------------------------------------------------------

Hx711get3:

'---------------------------------------------------------------

Hx711get4:

If your technology makes it difficult to create a complete program, please contact me directly on the bulletin board on my site. (English is also acceptable)
Back to top
View user's profile Visit poster's website
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1162
Location: France

france.gif
PostPosted: Thu Jan 14, 2021 2:34 pm    Post subject: Reply with quote

hi, I have added a HX711 on my clock see https://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=14726
JP Wink

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum