HX711 Sample code to configure ADC pin

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DeAa_335316
Level 4
Level 4
First like received

I am working on CYBLE-416045-02 EZ-BLE Module and HX711 24-Bit Analog-to-Digital Converter (ADC) for Weigh Scales.

As per datasheet link----https://cdn.sparkfun.com/assets/learn_tutorials/5/4/6/hx711F_EN.pdf

https://cdn.sparkfun.com/assets/learn_tutorials/5/4/6/hx711F_EN.pdf

Serial Interface

Pin PD_SCK and DOUT are used for data retrieval, input selection, gain selection and power down controls. When output data is not ready for retrieval, digital output pin DOUT is high. Serial clock input PD_SCK should below. When DOUT goes to low, it indicates data is ready for retrieval. By applying 25~27 positive clock pulses at the PD_SCK pin, data is shifted out from the DOUT output pin. Each PD_SCK pulse shifts out one bit, starting with the MSB bit first, until all 24 bits are shifted out. The 25th pulse at PD_SCK input will pull DOUT pin back to high (Fig.2). Input and gain selection are controlled by the number of the input PD_SCK pulses (Table 3). PD_SCK clock pulses should not be less than 25 or more than 27 within one conversion period, to avoid causing serial communication error.

Reference Driver(C)

//-------------------------------------------------------------------

sbit ADDO = P1^5;

sbit ADSK = P0^0;

unsigned long ReadCount(void){ unsigned long Count; unsigned char i;

ADDO=1;

ADSK=0;

Count=0;

while(ADDO);

for (i=0;i<24;i++)

{ ADSK=1;

Count=Count<<1;

ADSK=0;

if(ADDO)

Count++;

}

ADSK=1;

Count=Count^0x800000;

ADSK=0;

return(Count);

}

_______________________________________________________________________________

For Arduino sample code:

/* This program takes 10 samples from LC + HX711B at

   1-sec interval and then computes the average.

*/

unsigned long x = 0, y=0;

unsigned long dataArray[10];

int j = 0;

void setup()

{

  Serial.begin(9600);

  pinMode(A1, INPUT); //data line  //Yellow cable

  pinMode(A0, OUTPUT);  //SCK line  //Orange cable

}

void loop()

{

  for (int j = 0; j < 10; j++)

  {

    digitalWrite(A0, LOW);//SCK is made LL

    while (digitalRead(A1) != LOW) //wait until Data Line goes LOW

      ;

    {

      for (int i = 0; i < 24; i++)  //read 24-bit data from HX711

      {

        clk();      //generate CLK pulse to get MSB-it at A1-pin

        bitWrite(x, 0, digitalRead(A1));

        x = x << 1;

      }

      clk();  //25th pulse

      Serial.println(x, HEX);

      y = x;

      x = 0;

      delay(1000);

    }

    dataArray = y;

  }

  Serial.println("===averaging process=========");

  unsigned long sum = 0;

  for (j = 0; j < 10; j++)

  {

    sum += dataArray;

  }

  Serial.print("Average Count = ");

  sum = sum / 10;

  Serial.println(sum, HEX);

// float W = (float)0.90*(sum-901002)/946560 + 0.75;//0.005331 * sum - 1146.176;

  //W = (float)W / 1000.00;

  Serial.println(W, 2);

}

void clk()

{

  digitalWrite(A0, HIGH);

  digitalWrite(A0, LOW);

}

-----------------------------------------------------------------------------------------

while the development of a similar code. I am struggling to access HX711 serial pin via ADC as input and output. I am looking for support of the HX711 sample code with ADC or GPIO pin configured to configured HX711 in your project.

Best Regards

Deepak Aagri

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Deepak,

You can try to copy Pin settings and code from the HX711 custom component, made for PSoC4 and PSoC5

HX711: 24-bit Delta Sigma ADC interface for weight scale

The component has not been tested on PSoC6, but some portions of it might work on M0 processor.

You might also be interested using the median filtering instead of the moving average, as it is better discriminates spikes from load/unload.

MedianFilter: sliding window median filter component

The code can be directly copied from the datasheet attached.

/odissey1

HX711_P4_basic_01a_A.png

View solution in original post

0 Likes
2 Replies
Vasanth
Moderator
Moderator
Moderator
250 sign-ins 500 solutions authored First question asked

Hi Deepak,

I could not find any ported example code in our repository. We can help you with the problems while you port the code. Let us know what is blocking you.

Best Regards,
Vasanth

lock attach
Attachments are accessible only for community members.
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Deepak,

You can try to copy Pin settings and code from the HX711 custom component, made for PSoC4 and PSoC5

HX711: 24-bit Delta Sigma ADC interface for weight scale

The component has not been tested on PSoC6, but some portions of it might work on M0 processor.

You might also be interested using the median filtering instead of the moving average, as it is better discriminates spikes from load/unload.

MedianFilter: sliding window median filter component

The code can be directly copied from the datasheet attached.

/odissey1

HX711_P4_basic_01a_A.png

0 Likes