ESP32 is an IoT-based smart microcontroller board. With its advanced built-in features like Bluetooth and Wi-Fi, it can also interface several peripherals for reading real-time values. ESP32 has several different digital and analog pins that can read real-time values and display them on screen.

One example is designing an ESP32-based weight sensor using the HX711 amplifier. With these sensors, you can measure the weight of any object depending on the weight sensor capacity. Let’s cover all the steps involved in interfacing both these sensors with ESP32.

1. What is a Load Cell (Weight Sensor) and HX711 Amplifier

A load cell or weight sensor can convert the force that you apply to a detectable electrical signal. This signal can be read by the microcontroller boards. It’s like a sensor that tells us the applied force. Strain gauge load cells are commonly used. They include a metal bar attached with different string gauges. Once outside force is applied, strain gauges change resistance. By measuring this resistance change, we can calculate the weight of the object.

The HX711 is a breakout board that works with the load cells. It amplifies the tiny electrical signals from the load cells. This makes it easier for the microcontroller board like ESP32 to read those signals. You can connect the load wires to one side of the HX711 and the microcontroller to the other side. The HX711 exchanges data with the ESP32 or any microcontroller using a two-wire interface (Clock and Data). 

2. How to Interface Weight Sensor with ESP32

To interface ESP32 with the weight sensor, first you have to install the required library. Once it’s done, the next step is to connect your ESP32 board with a weight sensor via the HX711 module. After connecting them you have to calibrate the load cell with the help of a known weight, after that, you can measure any weight of your choice once the calibration is done.

Let’s cover all the steps of interfacing load cells with ESP32.

2.1. Installing the Required Libraries

To interface ESP32 with the load cell, you are going to need the HX711 Arduino library by Bogdan Necula. You can directly install it from the Arduino IDE library manager.

2.2. Circuit Diagram

After installing the library, the next step is to wire the HX711 and load the cell with ESP32. For this, follow the below wiring configuration. To communicate with HX711, you will need two wires, Clock and Data. Here I have used the D12 as a clock pin and D13 as a data pin. You can also use any other GPIO pins for both of these.

For powering HX711, you can either use the VIN or the 3.3 V pin of ESP32.

For interfacing HX711 with ESP32, the following pins can be used:

HX711 PinsESP32 Pins
GNDGND
DTGPIO 13
SCKGPIO 12
VCC3.3?V/VIN

2.3. Hardware

After you have wired your load cell with ESP32 using the HX711 sensor, the circuit will look like this. Here I have designed a DIY weight sensor, but you can 3D print a complete kit for your ESP32-based weight measuring system. You can also consider using any old weight balance kit that you no longer use.

3. Calibrating the Load Cell

Once your circuit is ready now you have to configure the load cell. For that, you have to place a known weight on the weight cell. After that, you have to divide the sensor reading by the weight placed. You will get the scale factor.

3.1. Code

Open Arduino IDE, connect the ESP32 board, and burn this code to your board.

#include "HX711.h"
const int LOADCELL_DOUT_PIN = 13;const int LOADCELL_SCK_PIN = 12;
HX711 scale;
void setup() {  Serial.begin(115200);  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);  scale.tare();  // reset scale}
void loop() {  if (scale.is_ready()) {    Serial.print("HX711 reading: ");    Serial.println(scale.get_value(5)); // Display 5 readings average after subtracting the tare weight from ADC  } else {    Serial.println("HX711 not found.");  }  delay(500);  }

3.2. Output

After uploading the code, place a known weight on your load cell. For reference here, I have placed my mobile phone, which weighs roughly 220 grams with the case.

Now note down the sensor reading and divide it with your weight placed.

Save this scale factor, as you are going to use this in the next code.

4. How to Measure Actual Weight Using Load Sensor

Now as we get the calibrated scale factor, we can just paste it inside the below-given code and get the weight of any object.

4.1. Code

Upload the given code to the ESP32 board after replacing the scale.set_scale() value with the one you got in the previous step.

#include "HX711.h"
const int LOADCELL_DOUT_PIN = 13;const int LOADCELL_SCK_PIN = 12;
HX711 scale;
void setup() {  Serial.begin(115200);  Serial.println("Load Cell Interfacing with ESP32 - DIY CHEAP PERFECT");    scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(230);    //scale factor input here  scale.tare();       // reset scale}
void loop() {  Serial.print("Weight: ");  Serial.println(scale.get_units(10), 1);  scale.power_down(); // set ADC to sleep mode  delay(1000);  scale.power_up();}

Code Explanation

The code begins by including the HX711.h library, which provides functions for interfacing with the HX711 load cell sensor. It defines two constants: LOADCELL_DOUT_PIN (connected to pin 13) and LOADCELL_SCK_PIN (connected to pin 12). Both these pins will help ESP32 to communicate with the HX711 sensor.

Next, the scale factor (calibration value) is set to 230. You will get this value after calibrating the load cell with known weights. The scale is tared (zeroed) to account for any initial offset.

In the loop() function, weight measurements are continuously read from the load cell using scale.get_units(10). The weight value is printed on the serial monitor. The ADC of the HX711 is put in sleep mode for 1 second. The HX711 is then powered up again after the delay.

4.2. Output

In output, you will see the actual weight of the object placed.

The output accuracy depends upon different factors. Like the standing base you used for the load cell, and how close and accurate the scale factors have you got. Also, how good the electrical connection of the load cell is.

Conclusion

ESP32 is a smart IoT microcontroller board that can interface with different peripherals and sensors to read real-world readings. This article explained how you can integrate the load cell with the ESP32 and HX711 sensors. Using both these sensors you can measure any weight of an object over the two wires Clock and Data. Before measuring any object weight, first calibrate the load cell with ESP32. After that, you can take any reading with these sensors. Read further about HX711 and load cells in this article.