Arduino Digital Scale
In this guide, we are showing you how to set up an Arduino Digital Scale using a load cell and the HX711 amplifier. First of all, you are shown how to connect the load cell and the HX711 amplifier to the Arduino to set up the scale. Then, we will guide you to adjust the scale, and an easy example to obtain the weight of the objects. And after, we are adding a display to indicate the measurements and a button to make zero the scale.
Introducing Load Cells
A load cell is used to convert the power into an electrical signal to do the measurements. The electrical signals are comparatively converted to the force applied. There are various types of load cells available: strain gauges, pneumatic, and hydraulic. Here in this article, we talk about strain gauge load cells.
The strain gauge load cells are gathered to a metal bar with linked strain gauges. A strain gauge is an electrical sensor that counts the force or the strain of an object. There are create various resistance levels of the strain gauges when any external force affects an object. This makes deformations on the object’s shape at this metal bar an example. The strain gauge resistance corresponds to the weight applied, and this lets us count the whole weights of the objects.
Most of the time, load cells are with four strain gauges associated with a Wheatstone bridge that let us receive the correct resistance counts. You can understand this by seeing the below image. There is information on the strain gauge process available.
The wires that come out from the cell have got following colors.
- Red color: VCC (E+)
- Black color: GND (E-)
- White color: Output – (A-)
- Green color: Output + (A+)
Applications
The strain gauge load cells are usable in many applications. As example:
- It can be used to check the weight of the objects varies over time.
- It checks the weight of any object
- Able to determine the availability of the object.
- It is used to rate the liquid level of the container etc
The thing is it only takes some little changes in strain when weighing objects. The cell we use comes with an HX711 amplifier. So that is the amplifier that we have to use.
HX711 Amplifier
The HX711 amplifier is used as a breakout board that supports you in easily identifying the load cells to count the weights. You set up the load cell wires on one side and the microcontroller on the other side. The HX711 links with the microcontroller using the two-wire interface.
You have to fuse the pins on the GND, DT, SCK, and VCC to link to the Arduino. I linked the load cells wireless straight to the E+, E-, A-, and A+ pins. The load cell wires are very thin and breakable. So you have to be very careful because there can be dames when linking them.
Setting Up the Load Cell
The load cell kit comes with two acrylic plates and a few screws to make the load cells a plate. You have the chance to use wood plates or 3D print for your own plates.
You have to link the plates to the load cell to build up pressure between the two opposite ends of the metal bar. The load cell is held by the plate at the bottom and you place the objects on the upper plate.
The below image shows the appearance of the load cells with the acrylic plates.
Where to Buy Load Cell with HX711?
If you check on Maker Advisor you will be able to find the load cell with the HX711 for a good price. They will be available with acrylic plates or without them. There are available various types of measurement ranges. The most used maximum weight categories are 1kg, 5kg, 10kg, and 20kg.
The previous link can be used to find all the parts needed for your project. Or else search them on MakerAdvisor.com/tools and you will get all for what you are searching for.
Wiring Load Cell and HX711 Amplifier to the Arduino
The HX711 amplifier has two wire interfaces for communication. They can be linked to any digital pins of the Arduino board. We connect the data pin (DT) to Pin 2 and the clock pin to Pin 3.
Check the below-given table or the simplified diagram to link the load cell to the Arduino board.
Installing the HX711 Library
There are various libraries to have measurements of a load cell by using the HX711 amplifier. We are using the HX711 library by bodge. It is well suited for the ESP32, ESP8266, and Arduino.
Arduino IDE
If you have been using the Arduino IDE follow the below methods to install the library.
- Open the Arduino IDE and move to Sketch > Include Library > Manage Libraries.
- Find the “HX711 Arduino Library” and you can install the library by Bogdan Necula.
Calibrating the Scale (Arduino with Load Cell)
By this time, we think that you have linked the load cell to the HX711 amplifier and the amplifier with the Arduino board. There you should set up the scale (It should be two wired plates linked at opposite ends on the load cell.), and the HX711 library should have been installed.
Before the objects become more weight, you have to adjust your load cell by the calibration factor. The calibration factors are different from each other, so note not to miss that part.
- Have an object that you do not know the weight of. For example, I got the kitchen scale and measured the glass of water on it. (104g)
- Upload the below code to the Arduino board. The following code is written by considering the instructions to adjust a load of cells given by the library documentation.
- After completing the upload, go to the Serial Monitor at a baud rate of 57600and press the RESET button on the Arduino board.
- Refer to the guide on the serial Monitor. Don’t put any weight on the scale. Because it can be tared automatically. Then put an object that you know the weight correctly on the scale and check its value of it.
- Study the below formula and calculate the calibration.
On this occasion, the reading is -49171. The weight we know is 104g, if it is the calibration factor equal becomes -49171/107 = -459.542.
calibration factor = -49171/107 = -459.542
You should save this calibration factor as it will be needed at later times. You can get a different one than ours.
The thing is the result of the sensor corresponds to the force applied to the load cell. You have the chance to adjust the scale using any unit that you like. I have used grams and you can use either pounds, kilograms, or even some cat foods.
Weighting Objects (Arduino with Load Cell)
Now you have the knowledge about the calibration factor. And you are able to use the load cell to weigh the objects. Do some weighing with a weight known object and repeat the same process if you do not get the correct values.
Get the below code to your Arduino IDE. You should keep in remember to insert the calibration factor before uploading it to the board. It should be in line 43/44 of the code. The below-given command is an example given by the library that shows the use of the relevant functions.
How the Code Works
You can begin it by including the relevant library.
#include “HX711.h”
The below shows lines give the pins that can be used to link to the HX711 amplifier. We are using Pin 2 and Pin 3. You have the freedom to choose any digital pins that you like.
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
Next, create an occasion of the HX711 library called scale. This will be needed later to get the measurements.
HX711 scale;
setup()
In the setup() part, format the Serial monitor.
Serial.begin(57600);
Edit the load cell by entering the begin() methods on the scale object and passing the digital pins as arguments.
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
Next, it gives some methods that can be used to obtain the readings using the library.
- read(): Receives a rough reading from the sensor.
- read_average(number of readings): Receives the standard of the recently defined number of readings.
- get_value(number of readings): Receives the average of the last marked number of readings after reducing the tare weight.
- get_units(number of readings): Receives the average of the previously defined number of the readings reduced the tare weight separated by the calibration factor. This makes an output of the reading in the desired units.
In the next lines do not forget to include the calibration factor. The method set_scale() is used by it.
scale.set_scale(INSERT YOUR CALIBRATION FACTOR)
After that, call the tare() method to make the scale zero.
scale.tare(); // reset the scale to 0
After that arrangement, the scale becomes ready to make the correct readings in the desired unit. As the example returns the earlier methods you can see the variation of the settings up before and after the scale.
loop()
In the loop() the example takes the get_units() method according to two different structures: to receive one single reading without any specifications and to have the average of the last 10 readings.
Serial.print(“one reading:\t”);
Serial.print(scale.get_units(), 1);
Serial.print(“\t| average:\t”);
Serial.println(scale.get_units(10), 5);
It stops the ADC that reads the sensor using the method power_down(). And it remains 5 seconds, start the ADC (power_up()) and the loop() repeats. So you receive new readings on the Serial Monitor every 5 seconds.
scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();
Demonstration
Upload the code to the Arduino board. After that launch the Serial monitor at a bandwidth of 115200.
Wait until the code runs a few seconds as it gets the time to set up the scale. (Note that some messages will appear on the Serial monitor.) Then put any object on the scale to, measure and see the results on the Serial monitor.
I examined various objects and compared the weight with my kitchen scale, and got the same values. So I can believe that my Arduino scale is accurate as the Kitchen scale.
Digital Scale with Arduino
In this part, we are creating a simple digital scale using Arduino. We are adding an OLED display to get the results and a push button to make zero the scale.
Parts Needed
There is a list of needed items for this project.
- Arduino UNO (read Best Arduino starter kits)
- Load Cell with HX711 Amplifier
- I2C SSD1306 OLED Display
- Pushbutton
- 10K Ohm Resistor
- Breadboard
- Jumper Wires
Schematic Diagram
Add an OLED display with a pushbutton to the earlier circuit on the below pins.
Note to connect to 3.3V or 5V based on the model.
Link the pushbutton using a 10kOhm pull-down resistor to Pin 4. The other side of the pushbutton has to be connected to 5V. So use any other Arduino digital pin.
Refer to the below-explained diagram to link the parts.
Arduino Digital Scale – Code
Simply, we control the pushbutton using a simple library that identifies the button presses with debouncing. Therefore we do not need to think about the code. To make the OLED display, we use the Adafruit SSD1306 and Adafruit GFX libraries.
Pushbutton Library
There are lots of libraries with numerous functions to control the pushbuttons. We are using the pushbutton library by polu. It seems like a simple library but contained all the things that are required for a project. In your Arduino IDE go to Sketch > Include Library > Manage Libraries and find the “pushbutton”. So install the pushbutton library by polu.
On the other hand, if you are not willing to use the library add the debounce code and it is not a difficult thing. An escalation for debounce code in the Arduino IDE goes to the File > Examples > Digital > Debounce.
OLED Libraries
We use the below libraries to control the OLED display. So you should have installed the below libraries.
Adafruit_SSD1306 library
Adafruit_GFX library
You have the possibility to install the libraries using the Arduino Library manager. Go to Sketch > Include Library > Manage Libraries and get the library name.
Code
Copy the below code into the Arduino IDE. Before uploading it to the Arduino board, you have to enter the calibration factor that you got earlier.
How the Code functions
Start by entering the relevant libraries.
Name the pins for the HX711 (load cell). Here we use the same one as the earlier example.
Set up an HX711 case called scale.
HX711 scale;
The below variables hold the present weight and last weight readings. We only need to update the OLED display in case of new readings. That is the reason we need those two variables. Moreover, we don’t need to count the decimals of the grams which makes the scale too delicate for the application. This is the reason for the variables to become integers. If you need the decimals for the measures, define the float variables.
int reading;
int last reading;
You should replace the next value with the calibration factor. I have got a negative value and you can see it below.
#define CALIBRATION_FACTOR -459.542
Next, let’s define the OLED width and height.,
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Create an occasion of the Adafruit_SSD1306 library named display.
Decide the GPIOm that you use to read the button and make a Pushbutton object called button on the pin.
displayWeight() function
We have made a function called displayWeight() that takes as arguments the weight we need to show on the OLED.
setup()
In the setup() part we format the Serial Monitor.
Format the OLED display.
And at last format the load cell.
loop()
The push-button library lets us wait some time for an event of a push-button press or release. In this part, we check if the pushbutton was moved using the getSingleDebouncePress() method and call the tare() function if you press the button.
The HX711, readings are obtainable without any blocking. It specifies a maximum timeout to wait for hardware initialization and avoids blocking your code if the scale becomes disconnected or creates any hardware issues.
We continuously obtain the latest readings and compare them with the new reading in the loop(). In order to update the OLED display when receiving a new measurement, we call the displayWeight() function.
Demonstration
You can use the load cell to weigh objects after uploading the code to the board. The OLED display will show the readings. The scale can be tare by using the button.
Again the readings on the Arduino digital scale keep in touch with the readings on the Kitchen scale.
Wrapping Up
So far you have learned how to make an interface for a strain gauge load cell using the HX711 amplifier on an Arduino board. The output received from the load cell corresponds to the force given. So we can adjust it to use in g, kg, ib, or any kind of unit that will be relevant to the project.
In conclusion, you got to know how to determine an object’s weight and adjust a scale. Moreover, you learned how to build up an Arduino digital scale with an OLED display that shows the measurements with a pushbutton to tare the scale.
By reading the tutorial about Arduino digital scale you should have got the hope of starting a load cell. As it is useful to measure the object weights, we can use it in several applications to know the availability of an object, to measure the liquid of a water tank, to measure the water evaporation rate, and check if there is food on your pet’s bowl.
Frequently asked questions:
How do you calibrate an Arduino scale?
Keep a weighted object on the load cell. Wait a moment for the Reading message to appear on the serial monitor. To get the correct weight, use the A key to increase the calibration factor by one unit and the Z key to lower it. Now you have calibrated the scale.