#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define NUM_SAMPLES 500
#define VREF 3.3
#define ADC_RES 4095.0
#define noiseThreshold 0.5
#define voltageCalibration 312.0 // Sesuaikan dengan hasil kalibrasi ZMPT101B Anda
// Pin untuk masing-masing input
const int pinUPS1 = 32;
const int pinUPS2 = 33;
const int pinPLN = 34;
const int pinGENSET = 35;
float readVoltageAC(int pin) {
float offset = 0;
for (int i = 0; i < NUM_SAMPLES; i++) {
offset += analogRead(pin);
}
offset /= NUM_SAMPLES;
float sumSq = 0;
for (int i = 0; i < NUM_SAMPLES; i++) {
float val = analogRead(pin);
float centered = val - offset;
sumSq += centered * centered;
delayMicroseconds(100); // Sampling delay
}
float rms = sqrt(sumSq / NUM_SAMPLES);
float voltageADC = (rms / ADC_RES) * VREF;
float acVoltage = voltageADC * voltageCalibration;
if (acVoltage < noiseThreshold) acVoltage = 0.0;
return acVoltage;
}
void setup() {
Serial.begin(115200);
analogReadResolution(12); // 12-bit ADC for ESP32
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("OLED tidak ditemukan"));
while (1);
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(10, 10);
display.println("Monitoring 4 Tegangan...");
display.display();
delay(1500);
}
void loop() {
float vUPS1 = readVoltageAC(pinUPS1);
float vUPS2 = readVoltageAC(pinUPS2);
float vPLN = readVoltageAC(pinPLN);
float vGENSET = readVoltageAC(pinGENSET);
Serial.printf("UPS1: %.1fV | UPS2: %.1fV | PLN: %.1fV | GENSET: %.1fV\n", vUPS1, vUPS2, vPLN, vGENSET);
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.printf("UPS1 : %.1f V\n", vUPS1);
display.setCursor(0, 16);
display.printf("UPS2 : %.1f V\n", vUPS2);
display.setCursor(0, 32);
display.printf("PLN : %.1f V\n", vPLN);
display.setCursor(0, 48);
display.printf("GENSET : %.1f V\n", vGENSET);
display.display();
delay(1000);
}
No comments:
Post a Comment