Skip to content

Instantly share code, notes, and snippets.

@fajarlabs
Created December 17, 2024 18:54
Show Gist options
  • Save fajarlabs/bfba91f8249ba43587067acd95af71a4 to your computer and use it in GitHub Desktop.
Save fajarlabs/bfba91f8249ba43587067acd95af71a4 to your computer and use it in GitHub Desktop.
Program Watt Meter LCD 20x4
#include "ACS712.h"
#include <ZMPT101B.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Inisialisasi LCD (Alamat I2C dan ukuran layar)
LiquidCrystal_I2C lcd(0x27, 20, 4); // Pastikan alamat I2C benar, biasanya 0x27
//ACS712_20A for 20 Amp type
//ACS712_30A for 30 Amp type
ACS712 sensor(ACS712_30A, A0);
// ZMPT101B sensor output connected to analog pin A0
// and the voltage source frequency is 50 Hz.
ZMPT101B voltageSensor(A1, 50.0);
#define SENSITIVITY 500.0f
// Tarif dasar listrik (Rp per KWh)
const float tarifPerKWh = 1444.0;
// Variabel untuk menyimpan nilai
float voltage = 0.0;
float current = 0.0;
float power = 0.0;
float energy = 0.0; // KWh
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
float cost = 0.0;
void setup() {
Serial.begin(9600);
// Inisialisasi LCD
lcd.init();
lcd.backlight(); // Pastikan LCD menyala
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SISTEM MONITORING ");
lcd.setCursor(0, 1);
lcd.print("KWH METER DIGITAL ");
lcd.setCursor(0, 2);
lcd.print("INDEKOS BERBASIS ");
lcd.setCursor(0, 3);
lcd.print("ARDUINO UNO - ACS712");
delay(2000);
lcd.clear();
// acs calibrate
sensor.calibrate();
// Change the sensitivity value based on value you got from the calibrate
// example.
voltageSensor.setSensitivity(SENSITIVITY);
}
void loop() {
float current = sensor.getCurrentAC();
//ignoring the value below 0.09
if (current < 0.09) {
current = 0;
} else {
current = current * 0.615; // calibrating current
}
// read the voltage and then print via Serial.
float voltage = voltageSensor.getRmsVoltage();
Serial.println(voltage);
// Menghitung daya dalam watt
power = voltage * current;
// Menghitung energi dalam KWh
elapsedTime = (millis() - startTime) / 1000.0; // Waktu dalam detik
energy = (power / 1000.0) * (elapsedTime / 3600.0); // Menghitung KWh (konversi detik ke jam)
// Menghitung biaya pemakaian
cost = energy * tarifPerKWh;
// Menampilkan data ke LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ID:MARCO SIRAIT");
lcd.setCursor(0, 1);
lcd.print("V:");
lcd.print(voltage, 1); // Menampilkan tegangan (1 angka desimal)
lcd.setCursor(10, 1);
lcd.print("A:");
lcd.print(current, 2); // Menampilkan arus (2 angka desimal)
lcd.setCursor(0, 2);
lcd.print("Kwh:");
lcd.print(energy, 5); // Menampilkan KWh (2 angka desimal)
lcd.setCursor(12, 2);
lcd.print("H:");
lcd.print(elapsedTime / 3600); // Menampilkan waktu dalam jam (desimal)
lcd.setCursor(16, 2);
lcd.print("S:");
lcd.print(elapsedTime); // Menampilkan waktu dalam jam (desimal)
lcd.setCursor(0, 3);
lcd.print("Rp:");
lcd.print(cost, 2); // Menampilkan biaya
lcd.setCursor(12, 3);
lcd.print("TDL:");
lcd.print(tarifPerKWh, 0); // Menampilkan tarif
delay(1000); // Delay untuk pembaruan tampilan
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment