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.
IntermediateRuns in your browser. Free, and no account needed.
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");
}
Parts list
3 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 3; Load cell with HX711 pin DT
- Arduino Uno R3 pin 2; Load cell with HX711 pin SCK
- Arduino Uno R3 pin 5V; Load cell with HX711 pin VCC
- Ground: Arduino Uno R3 pin GND; Load cell with HX711 pin 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.