HUM: HX711 + LOAD-CELL

You are a beginner with Dasduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by soldered where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect module with Dasduino and the basic code. Everything else is left to your imagination.
INTRODUCTION
In this tutorial, we will get acquainted with the working principle of a very useful module, which is used on a daily basis, i.e. a load cell module for measuring mass. We will also get acquainted with an analog-to-digital converter that represents a link between the load cell and Dasduino. This module is used in digital scales, as well as an analog-to-digital converter, in a somewhat different performance, but working on the same principle. To find out what is the load cell, and what is an analog-to-digital converter, we will describe how they work and what they contain.
HX711 module characteristics:
• ADC precision: 24-bit
• Gain: 128 for the A channel, 64 for the B channel
• Voltage: 2.7V – 5.5V
• Current: < 1.5mA
• Reading speed: 10SPS(samples per second) ili 80SPS
• Dimensions: 22 x 22 mm
• Communication: Serial
HOW DOES IT WORK?
For starters, let us learn something about the load cell, how does it work and what is it made of. The load cell is actually a variable resistor, located in a metal housing, which changes the resistance depending on the strain or force that acts on the housing. There are various versions depending on the force they can be loaded with, resistance, change of resistance, etc. In our example, four resistors are deployed in a metal housing and connected in an appropriate manner, i.e. in Wheatstone’s Bridge, so that the resistance of one pair of resistors decreases and the other pair’s resistance increases, and all we need to do is measure this change in resistance to know the amount of force.
In order to determine the mass (force), we have to measure the change of resistance of the resistors that are incorporated into our module. The image shows us how to connect the resistors in the bridge, where the power supply is connected and where the voltmeter that measures the voltage is when the bridge is not in balance. From the change in resistance, we can determine the mass that uses force to act on the load cell.
Since resistance changes are very small, the voltage change is also low, and that is why we use the HX711 analog-digital converter with an amplifier that amplifies the signal 128 times. The analog-digital converter is made to connect the load cell that is made by the Wheatstone bridge technique. The converter converts resistance measurement into digital data and thus sends them to the microcontroller which provides us with the mass data by processing the data measured by the load cell.
HOW TO CONNECT IT?
The load cell is connected to an analog-to-digital converter with four wires, in Wheatstone’s bridge, we have two wires for power supply and two for measurement. The colors of the conductors are standardized, so we have a red and black power supply cable (red E+, and black E-), and white and green wires for measurement, and we connect them to the amplifier (green A-, white A +), and we also have a yellow a wire that serves to reduce electromagnetic interference induced in the conductors. The yellow wire connects to the pin labeled with SHIELD.
When we connect the load cell to an analog-to-digital converter we need to connect it to Dasduino too. As our converter uses digital communication, we only need to connect the DOUT and CLK pins of the converter to Dasduino digital pins that we have defined in the code and we are done connecting.
ARDUINO CODE
To work with the HX711 analog-digital converter, we have to install the HX711 library that contains all the required functions. If you do not know how to install the library, check our tutorial.
The first example code serves to determine the calibration factor that we need in order to measure the mass correctly. When this code is uploaded to Dasduino and we open the Serial Monitor, we will have the mass and the calibration factor displaying. The code describes the keys used to reduce or increase the calibration factor and the calibration process itself. Once we determine the calibration factor for a particular load cell, it does not need to be redefined because it does not change. If we use another load cell, to which we do not know the calibration factor, then it must be determined in the described way in order to have correct mass measurements.
#include "HX711.h" //including the library necessary to work with the HX711 ADC
#define DOUT 5 //the pin to which we connect the DOUT pin of the converter
#define CLK 6 //the pin to which we connect the CLK pin of the converter
HX711 scale(DOUT, CLK);
//HX711 ADC library constructor
//calibration factor that needs to be determined (not exact for current load cell), it will be changed through Serial monitor in order to determine the exact factor
float
calibration_factor = -96650;
void
setup
() {
Serial.begin(9600);
//Serial communication initialization ( speed 9600 baud)
//Printing the calibration process on the Serial monitor
Serial.println(
"HX711 Calibration"
);
Serial.println(
"Remove the weight from the scale"
);
Serial.println(
"After the readings start, put the familiar weight on the scale"
);
Serial.println(
"Now the calibration factor needs to be changed, to get the precise reading"
);
Serial.println(
"Press a,s,d,f for increasing the calibration factor for 10,100,1000,10000"
);
Serial.println(
"press z,x,c,v for lowering the calibration factor for 10,100,1000,10000"
);
Serial.println(
"Press t to tare"
);
scale.set_scale();
scale.tare();
//set the scale to 0 (taring)
}
void
loop
() {
scale.set_scale(calibration_factor);
//adjusting the calibration factor we submit to the function
Serial.print(
"Reading: "
);
Serial.print(scale.get_units(), 3);
// we get the mass rounded to 3 decimals
Serial.print(
" kg"
);
//printing that it is about kilograms
Serial.print(calibration_factor);
//printing the calibration factor
Serial.println();
if
(Serial.available())
//if the user has entered something via Serial monitor, then the following code is executed
//depending on the sent letter, we change the calibration factor for a certain value
{
char
temp = Serial.read();
if
(temp ==
'+'
|| temp ==
'a'
)
calibration_factor += 10;
else
if
(temp ==
'-'
|| temp ==
'z'
)
calibration_factor -= 10;
else
if
(temp ==
's'
)
calibration_factor += 100;
else
if
(temp ==
'x'
)
calibration_factor -= 100;
else
if
(temp ==
'd'
)
calibration_factor += 1000;
else
if
(temp ==
'c'
)
calibration_factor -= 1000;
else
if
(temp ==
'f'
)
calibration_factor += 10000;
else
if
(temp ==
'v'
)
calibration_factor -= 10000;
else
if
(temp ==
't'
)
scale.tare();
//if the letter T is sent, then we set the scale to 0 (taring)
}
}
In this code, it is necessary to change the calibration factor that we have determined for the load cell that we will use.
If we do not want to check what is printed on the Serial Monitor, we can delete the parts of the code that are printed on the Serial Monitor so that the program takes up less memory or if we do not have the display we can exclude the part of the code for display and leave the Serial Monitor.
#include "HX711.h" //including the library that serves to work with the HX711 ADC
#include <Wire.h> //including the wire library that allows the I2C communication
#include <Adafruit_SSD1306.h> //including the library used to work with the OLED display
#define SCREEN_WIDTH 128 //defining display width, in pixels
#define SCREEN_HEIGHT 32 //defining display height, in pixels
#define DOUT 5 //the pin to which we connect the DOUT pin of the converter
#define CLK 6 //the pin to which we connect the CLK pin of the converter
long
t;
//the variable to which we save the time from starting Croduino, using which we make a break between readings
//HX711 ADC library constructor
HX711 scale(DOUT, CLK);
//Our OLED display library constructor, for access to the functions that control the display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
//calibration factor determined in the first example code, and we need to enter it here
float
calibration_factor = -35300;
void
setup
() {
Serial.begin(9600);
if
(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
//Address 0x3C for 128x32 display
//if the initialization on the Serial monitor is not successful we receive an error message
Serial.println(F(
"SSD1306 unsuccessful initialization"
));
for
(;;);
//an infinite loop, if the initialization is unsuccessful, for the program to stop performing
}
Serial.println(
"Remove all weights from the scale"
);
Serial.println(
"Press t to tare"
);
scale.set_scale();
scale.tare();
//set the scale to 0
}
void
loop
() {
scale.set_scale(calibration_factor);
//setting the calibration factor that has been declared in the beginning
if
(
millis
() > t + 1000) {
//1 second pause function that does not stop the program as delay,
// but only prints readings each second, since the program is constantly executing
float
masa=scale.get_units();
//we save the mass read from the scale in the mass variable
Serial.print(
"Reading: "
);
Serial.print(masa, 3);
// printing the mass rounded to 3 decimals
Serial.println(
" kg"
);
display.clearDisplay();
//function for deleting everything off the display
display.setTextSize(1);
//function for setting the text size
display.setTextColor(WHITE);
//function for setting the color of the text (our display can only contain one color)
display.setCursor(0,0);
//function for setting the cursor (0-128,0-32) on the display where we want to start printing the text
display.print(
"Mass: "
);
display.print(masa);
//printing the mass on the OLED display
display.print(
" kg"
);
display.display();
//displaying the full text that we have previously sent to the buffer of the display
t=
millis
();
//to the t variable, we save the new time that has passed from starting the Croduino
}
//if the user has entered something via Serial monitor, then the following code is executed
if
(Serial.available())
{
char
temp = Serial.read();
//if the letter T is sent, set the scale to 0 (taring)
if
(temp ==
't'
)
scale.tare();
}
}