Help Center
< All Topics
Print

Teensy 4.1 HX711 Load Cell Tutorial

Tutorial Aim:

In this tutorial, we will create a digital scale using a Teensy microcontroller, a load cell, and the HX711 amplifier. Our goal is to build and calibrate the scale. Then, we'll proceed to measure the weight of objects and calculate the average weight from multiple readings.

Introduction:

This straight bar load cell, sometimes also called a strain gauge, is like a translator for pressure or force. As the changes in strain when weighting objects is so small, we'll need an amplifier such as the HX711 amplifier.  These gauges are commonly incorporated into applications requiring mass calculation and tracking changes in weight over time. 

Key Concepts:
  1. How A Strain Gauge Works: A strain gauge measures the electrical resistance when the bar is pushed or pulled. The resistance of the strain gauges varies when an external force is applied to an object, which results in a deformation of the object’s shape (in this case, the metal bar). The strain gauge resistance is proportional to the load applied, which allows us to calculate the weight of objects. Calibrating against a known source is recommended.
  2.  Setting Up The Scale: Placing plates on a spacer connected to the strain gauge on opposite ends pushes the load cell when a weight is placed on top. From this, we calculate the objects weight using electrical resistance & amplifier.
  3. Calibrating The Scale: Determining the calibration factor so that all weight measurements are accurate. 

Please note, this tutorial continues from the "Getting Started With Teensy And The Arduino IDE" tutorial. It is recommended to follow this tutorial to familiarize yourself with the Arduino IDE and Teensy microcontroller.

Requirements:

  • Arduino IDE v.2.21 or later
  • Teensy 4.0 or 4.1 microcontroller
  • Load Cell and HX711 Amplifier Module
  • 8 Male to Male Jumper Wires
  • A Breadboard

Pin Layout:

The pin layout between the Teensy and the HX711 Amplifier is shown in the table below.

Note: You can use a Teensy 4.1 or Teensy 4.0 microcontroller for this tutorial with no changes to the pinout.

Sensor PinTeensy Pin
GNDGND
VCC3.3V
DT26
SCK27
click to expand

Setup:

Note: This mechanical assembly of the load cell is optional.

Please refer to image below for assembling the load cell and plates. Please note that these files are available in the Downloadable Content section of this tutorial.

  • Place 2 spacers on the load cell, one on either side of the cell and on opposite ends, and then place a plate on top of the spacers.
Click to exapnd
  • Connect the load cell’s SCK pin to pin 27 & SCL pin to pin 26 on the Teensy 4.1 using the Male to Male jumper wires. 
  • Open the Arduino IDE
  • Connect the Teensy to the computer via USB
click to expand

Working Principle

As shown below, the load cell is fixed at 1 point (the side with cables) and the other side is left to overhang. This will ensure that the the load cell will undergo deflection/strain and the amplifier will feed signal data to the controller.

Click to expand

Code Walk Through:

Code Integration:

There are two pieces of code that will need to be uploaded to the Teensy Microcontroller separately. The first piece of code will be for the calibration of the scale [Load Cell Calibration Code]. Note that there may be slight differences between the load cells so it is recommended to calibrate them for accuracy. 

After finding the load cell calibration, we will use this calibration factor in the main code [Digital Scale Full Code].

Libraries:

While there are multiple libraries to get measurements from a load cell, we’ll use the HX711.h library by Rob Tilaart. For the calibration a we need a known weight, ours is 500 grams, please update with your own weight.

For information about the HX711.h library by Rob Tilaart please visit his Github page.

Load Cell Calibration Code Explanation:

Initializing The Sensor:

The sensor & serial monitor are initialized so that the output can be displayed.

/* Init teensy & load cell */
void setup() {
  Serial.begin(9600);
  scale.begin(DT, SCK);
}
HX711 scale;
Calibrating The Sensor:

We set & tare the sensor before the known weight is weighed for accurate readings, and then calculate the calibration factor using:

Calibration Factor = reading / known weight

Because the output of the sensor is proportional to the force applied to the load cell, you can calibrate your scale using whatever unit makes sense for you.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

/* Calculating the calibration factor
  when scale input is available */
