vinay y.n
Published © GPL3+

XIAO ESP32C3 Portable Weighing Scale, Battery level Indicator

XIAO ESP32C3 Weigh-n-Charge: Portable scale for weight measurement with a built-in battery indicator, ideal for on-the-go use.

BeginnerFull instructions providedOver 1 day733
XIAO ESP32C3 Portable Weighing Scale, Battery level Indicator

Things used in this project

Hardware components

Seeed Studio XIAO ESP32C3
×1
Monochrome 0.91”128x32 I2C OLED Display with Chip Pad
DFRobot Monochrome 0.91”128x32 I2C OLED Display with Chip Pad
×1
Texas Instruments bq27441
×1
SparkFun Load Cell Amplifier - HX711
SparkFun Load Cell Amplifier - HX711
×1
HX711
×1
Adafruit STEMMA QT
×1
Micro slide switch
×1
Right Angle Tactile Button
×1
Polymer Lithium Ion Battery - 2200mAh 3.7V
Seeed Studio Polymer Lithium Ion Battery - 2200mAh 3.7V
×1

Software apps and online services

Arduino IDE
Arduino IDE
KiCad
KiCad
seeed studio PCB/PCBA Services

Hand tools and fabrication machines

Hot Air Station, Industrial
Hot Air Station, Industrial
Solder Wire, Lead Free
Solder Wire, Lead Free
Soldering iron (generic)
Soldering iron (generic)
Solder Flux, Soldering
Solder Flux, Soldering

Story

Read more

Custom parts and enclosures

3D Images

3D Images

Schematics

Schematic Diagram

Code

Weight Measuring code

Arduino
Before Compiling, make sure the required Libraries are added.
#include <SparkFunBQ27441.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "HX711.h"
#define calibration_factor 402.1 //sensor2
#define LOADCELL_DOUT_PIN  D0
#define LOADCELL_SCK_PIN  D1
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
int led = D10;
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4// Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Set BATTERY_CAPACITY to the design capacity of your battery.
const unsigned int BATTERY_CAPACITY = 500; // e.g. 850mAh battery
HX711 scale;


void setupBQ27441(void)
{
  lipo.begin();
  // Use lipo.begin() to initialize the BQ27441-G1A and confirm that it's
  // connected and communicating.
  if (!lipo.begin()) // begin() will return true if communication is successful
  {
    // If communication fails, print an error message and loop forever.
    Serial.println("Error: Unable to communicate with BQ27441.");
    Serial.println("  Check wiring and try again.");
    Serial.println("  (Battery must be plugged into Battery Babysitter!)");
    while (1) ;
  }

  Serial.println("Connected to BQ27441!");
  lipo.setCapacity(BATTERY_CAPACITY);
}

void printBatteryStats()
{
  // Read battery stats from the BQ27441-G1A
  unsigned int soc = lipo.soc();  // Read state-of-charge (%)
  unsigned int volts = lipo.voltage(); // Read battery voltage (mV)
  int current = lipo.current(AVG); // Read average current (mA)
  unsigned int fullCapacity = lipo.capacity(FULL); // Read full capacity (mAh)
  unsigned int capacity = lipo.capacity(REMAIN); // Read remaining capacity (mAh)
  int power = lipo.power(); // Read average power draw (mW)
  int health = lipo.soh(); // Read state-of-health (%)

  // Now print out those values:
  String toPrint = String(soc) + "% | ";
  toPrint += String(volts) + " mV | ";
  toPrint += String(current) + " mA | ";
  toPrint += String(capacity) + " / ";
  toPrint += String(fullCapacity) + " mAh | ";
  toPrint += String(power) + " mW | ";
  toPrint += String(health) + "%";

  display.clearDisplay();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0, 15);
  display.print("Battery Level:");
  display.print(soc);
  display.print("% ");
  display.setCursor(0, 25);
  display.print("Battery Volt:");
  display.print(volts);
  display.print("mV");

  display.setCursor(30, 0);  
  display.print("Weight:");// Start at top-left corner
  display.print(scale.get_units(), 1);
  display.print(" g");
  display.display();      // Show initial text
  display.display();      // Show initial text
  Serial.println(toPrint);
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1);//scale.get_units() returns a float
  Serial.print(" Grams"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();

  
}

void setup()
{
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  setupBQ27441();
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  Serial.println("Readings:");
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  display.display();
  delay(2000); // Pause for 2 seconds
  display.clearDisplay();
}

void loop()
{
  printBatteryStats();
}

Calibrating the load cell

Arduino
Calibrating the load cell
// Calibrating the load cell
#include "HX711.h"

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = D0;
const int LOADCELL_SCK_PIN = D1;

HX711 scale;

void setup() {
  Serial.begin(9600);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
}

void loop() {

  if (scale.is_ready()) {
    scale.set_scale();    
    Serial.println("Tare... remove any weights from the scale.");
    delay(5000);
    scale.tare();
    Serial.println("Tare done...");
    Serial.print("Place a known weight on the scale...");
    delay(5000);
    long reading = scale.get_units(10);
    Serial.print("Result: ");
    Serial.println(reading);
  } 
  else {
    Serial.println("HX711 not found.");
  }
  delay(1000);
}

//calibration factor will be the (reading)/(known weight)

Credits

vinay y.n

vinay y.n

26 projects • 44 followers
An electronic product engineer with 8 years of experience in the field. The passion for electronics began as a hobby 11 years ago.

Comments