#include <LiquidCrystal_I2C.h>
#include "HX711.h"
#include <NewPing.h> // Include NewPing library
HX711 scale;
LiquidCrystal_I2C LCD(0x27, 16, 2); // Initialize LCD object
const int LOADCELL_DOUT_PIN = 26; // Assuming DOUT pin is connected to analog pin A1
const int LOADCELL_SCK_PIN = 18; // Assuming SCK pin is connected to GPIO pin 18
const int TRIGGER_PIN = 5; // Trigger pin of ultrasonic sensor connected to GPIO pin 12
const int ECHO_PIN = 19; // Echo pin of ultrasonic sensor connected to GPIO pin 13
const float loadcell_calibration_factor = -50; // Adjust this value after calibration
const int known_height = 100; // Known height in centimeters (adjust as needed)
NewPing sonar(TRIGGER_PIN, ECHO_PIN); // Initialize NewPing with trigger and echo pins
void setup() {
Serial.begin(115200);
delay(10);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Initializing...");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(loadcell_calibration_factor); // Set the scale factor after calibration
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Load Cell Ready");
Serial.begin(9600);
Serial.println("Initializing the scale");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
}
void loop() {
// Read weight from load cell
float loadcell_raw_reading = scale.get_units(10); // Read the load cell value in grams
float loadcell_kg_reading = loadcell_raw_reading / 1000.0; // Convert the reading to kilograms
// Read distance from ultrasonic sensor
unsigned int distance = sonar.ping_cm();
// Calculate height
int height = known_height - distance;
// Calculate BMI
float height_m = height / 100.0; // Convert height to meters
float bmi = loadcell_kg_reading / (height_m * height_m); // Calculate BMI
// Display readings on LCD
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Weight: ");
LCD.print(loadcell_kg_reading, 2); // Display the weight in kilograms on LCD with two decimal places
LCD.print(" kg");
LCD.setCursor(0, 1);
LCD.print("Height: ");
LCD.print(height);
LCD.print(" cm");
LCD.setCursor(0, 2);
LCD.print("BMI: ");
LCD.print(bmi, 2); // Display BMI with two decimal places
// Print readings to Serial Monitor
Serial.print("Weight (kg): ");
Serial.println(loadcell_kg_reading, 2); // Print the weight in kilograms to Serial Monitor
Serial.print("Height: ");
Serial.print(height);
Serial.println(" cm");
Serial.print("BMI: ");
Serial.println(bmi, 2); // Print BMI to Serial Monitor
delay(1000);
}