Menu

HX711 Strain Gauge Amplifier

2020-04-20
2020-04-20
  • James Whyte

    James Whyte - 2020-04-20

    I have been working on using some strain Gauges, and the HX711 amplifier in the cheaply available board as pictured.

    The code could be improved, such as easier to change the HX711 channel and gain, but this does work as is

    ; ----- Configuration
      #chip 16F877A, 4.00
      #config OSC = XT, ;MCLRE_on, PWRT_ON
    
      #define HX711_DOUT PORTA.2  ;HX711 Serial Data Out (From HX711)
      #define HX711_SCK PORTA.3   ;HX711 Serial Clock
      #define HX711_BITS = 24     ;Number of bits to clock in
    
        ; ----- LCD connection settings
      #define LCD_IO 8
      #define LCD_RS PORTC.3
      #define LCD_RW PORTC.4
      #define LCD_Enable PORTC.5
      #define LCD_DATA_PORT PORTD
      #define LCD_Speed Meduim
    
      dim HX711_BIT as Byte   ;Current bit being read in
      dim HX711_RAW as long   ;Raw ADC input 2's Compliment
      dim HX711_AVG0 as long  ;Average 0, or most recent read from HX711
                              ;Note: (Not averaged in this example)
    
      dir HX711_DOUT in ;HX711 Serial Data Out (From HX711)
      dir HX711_SCK out ;HX711 Serial Clock
    
      CLS
      Locate 0,0
      Print "START"
      wait 1 s
    
    MAIN:
      Do Forever
        CALL HX711_GET  ;Get reading from HX711
    
        ;Display ADC result
        CLS
        Locate 0,0
        Print HX711_AVG0 ;Print latest reading from HX711
        wait 2 s
      Loop
    
    SUB HX711_GET
      ;HX711 24 bit ADC in 2's complement
      ;Each bit read on each clock (24bits), MSB first
      ;25 Clock pulses sets next read: Channel A, Gain = 128
      ;26 Clock pulses sets next read: Channel B, Gain = 32
      ;27 Clock pulses sets next read: Channel A, Gain = 64
      ;ADC Result returned in HX711_AVG0
    
      HX711_SCK = 0 ; Ensure clock low before we start
      HX711_BIT = 0 ;Setup for first bit
      HX711_RAW = 0
      wait 2 us
    
      do while HX711_DOUT = 1  ;if SDO high, HX711 is not ready
      loop
    
      ;HX711_DOUT now low, so is ready
      ;Clock in ADC result
      Do while HX711_BIT < HX711_BITS ;Do for 24bits. < as first pass Bit 0
        HX711_SCK = 1 ;Clock next bit
        wait 2 us     ;Data should be on DOUT 1us after clock
    
        if HX711_DOUT = 1 Then
          HX711_RAW.0 = 1 ;If serial data in is High, set Bit 0
        else
          HX711_RAW.0 = 0 ;If serial data in is Low, clear Bit 0
        end if
    
        HX711_SCK = 0 ;Set clock low
        wait 2 us
    
        HX711_BIT = HX711_BIT + 1 ;Setup for next bit in
        ROTATE HX711_RAW LEFT Simple ;Move the data left, ready for next bit
    
      loop
      ;Now Clocked in 24 Bits,
      ;but need CLK 25 times to set resolution and channel of next conversion
      ;Cycle clock one more Time
      HX711_SCK = 1
      wait 2 us
      HX711_SCK = 0
      wait 2 us
    
      ;Convert RAW from (2's compliment) to decimal/binary
      HX711_AVG0 = NOT HX711_RAW ;Invert
      HX711_AVG0_E = 0 'To clear upper byte, as only 24bit of 32bit
      HX711_AVG0 = HX711_AVG0 + 1 ;add 1
    
    end sub
    
     
  • James Whyte

    James Whyte - 2020-04-20

    How to reduce the sample noise for the code above?
    - Sample at a higher rate, or get more samples. I use at least 16Hz
    - Use a rolling average of 8 samples.

    Add:

        dim HX711_AVG1 as long  ;Average 1 in binary
        dim HX711_AVG2 as long  ;Average 2 in binary
        dim HX711_AVG3 as long  ;Average 3 in binary
        dim HX711_AVG4 as long  ;Average 3 in binary
        dim HX711_AVG5 as long  ;Average 3 in binary
        dim HX711_AVG6 as long  ;Average 3 in binary
        dim HX711_AVG7 as long  ;Average 3 in binary
        dim HX711_AVGOUT as long ;the 4 inputs/4
    

    And for each time you sample the HX711 (CALL HX711_GET), also call HX711_AVG.

    Average output of samples is in HX711_AVGOUT

    Sub HX711_AVG
    
        HX711_AVGOUT = HX711_AVG0 + HX711_AVG1
        HX711_AVGOUT = HX711_AVGOUT + HX711_AVG2
        HX711_AVGOUT = HX711_AVGOUT + HX711_AVG3
        HX711_AVGOUT = HX711_AVGOUT + HX711_AVG4
        HX711_AVGOUT = HX711_AVGOUT + HX711_AVG5
        HX711_AVGOUT = HX711_AVGOUT + HX711_AVG6
        HX711_AVGOUT = HX711_AVGOUT + HX711_AVG7
        HX711_AVGOUT = HX711_AVGOUT/8
    
        ;Now shuffle the samples for next time
        HX711_AVG7 = HX711_AVG6
        HX711_AVG6 = HX711_AVG5
        HX711_AVG5 = HX711_AVG4
        HX711_AVG4 = HX711_AVG3
        HX711_AVG3 = HX711_AVG2
        HX711_AVG2 = HX711_AVG1
        HX711_AVG1 = HX711_AVG0
    
    end Sub
    
     

    Last edit: James Whyte 2020-04-20

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.