#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HX711.h>
// OLED display width and height, in pixels
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin not used
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// HX711 Load Cell setup
HX711 scale;
const int loadCellDoutPin = 16; // Connect to HX711 DT
const int loadCellSckPin = 17; // Connect to HX711 SCK
// LED and Buzzer Pins
const int greenLedPin = 23;
const int redLedPin = 19;
const int buzzerPin = 18;
// Threshold weight (you can adjust this)
float weightThreshold = 10.0; // Example threshold for full bin
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize the OLED display with I2C address 0x3C
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Initialize the HX711
scale.begin(loadCellDoutPin, loadCellSckPin);
// Initialize the LEDs and buzzer as outputs
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Clear the display buffer
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
display.println("Weight Sensing Bin");
display.display(); // Display initial text
delay(2000); // Pause for 2 seconds
}
void loop() {
// Check if the scale is ready to provide readings
if (scale.is_ready()) {
float weight = scale.get_units(10); // Take an average of 10 readings
Serial.print("Weight: ");
Serial.println(weight);
// Display weight on the OLED screen
display.clearDisplay();
display.setCursor(0, 0); // Set cursor at top-left corner
display.println("Current Weight:");
display.setTextSize(2); // Make the text larger
display.setCursor(0, 16);
display.print(weight, 2); // Print the weight with two decimal points
display.print(" kg");
display.display(); // Update the display
// Handle LED and buzzer alerts
if (weight >= weightThreshold) {
digitalWrite(redLedPin, HIGH); // Turn on red LED
digitalWrite(greenLedPin, LOW); // Turn off green LED
tone(buzzerPin, 1000); // Buzzer ON
} else {
digitalWrite(greenLedPin, HIGH); // Turn on green LED
digitalWrite(redLedPin, LOW); // Turn off red LED
noTone(buzzerPin); // Buzzer OFF
}
delay(1000); // Wait for 1 second before the next reading
} else {
Serial.println("HX711 not found.");
}
}
Loading
ssd1306
ssd1306