Kitchen scale on an Arduino Uno
A bar load cell on an HX711, read with the HX711 library: tare, a scale in counts per gram, and grams on the serial monitor. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
Step by step, with the wiring and the common mistakes: Arduino HX711 Load Cell: Calibration and Code
What you'll learn
- Why a load cell needs an amplifier and converter like the HX711
- What taring does: subtract the empty reading
- A scale factor in counts per gram
- Averaging readings to cut noise
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
A load cell makes a tiny signal, about a millivolt at full load, far too small for the Uno's own ADC. The HX711 chip is a 24-bit converter made for the job. It sends its readings on two wires: DT on pin 3 and SCK on pin 2.
At startup the sketch prints the raw reading, then sets a scale of 429.5 counts per gram and tares ten times. Taring averages the empty reading and calls it zero.
After that, loop prints the weight in grams with one decimal. Each line averages five readings, which keeps the last digit from wandering.
Parts list
3 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Load cell with HX711
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Load cell with HX711 pin DT | Arduino Uno R3 pin 3 |
| Load cell with HX711 pin SCK | Arduino Uno R3 pin 2 |
| Load cell with HX711 pin VCC | Arduino Uno R3 pin 5V |
| Load cell with HX711 pin GND | Arduino Uno R3 pin GND |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Kitchen scale: a bar load cell on an HX711, weighed in grams.
//
// pin 3 the HX711's DT pin 2 its SCK
// VCC -> 5 V, GND -> GND the cell's four wires are on the far side
//
// A load cell's whole signal is about a millivolt at full load, which no
// Arduino ADC can see. The HX711 is a 24-bit converter made for it, with its
// own two-wire output: wait for DT to go low, clock 24 bits out on SCK, then
// one more pulse to ask for channel A at gain 128 again. HX711.h does that,
// and this sketch is the tutorial every kit prints.
//
// # Three numbers, and the order they come in
//
// * The raw reading with nothing on it is not zero. No real cell is
// balanced, and this one's zero is about 43 000 counts. So the first thing
// the sketch does is tare(): read ten times, and call the average zero.
// * set_scale() is counts per gram. This cell is 1 mV/V at 5 kg, which at
// gain 128 is 2 x 128 x 2^23 x 1 mV/V / 5000 g = 429.5 counts a gram. On a
// real cell you find it with a known weight: read_average(10) with and
// without it, and divide the difference by its grams.
// * get_units(5) is the average of five readings, less the tare, over the
// scale. At ten readings a second that is half a second a line.
//
// # The last digit wanders
//
// The HX711's input noise is about 50 nV, some twenty counts at gain 128:
// a twentieth of a gram. Drag the load and watch the tenths settle, then
// change get_units(5) to get_units(1) and watch them wander.
//
// # Floats you can print, and nothing more
//
// This board has no floating point. get_units() is a float built bit by bit
// from whole numbers, so it prints; doing sums on it would not link. The
// scale is taken to a hundredth, which is far finer than the cell.
#include "HX711.h"
const int DT_PIN = 3;
const int SCK_PIN = 2;
HX711 scale;
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: kitchen scale");
// begin() waits for the chip's first settled reading, four conversions.
scale.begin(DT_PIN, SCK_PIN);
Serial.print("raw with nothing on: ");
Serial.println(scale.read_average(5));
scale.set_scale(429.5);
scale.tare(10);
Serial.println("tared: drag the slider to put a weight on the cell");
}
void loop() {
Serial.print(scale.get_units(5), 1);
Serial.println(" g");
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts the HX711 with scale.begin on pins 3 and 2, and prints the raw reading with nothing on the cell. Then set_scale(429.5) sets counts per gram and tare(10) averages ten readings to make zero. After that, you can put a weight on.
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: kitchen scale");
// begin() waits for the chip's first settled reading, four conversions.
scale.begin(DT_PIN, SCK_PIN);
Serial.print("raw with nothing on: ");
Serial.println(scale.read_average(5));
scale.set_scale(429.5);
scale.tare(10);
Serial.println("tared: drag the slider to put a weight on the cell");
}loop sketch.ino
Prints scale.get_units(5) with one decimal and the unit g. That call averages five readings, subtracts the tare and divides by the scale. There is no delay, because reading the chip takes time.
void loop() {
Serial.print(scale.get_units(5), 1);
Serial.println(" g");
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The weight is off by a fixed amount. This project
Tare with nothing on the cell. Anything on it at startup becomes zero. - Readings look wrong by a constant factor. This project
Retuneset_scale. Read with and without a known weight and divide the difference by its grams. - Nothing prints after the title. This project
Check DT on pin 3, SCK on pin 2, and power and ground on the module. - The reading is zero, noisy, or drifts. Load cell with HX711
Check wiring: E+ and E- to the excitation, A+ and A- to the signal wires. Swapping A+ and A- makes weight read negative. Keep wires short, and let it settle before taring. - The grams value is wrong. Load cell with HX711
Call tare with nothing on the scale, then calibrate with a known weight to get the scale factor. Each load cell needs its own factor. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.