-
Notifications
You must be signed in to change notification settings - Fork 541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HX711 only reads once on startup #251
Comments
Did you try to call |
@Xarbenence
As I can see from the sources, I had and identical issue cause by PS. I'd greatly appreciate if you edit your post, use github's markdown to make it look like a code and clean it up from commented code. |
Hello,
I wanted to post a question regarding a project I am working on. I have an HX711 interfacing with 4 half bridge strain gauges: found here. When I use the library and initialize the board, I have a loop in my setup() that just checks to see if the board is ready to be read from. I never get stuck in that loop, and I can successfully read from the board immediately after doing scale.begin(DOUT,SCL); However, when I enter my actual loop(), scale.is_ready() always returns false. I even tested the first reading I get with different weights, and the value changes, telling me that the load cells/HX711 are working, but I'm at a loss for why I need can only read once. In my loop(), if I re-call scale.begin(), I can get more reads, but I don't want to have to reset the HX711 every time I want to read from it. Has anyone else experienced this issue? Thanks.
Code:
/************************ Adafruit IO Configuration *******************************/
// visit io.adafruit.com if you need to create an account,
// or if you need your Adafruit IO key.
// #define IO_USERNAME "xxxxx"
// #define IO_KEY "yyyyy"
/******************************* WIFI Configuration **************************************/
// #define WIFI_SSID "zzzzz"
// #define WIFI_PASS "wwwww"
// #include <AdafruitIO_WiFi.h>
// AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
/************************ Main Program Starts Here *******************************/
/******************************** Libraries **************************************/
//#include <ESP8266WiFi.h> // ESP8266 library
//#include <AdafruitIO.h> // Adafruit IO library
//#include <Adafruit_MQTT.h> // Adafruit MQTT library
//#include <ArduinoHttpClient.h> // http Client
#include <HX711.h> // HX711 library for the scale
#include "DFRobot_HT1632C.h" // Library for DFRobot LED matrix display
#define calibration_factor -21700.0 //-7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define DOUT 4 // Pin connected to HX711 data output pin
#define CLK 5 // Pin connected to HX711 clk pin
#define NUM_MEASUREMENTS 20 // Number of measurements
#define THRESHOLD 2 // Measures only if the weight is greater than 2 kg. Convert this value to pounds if you're not using SI units.
#define THRESHOLD1 0.5 // Restart averaging if the weight changes more than 0.5 kg.
#define DATA D6 // Pin for DFRobot LED matrix display
#define CS D2 // Pin for DFRobot LED matrix display
#define WR D7 // Pin for DFRobot LED matrix display
DFRobot_HT1632C ht1632c = DFRobot_HT1632C(DATA, WR,CS); // set up LED matrix display
//AdafruitIO_Feed *myWeight = io.feed("my-weight"); // set up the 'iot scale' feed
HX711 scale; // instantiate the scale
float weight = 0.0;
float prev_weight = 0.0;
void setup() {
// start the serial connection
Serial.begin(115200);
// setup LED matrix display
ht1632c.begin();
ht1632c.isLedOn(true);
ht1632c.clearScreen();
delay(500);
scale.begin(DOUT,CLK); // initialize scale
while(!scale.is_ready()){ // wait until the scale is ready to start reading
Serial.println("Initializing Scale");
delay(1000); // scale.is_ready() never fails
}
long reading = scale.read();
Serial.println("HX711 reading: ");
Serial.println(reading);
scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
Serial.println("Board Tared");
//scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
// connect to io.adafruit.com
//Serial.print("Connecting to Adafruit IO");
//io.connect();
// set up a message handler for the 'my weight' feed.
// the handleMessage function (defined below)
// will be called whenever a message is
// received from adafruit io.
//myWeight->onMessage(handleMessage);
//ESP.wdtDisable();
//ht1632c.print("connecting...",50);
// wait for a connection
// while(io.status() < AIO_CONNECTED) {
// Serial.print(".");
// ht1632c.print("...",50);
// ESP.wdtFeed();
// delay(500);
// }
// we are connected
// Serial.println(io.statusText());
// ESP.wdtFeed();
//ht1632c.print("connected...",50);
}
void loop() {
// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
//io.run();
ht1632c.clearScreen();
//ESP.wdtFeed();
weight = scale.get_units();
Serial.println(weight);
float avgweight = 0;
int i = 0;
while(i<9){
if(scale.is_ready()){ // this is_ready() always fails
long reading = scale.read();
Serial.print("HX711 reading: ");
Serial.println(reading);
break;
}
i++;
}
if(i==9){
Serial.println("Resetting");
scale.begin(DOUT,CLK);
long reading = scale.read();
Serial.println("HX711 reading: ");
Serial.println(reading); // these readings always work
Serial.println("Board reset");
}
// if (weight > THRESHOLD) { // Takes measures if the weight is greater than the threshold
// float weight0 = scale.get_units();
// for (int i = 0; i < NUM_MEASUREMENTS; i++) { // Takes several measurements
// delay(100);
// weight = scale.get_units();
// avgweight = avgweight + weight;
// if (abs(weight - weight0) > THRESHOLD1) {
// avgweight = 0;
// i = 0;
// }
// weight0 = weight;
// }
// avgweight = avgweight / NUM_MEASUREMENTS; // Calculate average weight
// char result[8]; // Buffer big enough for 7-character float
// dtostrf(avgweight, 6, 1, result);
// ht1632c.print(result); // Display on LED matrix
//}
}
// void handleMessage(AdafruitIO_Data *data) {
// }
The text was updated successfully, but these errors were encountered: