#define BLYNK_TEMPLATE_ID "TMPL6gu43SB9Q"
#define BLYNK_TEMPLATE_NAME "Sensor HX711 dan PIR"
#define BLYNK_AUTH_TOKEN "nR1mq5i5mxHO57Bd6ieAS1qDGiPYXHLg"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h> // Blynk library for ESP32
#include <HX711.h> // HX711 load cell library
const char* ssid = "Wokwi-GUEST"; // Replace with your Wi-Fi SSID
const char* password = ""; // Replace with your Wi-Fi password
char auth[] = "nR1mq5i5mxHO57Bd6ieAS1qDGiPYXHLg"; // Blynk Auth Token
const int PIR_PIN = 18; // PIR Motion Sensor pin
const int LED_PIN = 15; // LED pin
const int BUZZER_PIN = 2; // Buzzer pin
// Load Cell HX711 setup
const int LOADCELL_DOUT_PIN = 4;
const int LOADCELL_SCK_PIN = 5;
HX711 scale;
float calibration_factor = 420; // Adjust based on calibration
// Initialize LCD I2C with address 0x27 and size 16x2
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT); // Set PIR sensor pin as input
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
pinMode(BUZZER_PIN, OUTPUT); // Set Buzzer pin as output
// Initialize LCD
Wire.begin(21, 22);
lcd.begin(16, 2);
lcd.backlight();
lcd.print("LCD OK!");
delay(1000);
// Setup Load Cell HX711
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor); // Set calibration factor
scale.tare(); // Set initial weight to 0
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Connect to Blynk
Blynk.begin(auth, ssid, password);
}
void blinkLEDAndBuzzer() {
for (int i = 0; i < 3; i++) {
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 800);
delay(300);
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
delay(300);
}
}
void loop() {
Blynk.run(); // Keep Blynk connection alive
// Read PIR sensor to detect motion
bool motionDetected = digitalRead(PIR_PIN) == HIGH;
// Read Load Cell weight
float weight = scale.get_units(10); // Average of 10 readings
// Display PIR and Load Cell data on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PIR: ");
lcd.print(motionDetected ? "Motion" : "No Motion");
lcd.setCursor(0, 1);
lcd.print("Weight: ");
lcd.print(weight, 2); // Display weight with 2 decimal places
// Send PIR and weight data to Blynk
Blynk.virtualWrite(V1, motionDetected ? "Motion" : "No Motion"); // V1 for PIR status
Blynk.virtualWrite(V2, weight); // V2 for Load Cell weight
// Serial monitor output
Serial.print("PIR: ");
Serial.println(motionDetected ? "Motion" : "No Motion");
Serial.print("Weight: ");
Serial.println(weight, 2); // Display weight with 2 decimal places
// If motion is detected
if (motionDetected) {
lcd.setCursor(0, 1);
lcd.print("Motion Detected");
Serial.println("Motion Detected");
blinkLEDAndBuzzer();
}
// If weight exceeds 3kg
else if (weight >= 3.0) {
lcd.setCursor(0, 1);
lcd.print("Weight >= 3kg");
Serial.println("Weight >= 3kg");
blinkLEDAndBuzzer();
} else {
// Turn off LED and Buzzer if no action
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
}
delay(200); // Wait before reading again
}