Quantcast
Viewing all articles
Browse latest Browse all 4867

C/C++ • Re: HX711 SimpleHx711Test Hanging

Thanks to the links from ame, I reworked used first principles to get a reading using WiringPi which works on Raspberry Pi 5.

It was good for me to be forced to understand the way the HX711 amplifier actually works. I see there are more options on gain etc I might need down the track.

Here is some source code for testing if anyone else is interested.

Code:

#include <iostream>#include <wiringPi.h>double readHx711Count(int GpioPinDT = 2, int GpioPinSCK = 3) {    wiringPiSetupGpio(); // Initialize wiringPi to use GPIO pin numbers (not WiringPi numbers)    // e.g. 22 = GPIO 22 = Wiring Pi Pin 3 = Physical Pin 15    int i;    unsigned int Count = 0;    pinMode(GpioPinDT, OUTPUT);    digitalWrite(GpioPinDT, HIGH);    digitalWrite(GpioPinSCK, LOW);    pinMode(GpioPinDT, INPUT);    while (digitalRead(GpioPinDT) == 1) {        i = 0;    }    for (i = 0; i < 24; i++) {        digitalWrite(GpioPinSCK, HIGH);        Count = Count << 1;                digitalWrite(GpioPinSCK, LOW);        if (digitalRead(GpioPinDT) == 0) {            Count = Count + 1;        }    }    digitalWrite(GpioPinSCK, HIGH);    Count = Count ^ 0x800000;    digitalWrite(GpioPinSCK, LOW);    return double (Count);}int main() {    auto offset = 8156931; // Run with 0 initially to get the average    auto actualWeight = 300.0; // Initially make 1 for calibration    auto measuredWeight = 113318.0; // Initially make 1 for calibration    auto factor = actualWeight / measuredWeight;     auto average = 0;    auto MaxMeasurements = 10;    for (auto measure = 1; measure <= MaxMeasurements; measure++) {        auto reading = readHx711Count();        average += reading;        std::cout << measure << "\tMeasure\t" << reading  << "\tWeight\t" << (reading-offset) * factor << std::endl;        delay(50);    }    average /= MaxMeasurements;    std::cout << "Average" << "\tMeasure\t" << average  << "\tWeight\t" << (average-offset) * factor << std::endl;    return 0;}
Sample results for my iPhone which according to my kitchen scales is 216g
/home/bk/Code/bin/Debug/003_hx711
1 Measure 8.2387e+06 Weight 216.466
2 Measure 8.23854e+06 Weight 216.05
3 Measure 8.23854e+06 Weight 216.048
4 Measure 8.23859e+06 Weight 216.193
5 Measure 8.23865e+06 Weight 216.336
6 Measure 8.23855e+06 Weight 216.08
7 Measure 8.23868e+06 Weight 216.416
8 Measure 8.23876e+06 Weight 216.635
9 Measure 8.23866e+06 Weight 216.384
10 Measure 8.2387e+06 Weight 216.471
Average Measure 8238636 Weight 216.307

Process finished with exit code 0


Process finished with exit code 0
Thanks all for your help in getting to the bottom of this. Add a comment if you find this useful.

Statistics: Posted by gettingstronger — Sun May 05, 2024 8:42 am



Viewing all articles
Browse latest Browse all 4867

Trending Articles