void loop() {
  // Checking if there is any scale inputS
  if (scale.is_ready()) {
    // Set & tare the scale
    scale.set_scale();
    Serial.println("Tare... remove any weights from the scale.");
    delay(5000);
    scale.tare();
    Serial.println("Tare done...");

 

    // Collecting & displaying the known weight input
    Serial.print("Place a known weight on the scale...");
    delay(5000);
    long reading = scale.get_units(10);
    Serial.print("Result: ");
    Serial.println(reading);

 

    // Calculating & displaying the calibration factor
    Serial.print("Calibration factor: ");
    Serial.println(reading / KNOWN_WEIGHT);
  }
  else {
    Serial.println("HX711 not found.");
  }
  delay(1000);
}
Serial Output:

The reading & calibration factor is displayed on the serial monitor. Take note of this Calibration factor figure for use in the next stage of the programming.

Click to expand

Load Cell Calibration Full Code:

// AUTHOR: VBG of MakerBotics
// VERSION: 1
//    DATE: 2023/11/28
// PURPOSE: Using a HX711 and Load Cell as a digital scale
//     License details:
//     Attribution-ShareAlike 4.0 International
//     CC BY-SA 4.0 Deed.
#include "HX711.h"
// Load cell's SCK & DT pins
#define SCK 27
#define DT 26
// Preset known weight
// Please update to match your known weight
#define KNOWN_WEIGHT 500.0
HX711 scale;
/* Init teensy & load cell */
void setup() {
  Serial.begin(9600);
  scale.begin(DT, SCK);
}
/* Calculating the calibration factor
  when scale input is available */
void loop() {
  // Checking if there is any scale inputS
  if (scale.is_ready()) {
    // Set & tare the scale
    scale.set_scale();
    Serial.println("Tare... remove any weights from the scale.");
    delay(5000);
    scale.tare();
    Serial.println("Tare done...");
    // Collecting & displaying the known weight input
    Serial.print("Place a known weight on the scale...");
    delay(5000);
    long reading = scale.get_units(10);
    Serial.print("Result: ");
    Serial.println(reading);
    // Calculating & displaying the calibration factor
    Serial.print("Calibration factor: ");
    Serial.println(reading / KNOWN_WEIGHT);
  }
  else {
    Serial.println("HX711 not found.");
  }
  delay(1000);
}

Using The Sensor As A Digital Weight:

Initializing The Sensor:

The sensor & serial monitor are initialized so that the output can be displayed.

/* Initialising the teensy & load cell */
void setup() {
  Serial.begin(9600);
  scale.begin(DT, SCK);
  // Calibration values
  scale.set_offset(4294799235);
  scale.set_scale(CALIBRATION_FACTOR);
  // Set the scale to 0
  scale.tare();
  Serial.println("Please place weight on scales");
}
Gathering Weight Readings:

We first check if there is an object on the scale, then call get_units() to get the weight readings before displaying them on the serial monitor.

/* Calculating & displaying the weight & average weight readings */
void loop() {
  // Reading scale input
  float weight = scale.get_units();

  // Checking for valid weight and updating the average
  if (weight > 1) {
    Serial.printf("reading %d: ", count++);
    Serial.println(scale.get_units(), 3);
    Serial.println("Please place another weight on scales");
  }
  delay(2000);
}

Digital Scale Full Code:

// AUTHOR: VBG of MakerBotics
// VERSION: 1
//    DATE: 2023/11/28
// PURPOSE: Using a HX711 and Load Cell as a digital scale
//     License details:
//     Attribution-ShareAlike 4.0 International
//     CC BY-SA 4.0 Deed.
#include "HX711.h"
#define SCK 27
#define DT 26
#define CALIBRATION_FACTOR -228.4

 

HX711 scale;
// Scale weight reading counter
int count = 1;

 

/* Initialising the teensy & load cell */
void setup() {
  Serial.begin(9600);
  scale.begin(DT, SCK);
  // Calibration values
  scale.set_offset(4294799235);
  scale.set_scale(CALIBRATION_FACTOR);
  // Set the scale to 0
  scale.tare();
  Serial.println("Please place weight on scales");
}

 

/* Calculating & displaying the weight & average weight readings */
void loop() {
  // Reading scale input
  float weight = scale.get_units();

 

  // Checking for valid weight and updating the average
  if (weight > 1) {
    Serial.printf("reading %d: ", count++);
    Serial.println(scale.get_units(), 3);
    Serial.println("Please place another weight on scales");
  }
  delay(2000);
}

Results

Serial Output:

The weight is displayed on the serial monitor. Note that the weight is in grams

Click to expand

Downloadable Content:

Please find this tutorial's code and the files for the dxf plates for the project on our GitHub page.

Credits:

Table of Contents