Skip to content

Instantly share code, notes, and snippets.

@roboter
Created November 1, 2024 08:05
Show Gist options
  • Save roboter/a097e4176e5e89a86056eea6906fdb0c to your computer and use it in GitHub Desktop.
Save roboter/a097e4176e5e89a86056eea6906fdb0c to your computer and use it in GitHub Desktop.
#include <HX711.h>
#include <Wire.h>
#include <LCD-I2C.h>
// HX711 connections
#define LOADCELL_DOUT_PIN 6
#define LOADCELL_SCK_PIN 5
// Initialize HX711
HX711 scale;
// Initialize the I2C LCD (make sure the address is correct)
LCD_I2C lcd(0x27, 16, 2); // Change address if necessary
void setup() {
// Start serial communication for debugging
Serial.begin(9600);
// Initialize the LCD
lcd.begin();
lcd.backlight(); // Turn on the backlight
// Initialize the scale
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(); // Set scale to the default value, adjust if needed
scale.tare(); // Reset the scale to 0
lcd.print("Taring..."); // Display tare message
delay(2000); // Wait for a moment
lcd.clear(); // Clear the LCD
}
void loop() {
// Read weight from the scale
float weight = scale.get_units(10); // Average of 10 readings
// Print weight to serial monitor
Serial.print("Weight: ");
Serial.println(weight);
// Display the weight on the LCD
lcd.setCursor(0, 0); // Set cursor to first row
lcd.print("Weight: ");
lcd.setCursor(0, 1); // Set cursor to second row
lcd.print(weight); // Print weight
lcd.print(" g"); // Add unit (grams)
delay(1000); // Update every second
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment