#define BLYNK_TEMPLATE_ID "TMPL6d-s2kt4O"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "EKHllwYWMhkZFllcdEyl5eaaD5y_U6EY"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST"; //nama hotspot yang digunakan
char pass[] = ""; //password hotspot yang digunakan
// Ultrasonic Sensor Pins
const int trigPin = 5; // Pin Trigger HC-SR04
const int echoPin = 18; // Pin Echo HC-SR04
// Load Cell Pins
const int LOADCELL_DOUT_PIN = 21; // Pin DOUT Load Cell
const int LOADCELL_SCK_PIN = 19; // Pin SCK Load Cell
// Buzzer Pin
const int buzzerPin = 25; // Pin buzzer
// Servo Pin
const int servoPin = 26; // Pin servo
// Custom I2C Pins
const int I2C_SDA = 23; // Contoh pin SDA kustom
const int I2C_SCL = 22; // Contoh pin SCL kustom
// LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);
// HX711 Load Cell
HX711 scale;
// Servo
Servo myservo;
// Calibration factor for the load cell (example value, replace with your own)
float calibration_factor = 10000.0; // Adjust this value based on your calibration
void setup() {
Serial.begin(9600);
// Initialize I2C with custom pins
Wire.begin(I2C_SDA, I2C_SCL);
// Initialize LCD
lcd.begin(16, 2);
lcd.backlight();
// Initialize Load Cell
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
// Initialize Buzzer
pinMode(buzzerPin, OUTPUT);
// Initialize Servo
myservo.attach(servoPin);
// Initialize Ultrasonic Sensor Pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Connect to WiFi and Blynk
Blynk.begin(auth, ssid, pass);
}
BLYNK_WRITE(V0) {
int pinValue = param.asInt(); // Get value as integer
if (pinValue) {
for (int pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 90 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 5ms for the servo to reach the position
}
} else {
myservo.write(0); // Move servo to 0 degrees
}
}
// Blynk virtual write handlers for button 2 (move servo from 90 to 0 degrees)
BLYNK_WRITE(V1) {
int pinValue = param.asInt(); // Get value as integer
if (pinValue) {
for (int pos = 90; pos >= 0; pos -= 1) { // goes from 90 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 5ms for the servo to reach the position
}
} else {
myservo.write(0); // Move servo to 0 degrees
}
}
void loop() {
Blynk.run();
// Measure distance with Ultrasonic Sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Read Load Cell
if (scale.is_ready()) {
long reading = scale.read() * 20000;
float grams = (float)reading / calibration_factor; // Convert to grams
grams = grams + 800; // Adjust for tare weight or other offsets if needed
Serial.print("Load Cell Reading: ");
Serial.print(grams);
Serial.println(" g");
// Display on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dist: ");
lcd.print(distance);
lcd.print(" cm");
lcd.setCursor(0, 1);
lcd.print("Weight: ");
lcd.print(grams);
lcd.print(" g");
// Check Condition to Trigger Buzzer
if (distance < 10 || grams > 3000) { // Sesuaikan ambang batas sesuai kebutuhan
digitalWrite(buzzerPin, HIGH);
} else {
digitalWrite(buzzerPin, LOW);
}
} else {
Serial.println("Load Cell not found.");
}
delay(1000);
}