im working on a project with four weight sensors and an HX711 module. I tried to run the script below with this library.
My main issue is that i didnt have anything on my serial monitor whereas i print "HX711 test" at the begining. I used an arduino pro micro. Did you ever used this library with a pro micro ? I tried this script exemple in the HX711 library and it worked. I dont understand what is happening.
Thank you for your help.
#include "Arduino.h"
#include <HX711.h>
//void calibrate();
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 8;
const int LOADCELL_SCK_PIN = 7;
HX711 scale;
void setup() {
Serial.begin(57600);
Serial.println("HX711 test");
Serial.println("Initializing the scale");
// Initialize library with data output pin, clock input pin and gain factor.
// Channel selection is made by passing the appropriate gain:
// - With a gain factor of 64 or 128, channel A is selected
// - With a gain factor of 32, channel B is selected
// By omitting the gain factor parameter, the library
// default "128" (Channel A) is used here.
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_gain(128);
// Serial.println("Before setting up the scale:");
// Serial.print("read: \t\t");
// Serial.println(scale.read()); // print a raw reading from the ADC
//
// Serial.print("read average: \t\t");
// Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
//
// Serial.print("get value: \t\t");
// Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
//
// Serial.print("get units: \t\t");
// Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// // by the SCALE parameter (not set yet)
//
// scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
// scale.tare(); // reset the scale to 0
//
// Serial.println("After setting up the scale:");
calibrate();
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
}
void loop() {
Serial.print("one reading:\t");
Serial.print(scale.get_units(), 1);
Serial.print("\t| average:\t");
Serial.println(scale.get_units(10), 1);
// scale.power_down(); // put the ADC in sleep mode
delay(2000);
// scale.power_up();
}
void calibrate() {
// Remove any calibration values and clear the scale
scale.set_scale();
scale.tare();
// Prompt the user
Serial.println("Add your known weight to the scale, enter the weight and press <Enter>");
int userInput = -123;
String inputString = "";
// Loop until we receive an input (which will change the value of userInput
while (userInput == -123) {
// Read serial input:
while (Serial.available() > 0) {
int inputChar = Serial.read();
if (isDigit(inputChar)) {
// convert the incoming byte to a char and add it to the string:
inputString += (char)inputChar;
}
// if you get a newline, print the string, then the string's value:
if (inputChar == '\n') {
userInput = inputString.toInt();
}
}
}
// Now get the reading from the scale
float calReading = scale.get_units(10);
Serial.print("Setting the cal to ");
Serial.println(calReading / userInput);
scale.set_scale(calReading / userInput);
}
For boards with native USB (like your Pro Micro), you'll have to wait till a USB connection is established if you don't want to miss the first data. I suggest that you start with below to see if that will display the messages in setup(). After that you can start adding the rest of the code.
void setup() {
Serial.begin(57600);
// wait for connection to establish
while (!Serial);
Serial.println("HX711 test");
Serial.println("Initializing the scale");
}
void loop()
{
}
Notes
while(!Serial) will hang till e.g. the Serial Monitor is opened. If you eventually want to use your project without dependency on a terminal program, you will need a different solution that e.g. only blocks for a short while.
If you use external power and USB and later disconnect the USB, you're board can come to a near grinding halt if you keep on printing; this can be prevented by checking how much space there is left for printing using Serial.availableForWrite().
Thank you for your answer !
Your code works with my arduino.
I've changed the code and now I've got some things in the serial monitor.
However, when I need to enter a parameter, I enter a value in the serial monitor then validate and nothing happens.
Yes exactly. In any cases i will open the Serial monitor at the begining in order to calibrate my weight sensor. But after that i will disconect my usb cable and the arduino will be charge with my external power on VCC pin and my data is stored on the SD card.
I found the issue. The value of user input is never read. I put the "userInput" in another part of the code and I get results.
while (Serial.available() > 0) {
int inputChar = Serial.read();
Serial.print("inputChar:");
Serial.println(inputChar);
if (isDigit(inputChar)) {
// convert the incoming byte to a char and add it to the string:
inputString += (char)inputChar;
Serial.print("inputString:");
Serial.println(inputString);
userInput = inputString.toInt();
Serial.print("userInput");
Serial.println(userInput);
}
I calibrate my scales with a weight of 2500g and it works fine. However, when I step on the scales, I get a weight of 30kg when I'm 80...
HX711 Demo
Initializing the scale
Add your known weight to the scale, enter the weight and press <Enter>
inputChar:50
inputString:2
userInput2
inputChar:53
inputString:25
userInput25
inputChar:48
inputString:250
userInput250
inputChar:48
inputString:2500
userInput2500
Setting the cal to 11.64
read: 312234
read average: 312389
get value: 29706.00
get units: 2546.3
Readings:
one reading: 2542.4 | average: 2537.0
one reading: 2581.8 | average: 2555.8
one reading: 2550.5 | average: 2529.8
one reading: 2551.9 | average: 2540.2
one reading: 2503.9 | average: 2524.5
one reading: 2515.0 | average: 2521.0
one reading: 2530.0 | average: 2521.1
one reading: 2557.9 | average: 2531.2
one reading: 2474.2 | average: 2580.9
one reading: 36742.8 | average: 36981.2
one reading: 36348.8 | average: 36026.7
one reading: 35442.6 | average: 35702.0
one reading: 35927.6 | average: 36427.3
one reading: 36417.3 | average: 36544.9
And if i tried to calibrate when i step on the scale i obtained these values as below.
HX711 Demo
Initializing the scale
Add your known weight to the scale, enter the weight and press <Enter>
inputChar:56
inputString:8
userInput8
inputChar:48
inputString:80
userInput80
inputChar:48
inputString:800
userInput800
inputChar:48
inputString:8000
userInput8000
inputChar:48
inputString:80000
userInput14464
Setting the cal to 27.77
read: 675263
read average: 679407
get value: 410759.00
get units: 14799.4
Readings:
one reading: 14809.3 | average: 14812.4
one reading: 14833.7 | average: 14843.8
one reading: 14750.6 | average: 14773.2
one reading: 14817.6 | average: 14799.8
one reading: 14772.0 | average: 14776.9
one reading: 14460.6 | average: 14393.1
one reading: 13555.8 | average: 14948.3
#include "Arduino.h"
#include <HX711.h>
//void calibrate();
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 8;
const int LOADCELL_SCK_PIN = 7;
HX711 scale;
void setup() {
Serial.begin(57600);
while (!Serial);
Serial.println("HX711 Demo");
Serial.println("Initializing the scale");
// Initialize library with data output pin, clock input pin and gain factor.
// Channel selection is made by passing the appropriate gain:
// - With a gain factor of 64 or 128, channel A is selected
// - With a gain factor of 32, channel B is selected
// By omitting the gain factor parameter, the library
// default "128" (Channel A) is used here.
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_gain(128);
// Serial.println("Before setting up the scale:");
// Serial.print("read: \t\t");
// Serial.println(scale.read()); // print a raw reading from the ADC
//
// Serial.print("read average: \t\t");
// Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
//
// Serial.print("get value: \t\t");
// Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
//
// Serial.print("get units: \t\t");
// Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// // by the SCALE parameter (not set yet)
//
// scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
// scale.tare(); // reset the scale to 0
//
// Serial.println("After setting up the scale:");
calibrate();
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
}
void loop() {
Serial.print("one reading:\t");
Serial.print(scale.get_units(), 1);
Serial.print("\t| average:\t");
Serial.println(scale.get_units(10), 1);
// scale.power_down(); // put the ADC in sleep mode
delay(2000);
// scale.power_up();
}
void calibrate() {
// Remove any calibration values and clear the scale
scale.set_scale();
scale.tare();
// Prompt the user
Serial.println("Add your known weight to the scale, enter the weight and press <Enter>");
int userInput = -123;
String inputString = "";
// Loop until we receive an input (which will change the value of userInput
while (userInput == -123) {
// Read serial input:
while (Serial.available() > 0) {
int inputChar = Serial.read();
Serial.print("inputChar:");
Serial.println(inputChar);
if (isDigit(inputChar)) {
// convert the incoming byte to a char and add it to the string:
inputString += (char)inputChar;
Serial.print("inputString:");
Serial.println(inputString);
//userInput = inputString.toInt();
userInput = inputString.toInt();
Serial.print("userInput");
Serial.println(userInput);
}
// if you get a newline, print the string, then the string's value:
// if (inputChar == '\n') {
// userInput = inputString.toInt();
// Serial.print("userInput");
// Serial.println(userInput);
// }
}
}
// Now get the reading from the scale
float calReading = scale.get_units(10);
Serial.print("Setting the cal to ");
Serial.println(calReading / userInput);
scale.set_scale(calReading / userInput);
}