#include "HX711.h"
#include "Adafruit_ILI9341.h"
#include "SPI.h"
#include "Wire.h"
#include "Adafruit_FT6206.h"
#define TFT_CLK 18 // CLK pin for TFT display
#define TFT_MISO 12 // MISO pin for TFT display
#define TFT_MOSI 23 // MOSI pin for TFT display
#define TFT_CS 5 // CS pin for TFT display
#define TFT_DC 21 // DC pin for TFT display
#define TFT_RST 22 // RST pin for TFT display
#define DOUT_PIN 2 // Data out pin for HX711
#define SCK_PIN 4 // Clock pin for HX711
HX711 scale;
float calibration_factor = 420; // this value is obtained by calibrating the scale with known weights
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
Serial.begin(115200);
tft.begin();
scale.begin(DOUT_PIN, SCK_PIN);
scale.set_scale(calibration_factor); // Use getDefaultCalibration() for more accuracy
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10, 10);
tft.println("Weight Measurement");
}
void loop() {
if (scale.is_ready()) {
long weight = scale.get_units(10)*1000; // Read weight in grams (adjust as needed)
tft.fillScreen(ILI9341_BLACK); // Clear previous readings
tft.setCursor(10, 10);
tft.setTextSize(2);
tft.print("Weight: ");
tft.print(weight);
tft.println(" g");
Serial.print("Weight: ");
Serial.print(weight);
Serial.println(" g");
delay(0.1); // Update every second
} else {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.setTextSize(2);
tft.setTextColor(ILI9341_RED);
tft.println("Error: HX711 not found.");
Serial.println("Error: HX711 not found.");
delay(5000); // Wait for 5 seconds before retrying
}
}