Skip to content

Instantly share code, notes, and snippets.

@robinkrens
Created February 21, 2022 10:37
Show Gist options
  • Save robinkrens/781d8c1c676bf4872f0f3f233ec04046 to your computer and use it in GitHub Desktop.
Save robinkrens/781d8c1c676bf4872f0f3f233ec04046 to your computer and use it in GitHub Desktop.
HX711 read force routine
static void read_force(void)
{
int count;
/* Offset and gain need to be calculated for each load cell */
int offset = 1234; /* calibration offset */
float gain = 1.818e-5; /* gain: convert output data to grams */
while(1) {
count = 0;
for (int i = 0; i < 24; ++i) {
gpio_set_level(CLOCK_PIN, 1);
count <<= 1;
gpio_set_level(CLOCK_PIN, 0);
if (gpio_get_level(DATA_PIN))
count++;
}
gpio_set_level(CLOCK_PIN, 1);
count <<= 8;
float grams = gain * (float) (count + abs(offset));
gpio_set_level(CLOCK_PIN, 0);
/* Note: delay is needed: HX711 cannot process data
faster 80 SPS */
vTaskDelay(100 / portTICK_RATE_MS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment