Christopher Mendez Martinez
Published © GPL3+

DIY Arduino Smart Digital Scale | ESP8266 + HX711

Digital Scale capable to measure weight precisely in the spectrum of grams with Arduino

IntermediateFull instructions provided4 hours1,879
DIY Arduino Smart Digital Scale | ESP8266 + HX711

Things used in this project

Hardware components

JLCPCB Customized PCB
JLCPCB Customized PCB
×1
SparkFun Load Cell Amplifier - HX711
SparkFun Load Cell Amplifier - HX711
×1
ESP8266 ESP-12E
Espressif ESP8266 ESP-12E
×1
Gravity I2C OLED-2864 Display
DFRobot Gravity I2C OLED-2864 Display
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)
Soldering iron (generic)
Soldering iron (generic)
Mini Side Cutter, 120mm Length with 25mm Jaw Capacity
Mini Side Cutter, 120mm Length with 25mm Jaw Capacity
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Custom parts and enclosures

Base.stl

Cuerpo.stl

Schematics

Circuit Diagram

Code

Project Code

Processing
//Author: Christopher Mendez (Industrial Electronics Engineering Degree)
//Tutorial: https://youtu.be/17FfC5F5Mtw

#include "HX711.h"

//Para la comunicación I2C
#include <Wire.h>

#include <EEPROM.h>

//Para la pantalla
#include <U8g2lib.h>
#include <string.h>


U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /* reset=*/ U8X8_PIN_NONE, 2, 4); // SCL, SDA


/*U8G2_R0  sin rotación
  U8G2_R1 90 grados (sentido del reloj)
  U8G2_R2 180 grados (sentido del reloj)
  U8G2_R3 270 grados (sentido del reloj)
  U8G2_MIRROR Sin rotación pero el contenido en modo espejo
*/


// Parámetro para calibrar el peso y el sensor
float CALIBRACION; // = 475910.00 iniciar con este valor o ir tanteando hasta obtener valores iniciales muy cercanos al objeto de referencia para que la calibración sea rapida
int addr = 0;

// Pin de datos y de reloj
byte pinData = 4;
byte pinClk = 5;

const byte led = 16; //LED en la placa para uso personal
const byte boton1 = 14; //Pulsador que puedes usar para lo que quieras XD
const byte boton2 = 13; //Pulsador para la calibración
const byte boton3 = 12; //Pulsador para cambiar la unidad de Kg a Lb
const byte boton4 = 15; //Pulsador para setear el cero.

bool unidad = 0;

float peso;

// Objeto HX711
HX711 bascula;

/****************************************

   Funciones auxiliares

 ****************************************/

 //Funcion para escribir en la EEPROM
void eeWriteInt(int pos, int val) {
    byte* p = (byte*) &val;
    EEPROM.write(pos, *p);
    EEPROM.write(pos + 1, *(p + 1));
    EEPROM.write(pos + 2, *(p + 2));
    EEPROM.write(pos + 3, *(p + 3));
    EEPROM.commit();
}

//Funcion para leer de la EEPROM
int eeGetInt(int pos) {
  int val;
  byte* p = (byte*) &val;
  *p        = EEPROM.read(pos);
  *(p + 1)  = EEPROM.read(pos + 1);
  *(p + 2)  = EEPROM.read(pos + 2);
  *(p + 3)  = EEPROM.read(pos + 3);
  return val;
}


// Funcion que presenta los datos en pantalla
void pantalla(String x, String unit) {
  u8g2.clearBuffer();                    // limpiar los datos de la pantalla
  u8g2.setCursor(60 - (x.length() + unit.length()) * 7.8 / 2, 40);
  Serial.println((x.length() + unit.length()));
  u8g2.print(x + unit); //Mapeo el brillo de 0-255 a 0-100%
  u8g2.sendBuffer();                     // Presentar todo en pantalla

}

void setup() {


  pinMode(boton1, INPUT);
  pinMode(boton2, INPUT);
  pinMode(boton3, INPUT);
  pinMode(boton4, INPUT);

  // Iniciar comunicación serie
  Serial.begin(115200);

  EEPROM.begin(512);
  
  CALIBRACION = eeGetInt(addr);
  
  Wire.begin();
  u8g2.begin();
  u8g2.setFont( u8g2_font_crox4h_tr  );  // Tipo de letra // u8g2_font_6x13_te //
  u8g2.setFlipMode(2);
  // Iniciar sensor
  bascula.begin(pinData, pinClk);
  // Aplicar la calibración
  bascula.set_scale(CALIBRACION);
  // Iniciar la tara
  // No tiene que haber nada sobre el peso
  bascula.tare();

}

void loop() {

  peso = bascula.get_units(5);
  Serial.print(bascula.get_units(5), 3);
  Serial.print(" Kg");
  Serial.println();
  if (unidad == 0) {
    pantalla(String(peso, 3), " kgs");
  } else {
    pantalla(String(peso * 2.20462, 3), " lbs");
  }

  //Tara, seteo del cero 
  if (digitalRead(boton4) == 1) {
    cero();
  }

  //Cambio de unidad
  if (digitalRead(boton3) == 0) {
    unidad = !unidad;
    if (unidad) {
      pantalla("lbs", "");
    } else {
      pantalla("kgs", "");
    }
    delay(500);
  }

  //Proceso de calibracion automatica a una libra (1 lb) o a 0.454 kg (454 g)
  if (digitalRead(boton2) == 0) {
    pantalla("Calibrando", "");
    delay(1000);
    while (!(peso < 0.455 && peso > 0.453)) {
      peso = bascula.get_units();
      if (peso > 0.455) {
        CALIBRACION = (CALIBRACION + 1000);
      }
      if (peso < 0.453) {
        CALIBRACION = (CALIBRACION - 1000);
      }
      bascula.set_scale(CALIBRACION);
      delay(1);
      pantalla(String(peso, 3), "");
    }
    pantalla(String(CALIBRACION, 1), "");
    bascula.set_scale(CALIBRACION);
    eeWriteInt(addr,CALIBRACION);
    delay(1000);
  }

}

void cero() {
  pantalla("Cero", "");
  delay(500);
  bascula.tare();
}

Credits

Christopher Mendez Martinez
35 projects • 76 followers
Electronic Engineer and Tech YouTuber who loves automation, programming and sharing his knowledge with everyone.

Comments