Digital scale
Weigh things with a load cell and an HX711 amplifier: tare it, scale it, read grams.
Sensors · Intermediate
In a real product: Kitchen scale (Consumer electronics): A load cell and a 24-bit converter weigh to the gram.
A load cell is a metal bar with strain gauges that change resistance very slightly as it bends under a weight. The HX711 is a 24-bit ADC made to measure that tiny change, and it sends the result over a simple clocked serial line.
What you'll learn
- Reading a 24-bit sensor over a clock (SCK) and data (DT) line
- Taring (zeroing) a scale and calibrating it with a scale factor
- Averaging readings with the HX711 library
How it works
DT goes to pin 3 and SCK to pin 2. When a reading is ready the HX711 pulls DT low; the board then sends 24 clock pulses and reads one bit on each. The HX711 library does that for you.
scale.tare() reads the empty scale and stores it as zero. set_scale(0.42) sets how many raw counts make a gram for this load cell, so get_units(5) returns grams, averaged over five readings. On real hardware you find the scale factor by weighing something you know.
Try this
- Once it has started, set the load cell's weight slider and watch the reading.
- Add a button that tares the scale again.
- Show the weight on an LCD, and light an LED over 1 kg.
sketch.ino
// A load cell through an HX711: DT on pin 3, SCK on pin 2. Prints the weight in grams.
#include <HX711.h>
HX711 scale;
void setup() {
Serial.begin(9600);
scale.begin(3, 2); // DT, SCK
scale.set_scale(0.42); // raw counts per gram for this load cell
scale.tare(); // the empty scale reads 0
Serial.println("Scale ready: put a weight on the load cell");
}
void loop() {
float grams = scale.get_units(5); // average of 5 readings
Serial.print("Weight: ");
Serial.print(grams, 0);
Serial.println(" g");
delay(500);
}