Help with HX711 Strain Gauge

Posted on
  • Hello,

    I am trying to get the HX711 IC to work with espruino. I have had some issues with getting it to work and would be very grateful if someone could advise me. The datasheet can be found at
    https://cdn.sparkfun.com/datasheets/Sens­ors/ForceFlex/hx711_english.pdf

    My code so far:

    // Strain Gauge Program for HX711
    // By: Aaron Button
    // Date: 16/05/16
    //______________________________________­________________
    //
    //User settable variables
    //______________________________________­________________
    //
    //Set Gain 1= 128, 2=32, 3=64.
    var gain = 1;
    //Calibration factor
    var calFac = -100;
    // Pin Number for strain gauge SCK
    var sgSend = C12;
    // Pin Number for strain gauge DOUT
    var sgRecieve = C10;
    //
    //Calculated Variables
    //______________________________________­________________
    //
    var strain = 0;
    var weight = 0;
    var bitValue  = 0;
    var tareStrain = 0;
    //
    //Strain Gauge
    //
    //Define the function for the strain gauge
    function strainGauge(){
      print("running strainGauge Routine");
      //Check if the HX711 Controller is ready to transmit
      if (digitalRead(sgRecieve) === 0){
        //Run the following code 24 times
        print("sgRecieve = 0");
        for (var i=24; i>1;i--){
          print("loop");
          print(i);
    
        digitalPulse(sgSend,1,0.001);//pulse on for 1us
        if (digitalRead(sgRecieve) == 1)//check if HX711 output if logic 1
        {
          bitValue = Math.pow(2, i);//calculate bit value
          print("bitValue =" + bitValue);     
          strain = strain + bitValue;//add the value to the total
          print("strain =" + strain);
        }
        // set the gain for the next transmission
          for (var o=0; o<gain;o++)//repeat the code as defined by gain
          {
            digitalPulse(sgSend,1,0.001);//pulse on for 1us
          }// End of gain statement
          weight = (strain*calFac);
          print("weight =" + weight);
        } // End for statement
      } // End if ready to transmit function
    }// End Function strainGauge
    
    // Set the unladen strain to 0
    function tare(){
      tareStrain=strain;
    }
    // Power down the strain gauge
    function powerdown()
    {
      digitalWrite(sgSend,1);//pulse on for 60us
    }// End of powerdown function
    
    // Power up after sleeping
    function powerUp()
    {
      digitalWrite(sgSend,0);
    }
    

    I am trying to measure the torque from a bicycle by measuring tension of the chain. A load cell will be cantilevered off of the frame and a idler wheel will pull the chain up by around 10 deg. I am thinking that the force pulling the pulley down will be around 20% of the total force in the chain. I would then like to use the measurement to drive a electric motor.

    I may also use it to weigh our fat cat...

    Thank you in advance

  • Hi - I think one issue is that digitalPulse is asyncronous - the pulse happens in the background.

    You might have more luck changing it to:

    digitalWrite(sgSend,1);
    digitalWrite(sgSend,0);
    

    At 1uS pulses, the above will be fine (due to code execution speed it's very unlikely to ever exceed 1uS there :)

    Also, as far as I can tell, the gain is selected by the number of clock pulses. I think (and this could be the main issue) the loop you have (for (var o=0; o<gain;o++)) should be after the main loop, not inside it. It's not quite indented right (where you read the bit) and that might have made you think the for loop had ended when it actually hadn't

    As it is, I think instead of sending 25 pulses (24+1) you're actually sending 48 (24*2).

    Hope that helps!

  • Someone else just asked about the HX711, and there is a possibility that using normal JS code will be a bit too slow for it. If your code doesn't work, I posted some ideas here: http://forum.espruino.com/conversations/­258182/#comment12991634

  • yes it's me.
    sorry, I've mistaked on the other post. I'll post on this thread now.
    I managed to have a 22microsecond pulse with digitalPulse(..,.., 1000/65000) for a 65khz freq.
    digitalwrite alternance just makes a 10ms perior for high time, this is too long for hx711, we have a 50microsecond limit according to the specs for the clock.
    I've checked with an oscillo.
    I'll finish my hx711 javascript port and post it there.

  • Gordon,

    I have tried your minified code from the other thread and I am receiving and I am getting the following

    >get()
    =169289
    >get()
    =169350
    >get()
    =169282
    >get()
    =169263
    >get()
    =169297
    

    Does this appear reasonable? the load cell is currently laying on the table.

    I can get a reading using digitalPulse but it is much larger at around 338642 and differs by a few hundred between readings.

    Many thanks again...

  • Well, the fact that it only differs by a bit each time sounds good. It's a 24 bit number, so could give you anything up to 24 million. The fact it's 169k probably means it's basically unloaded.

    I'd experiment by putting different stresses on it and seeing how it changes.

    Also, 338642 / 2 = 169321 - so it seems like the digitalPulse code might be working too, although maybe you're reading one too many bits, which is pushing the value up. Personally I'd avoid digitalPulse though because the async-ness of it might make it less reliable. The SPI method is quite efficient too.

  • There's some code for this posted up in another thread now: http://forum.espruino.com/conversations/­287046/#comment14322664

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Help with HX711 Strain Gauge

Posted by Avatar for AaronB @AaronB

Actions