#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <HX711.h>
// Define I2C address for the LCD
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
// Initialize the LCD object
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
// DHT Sensor Type
#define DHTTYPE DHT22
// DHT Pin (Adjust based on your Wokwi setup)
const int dhtPin = 14;
// HX711 Pins (Adjust based on your Wokwi setup)
const int doutPin = 5;
const int sckPin = 4;
// Button Pin (Adjust based on your Wokwi setup)
const int buttonPin = 12;
// HX711 object
HX711 scale;
// DHT object
DHT dht(dhtPin, DHTTYPE); // Correct constructor call
// Flag for displaying temperature or weight
bool showTemperature = false;//
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
// Initialize DHT sensor
dht.begin(); // Call begin() without arguments
// Initialize the HX711 scale
scale.begin(doutPin, sckPin);
// Set the HX711 scale's tare value (zeroing the scale)
scale.tare(42);
scale.set_scale(420);
// Set the button as input with pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Initial display
displayWeight();
}
void loop() {
// Check for button press
if (digitalRead(buttonPin) == LOW) {
showTemperature = !showTemperature;
if (showTemperature) {
displayTemperature();
}
else {
displayWeight();
}
delay(200); // Debounce the button
}
}
// Function to display weight on the LCD
void displayWeight() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weight: ");
lcd.print(scale.get_units(), 2); // Print weight with one decimal place
lcd.print("k.g");
}
// Function to display temperature on the LCD
void displayTemperature() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(dht.readTemperature(), 1); // Print temperature with one decimal place
lcd.print("C");
lcd.setCursor(0, 2);
lcd.print("Vlag: ");
lcd.print(dht.readHumidity(), 1); // Print temperature with one decimal place
lcd.print("%");
}
/////////