Blog - Sketching with Hardware

HX711 – How to measure (in)correctly

Published on: | Author: Jan Kaiser | Categories: 2018b, Tutorials

Introduction

The Naughty Weighn, known from Julia’s blog post, is a smart scale for human measurements. Built from a used scale purchased for exactly one purpose – experimenting – it was quickly taken apart and analyzed.

Image 1: The scale’s microcontroller and load-cell cables

As you can see from Image 1, the scale uses four weight sensors, which we identified as 50KG Load Cells. Our first thought was to use the original microcontroller, which proved to be problematic. The serial number of the printed circuit board (pcb) showed no search results and is likely only used internally by the manufacturer. The chip it uses is implemented using the chip-on-board (cob)-technique, and therefore hidden behind an epoxy-based material (the black blob that can be seen in Image 2). We asked the company for a data sheet, but have so far not gotten any kind of reply. Reverse-engineering the pcb might have been a possible solution, but not in the short time frame we had.

Image 2: Closeup of the original pcb

Alternative

What’s every tinkerers’ first idea when faced with an unknown component that cannot be analyzed in detail? Why not build it myself?! After a quick online search we found a commonly mentioned alternative to built-in sensors – the so called HX711.

Image 3: HX711 pcb. Image taken from https://potentiallabs.com/cart/hx711-weighing-sensor-module-india

The HX711 is a 24-bit analog-to-digital converter (ADC), that (coupled with a few other electronic components) amplifies the signals from weight sensors. We purchased a pcb with the chip on it on Wednesday (similar to the one in Image 3), and had it delivered directly to our room on Thursday. With a heavy heart we decided to remove the original pcb to make room for our own circuit board. The process can be seen in Image 4 and 5.

Image 4: Cutting off the wires to the original pcb

Image 5: All wires to the original pcb have been cut off

Setup

The setup looks relatively simple, but has quite the ingenious concept behind it. Have you ever heard of a Wheatstone bridge? If you haven’t, check out its Wikipedia article for a quick summary. What makes it so cool is the following:

When putting weight on a weight sensor, it creates positive (white cable in Image 6) and negative (black cable in Image 6) strain. When set up the way it can be seen in Image 6, all four sensors can be used without extra resistors, because the strains oppose each other. Due to the way the Wheatstone bridge works, the weight sensors are wired in a ring/diamond, and the diagonally-opposite sides are each working together, as is shown in Image 7. For a more detailed explanation of the physical concept behind it, check out the Wikipedia article again, or read through this really informative wiki entry.

Image 6: Setup of the four load cells and the HX711-pcb, taken from https://www.instructables.com/id/Arduino-Bathroom-Scale-With-50-Kg-Load-Cells-and-H/

Image 7: Simple Wheatbridge drawing

But even though we have twelve cables coming from four weight sensors, only four of those need to be connected to the HX711. The other ones are connected to one another.

Now that we have used eight of the twelve cables from the weight cells, the four remaining ones need to be connected to the HX711-pcb. Simply follow the diagram, and all that’s left to do is to connect the Arduino to the HX711-pcb. The top pin is ground, while the bottom one is +5V. The two pins in the middle of the HX711-pcb need to be connected to two GPIO-pins on the Arduino of your choice. In our case, we picked GPIO-pins 50 & 52, as we had an Arduino Mega at our disposal. That’s it, you are ready to power it up!

Calibration

Now you probably want to simply place something on the scale, and read the correct weight from HX711, correct? But not so fast!

Image 8: taken from https://iamcalledtobe.wordpress.com/2013/06/03/patience-young-padawan-its-a-virtue/

First, you need to convert the signal you read to a value you can work with. We used bodge’s HX711 library, which can be found on GitHub. Now you can read values from the weight sensors, though they won’t make too much sense. The scale doesn’t know yet what unit you’re measuring in. Would you like your weight in kilograms, grams or rather in pounds? That’s why you need to calibrate the scale first. You can use the sparkfun calibration example to help you with that.

Remove all weight from the scale, connect the Arduino, and power it up. Include the HX711 library, create a new scale and call set_scale() and tare() in the setup()-method.

void setup() {
  HX711 scale(DOUT, CLK);
  scale.set_scale();
  scale.tare();
}

tare() resets the current weight to zero. This can be useful in case you want to weigh the contents of a container, without weighing the container itself. However, as you removed all weight before, the scale should be set to zero now. Now we get to the actual calibration. Create a variable called calibration_factor, and set it to zero, or a value of your choice.

float calibration_factor = 0;

In the loop()-method, call the set_scale()-method with the calibration_factor variable as parameter. Afterwards, call get_units(), and print its result. I call print() with a two as second parameter, as I only need two decimals. If you need more or less, adjust the value accordingly.

void loop() {
  scale.set_scale(calibration_factor);
  float weight = scale.get_units();
  Serial.print(weight, 2);
}

Upload the code to the Arduino. Once the loop()-function has been reached (the printed weight should be zero), place an object with a known weight on the scale.

If you don’t have anything handy, using a water bottle is an easy alternative where you can approximate the weight closely enough. Of course, using something like a 1kg weight will give you more accurate results later on. The scale will now likely report something completely different from what you want, but this is where the calibration_factor plays its role. The example code thankfully makes it easy to adjust, simply send + to increase or to decrease the calibration factor to the Arduino. Repeat this until it reports the weight you’re looking for. Finding the correct value might take some time, but needs to be done only once. For example, our calibration factor was -19000.

You can now use the scale by setting the calibration-factor you found in the setup()-method with

scale.set_scale(calibration_factor);

In the loop()-method, call

float weight = scale.get_units();

and you can finally measure whatever it is you’ve built the scale for!

Problems

This looked easy enough, but there was one thing that was giving us headaches – the wiring.

We have three cables coming from each load-cell (a total of twelve), and needed to connect eight of them to each other. As we were still in the testing phase, and didn’t want to waste too much time in case we did something wrong, we figured using a breadboard would be the easiest solution for us. We didn’t really think of all the other wiring we had to do. The remaining four wires from the load-cells needed to be connected to the HX711-pcb, and from the HX711-pcb another four wires were going to the Arduino. Eleven wires were needed to connect the three-digit seven-segment-display, another three for the LED-strips, and another four for the stepper-motor. That’s a total of thirty-four wires, and a complete mess, which you can see in Image 11. Originally, each three-wire-pair of the load-cells was neatly connected to the pcb, but in our case the wires were everywhere. This had negative effects on our accuracy of the weight sensors. Whenever we accidentally moved the Arduino or touched the wires, our results varied greatly, and went from a normal measurement to negative numbers or adding up to 50kg. A possible solution to this problem would be to shorten the wire connections to an absolute minimum, or to simply solder the wires together instead of using jumper-cables like we did. Using lanes as the original scale did, to make sure the wires don’t interfere with each other, would have greatly helped as well. Using a separate breadboard could also have improved the situation.

Image 11: Wires – a bit of a mess

Summary

We’ve looked at how load-cells work, how you need to connect them and how to read their data using the HX711-pcb. We learned how to calibrate a working scale, and how to measure weight after calibration. We’ve seen what kind of problems ugly wiring can produce, and looked at different ways to avoid them.

Congratulations, you’ve made it! Go ahead and buy a cheap scale, grab an HX711-pcb and get to work. And don’t forget to have fun!

linked categories 2018b, Tutorials

|

Write a comment

Comment

[…] see Jan’s blog post for a more detailed […]