How to build an Automatic Weighing Scale with Label Printer???
In this tutorial, I will explain the step-by-step process to build your own Weighing Scale with a Label Printer using ESP32. The project is simple and easy to build. You only need a few components – ESP32, Loadcell, HX711 LoadCell Module, and thermal printer.
What is the excepted Output???
A Wireless Bluetooth Printer prints out the measured weight on a thermal label.
The ESP32 continuously monitors the weight on a load cell, when it detects weight on the load cell, it sends instructions to print the measured weight on a thermal label.
This project is great to understand the working of a loadcell and wireless Bluetooth printers.
Connections:
The loadcell cannot be directly connected to an ESP32. The signals from the loadcell have to be amplified with noise filtering. This is done by the HX711 load cell amplifier board.
We have used a wireless Bluetooth printer in this project which communicates with the ESP32 through Bluetooth. This simplifies wiring and gives us portability.
Follow this link for detailed connections. Connections are the same as per the previous article and also for setting up the printer with the ESP32.
Code:
Step1: Download and install the library from this link. We will use this super easy Adafruit-Thermal-Printer-Library. It is a great easy to use by quite a powerful library that will let us communicate with the printer. We will also require the HX711 library from this link.
#include "HX711.h"
#include "Adafruit_Thermal.h"
#include "BluetoothSerial.h"
#include <Wire.h>
Step2: Setting the parameters for connecting loadcell and printer to ESP32.
#define minWeightToPrint 0.005F
float weight;
BluetoothSerial SerialBT;
Adafruit_Thermal printer(&SerialBT);
#define calibration_factor 681880 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define DOUT 23
#define CLK 19
HX711 scale;
uint8_t address[6] = {0x02, 0x3E, 0x9F, 0xEF, 0xFA, 0xBC};
String name1 = "P58D";
char *pin = "0000";
bool connected1;
bool printWeight = false;
Step3: Initialize the variables in the setup function required.
void setup()
{
Serial.begin(115200);
Serial.println("Weighing Scale with Printer");
scale.begin(DOUT, CLK);
scale.set_scale(calibration_factor);
scale.tare();
Serial.println("Readings:");
connectBTPrinter();
}
Step4: This function is used to connect to the printer.
void connectBTPrinter()
{
Serial.println("Connecting to the Bluetooth Printer");
SerialBT.setPin(pin);
SerialBT.begin("ESP32test", true);
connected1 = SerialBT.connect(name1);
if(connected1)
Serial.println("Connected Succesfully!");
else
while(!SerialBT.connected(10000))
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
printer.begin();
}
Step5: This function is used to send data to the printer which includes alignment properties and values to be printed.
void printWeightValues(float w)
{
Serial.println("Sending Data to printer");
printer.setSize('L');
printer.justify('C');
printer.print("Weight: ");
printer.print(w, 3);
printer.println(" kg");
printer.setSize('M');
printer.println("\nwww.probots.co.in\n\n\n");
}
Step6: The loop part in this code explains the logic of what happens when there is no weight or if I suddenly take off the weight what will happen.
void loop()
{
float prevWeight = weight;
weight = scale.get_units(3);
Serial.print("Reading: ");
Serial.print(weight, 3); //scale.get_units() returns a float
Serial.print(" kg");
Serial.println();
if(weight > minWeightToPrint && printWeight == false)
{
printWeight = true;
delay(1000);
weight = scale.get_units(3);
Serial.print("Printing Weight: ");
Serial.print(weight, 3);
printWeightValues(weight);
}
else if(weight < minWeightToPrint)
printWeight = false;
}
Tested out on different platforms:
awsome