#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Wire.h>
#include <math.h>
// Define color codes
#define BLACK 0x0000
#define GREY 0x7BEF
#define LIGHTGREY 0xD69A
#define GREEN 0x07E0
#define YELLOW 0xFFE0
#define CYAN 0x07FF
#define RED 0xF800
#define MINTBLUE 0x03A6
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define ORANGE 0xFD20
#define WHITE 0xFFFF
#define TFT_DC 2
#define TFT_CS 15
#define TFT_RST -1
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Potentiometer pins
#define VOLTAGE1_PIN 34
#define CURRENT1_PIN 35
#define VOLTAGE2_PIN 32
#define CURRENT2_PIN 33
#define VOLTAGE3_PIN 25
#define CURRENT3_PIN 26
const float referenceVoltage = 3.3;
const int adcMaxValue = 4095;
const float sensorSensitivity = 0.185;
const float sensorOffset = 1.65;
const float voltageDivider = 1.0;
const float powerFactor = 0.8;
float energy = 0.0;
unsigned long lastTime = 0;
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(1);
tft.fillScreen(BLACK);
drawGrid();
}
void loop() {
// Read current and voltage for each phase
float current1 = readCurrent(CURRENT1_PIN);
float voltage1 = readVoltage(VOLTAGE1_PIN);
float current2 = readCurrent(CURRENT2_PIN);
float voltage2 = readVoltage(VOLTAGE2_PIN);
float current3 = readCurrent(CURRENT3_PIN);
float voltage3 = readVoltage(VOLTAGE3_PIN);
// Calculate parameters for each phase
float apparentPower1 = current1 * voltage1;
float activePower1 = apparentPower1 * powerFactor;
float reactivePower1 = sqrt(pow(apparentPower1, 2) - pow(activePower1, 2));
float apparentPower2 = current2 * voltage2;
float activePower2 = apparentPower2 * powerFactor;
float reactivePower2 = sqrt(pow(apparentPower2, 2) - pow(activePower2, 2));
float apparentPower3 = current3 * voltage3;
float activePower3 = apparentPower3 * powerFactor;
float reactivePower3 = sqrt(pow(apparentPower3, 2) - pow(activePower3, 2));
// Aggregate parameters for three-phase
float totalVoltage = (voltage1 + voltage2 + voltage3) / 3;
float totalCurrent = (current1 + current2 + current3) / 3;
float totalApparentPower = apparentPower1 + apparentPower2 + apparentPower3;
float totalActivePower = activePower1 + activePower2 + activePower3;
float totalReactivePower = reactivePower1 + reactivePower2 + reactivePower3;
unsigned long currentTime = millis();
float deltaTime = (currentTime - lastTime) / 3600000.0;
energy += totalActivePower * deltaTime;
lastTime = currentTime;
float frequency = calculateFrequency(voltage1);
// Display parameters for each phase and three-phase
tft.fillScreen(BLACK); // Clear screen before updating
drawGrid();
displayPhase1(voltage1, current1, activePower1, powerFactor, apparentPower1, reactivePower1);
displayPhase2(voltage2, current2, activePower2, powerFactor, apparentPower2, reactivePower2);
displayPhase3(voltage3, current3, activePower3, powerFactor, apparentPower3, reactivePower3);
displayThreePhase(totalVoltage, totalCurrent, totalActivePower, powerFactor, totalApparentPower, totalReactivePower);
delay(1000); // Update every second
}
float readVoltage(int pin) {
int voltageValue = analogRead(pin); // Baca nilai ADC
float voltage = (voltageValue / float(adcMaxValue)) * referenceVoltage / voltageDivider; // Hitung tegangan
return voltage;
}
float readCurrent(int pin) {
int currentValue = analogRead(pin); // Baca nilai ADC
float currentVoltage = (currentValue / float(adcMaxValue)) * referenceVoltage; // Hitung tegangan dari sensor
float current = (currentVoltage - sensorOffset) / sensorSensitivity; // Hitung arus
return current;
}
float calculateFrequency(float voltage) {
// Placeholder for actual frequency calculation
// This needs to be replaced with a method to calculate frequency based on voltage waveform
return 50.0; // Assuming a default frequency of 50Hz
}
void drawGrid() {
tft.drawLine(76, 0, 76, 240, GREY);
tft.drawLine(158, 0, 158, 240, GREY);
tft.drawLine(0, 0, 0, 240, GREY);
tft.drawLine(239, 0, 239, 240, GREY);
tft.drawLine(0, 120, 240, 120, GREY);
tft.drawLine(0, 0, 240, 0, GREY);
tft.drawLine(0, 239, 239, 239, GREY);
tft.drawLine(0, 18, 240, 18, GREY);
tft.drawLine(0, 138, 240, 138, GREY);
}
// Function to display phase 1 parameters
void displayPhase1(float voltage, float current, float power, float pf, float va, float VAR) {
tft.setTextColor(WHITE, BLACK);
tft.setCursor(8, 6);
tft.println("PARAMETER");
tft.setCursor(85, 6);
tft.println("PHASE-1 (R)");
tft.setTextColor(GREEN, BLACK);
tft.setCursor(8, 23);
tft.printf("VOLTAGE ");
tft.setCursor(82, 23);
tft.printf("%8.2f V\n", voltage);
tft.setTextColor(YELLOW, BLACK);
tft.setCursor(8, 35);
tft.printf("CURRENT ");
tft.setCursor(82, 35);
tft.printf("%8.2f A\n", current);
tft.setTextColor(CYAN, BLACK);
tft.setCursor(8, 47);
tft.printf("POWER ");
tft.setCursor(82, 47);
tft.printf("%8.2f W\n", power);
tft.setTextColor(RED, BLACK);
tft.setCursor(8, 59);
tft.printf("COS-PHI ");
tft.setCursor(82, 59);
tft.printf("%8.2f PF\n", pf);
tft.setTextColor(MINTBLUE, BLACK);
tft.setCursor(8, 71);
tft.printf("APPRNT PWR");
tft.setCursor(82, 71);
tft.printf("%8.2f VA\n", va);
tft.setTextColor(PURPLE, BLACK);
tft.setCursor(8, 83);
tft.printf("REACTV PWR");
tft.setCursor(82, 83);
tft.printf("%8.2f VAR\n", VAR);
tft.setTextColor(OLIVE, BLACK);
tft.setCursor(8, 95);
tft.printf("FREQUENCY ");
tft.setCursor(82, 95);
tft.printf("%8.2f Hz\n", calculateFrequency(voltage)); // Update this with actual frequency value
tft.setTextColor(ORANGE, BLACK);
tft.setCursor(8, 107);
tft.printf("ENERGY ");
tft.setCursor(82, 107);
tft.printf("%8.2f kWh\n", energy); // Update this with actual energy value
delay(100);
}
// Function to display phase 2 parameters
void displayPhase2(float voltage, float current, float power, float pf, float va, float VAR) {
tft.setTextSize(1);
tft.setTextColor(WHITE, BLACK);
tft.setCursor(8, 130);
tft.println("PHASE-2 (S)");
tft.setTextColor(GREEN, BLACK);
tft.setCursor(8, 147);
tft.printf("VOLTAGE ");
tft.setCursor(82, 147);
tft.printf("%8.2f V\n", voltage);
tft.setTextColor(YELLOW, BLACK);
tft.setCursor(8, 159);
tft.printf("CURRENT ");
tft.setCursor(82, 159);
tft.printf("%8.2f A\n", current);
tft.setTextColor(CYAN, BLACK);
tft.setCursor(8, 171);
tft.printf("POWER ");
tft.setCursor(82, 171);
tft.printf("%8.2f W\n", power);
tft.setTextColor(RED, BLACK);
tft.setCursor(8, 183);
tft.printf("COS-PHI ");
tft.setCursor(82, 183);
tft.printf("%8.2f PF\n", pf);
tft.setTextColor(MINTBLUE, BLACK);
tft.setCursor(8, 195);
tft.printf("APPRNT PWR");
tft.setCursor(82, 195);
tft.printf("%8.2f VA\n", va);
tft.setTextColor(PURPLE, BLACK);
tft.setCursor(8, 207);
tft.printf("REACTV PWR");
tft.setCursor(82, 207);
tft.printf("%8.2f VAR\n", VAR);
tft.setTextColor(OLIVE, BLACK);
tft.setCursor(8, 219);
tft.printf("FREQUENCY ");
tft.setCursor(82, 219);
tft.printf("%8.2f Hz\n", calculateFrequency(voltage)); // Update this with actual frequency value
tft.setTextColor(ORANGE, BLACK);
tft.setCursor(8, 231);
tft.printf("ENERGY ");
tft.setCursor(82, 231);
tft.printf("%8.2f kWh\n", energy); // Update this with actual energy value
delay(100);
}
// Function to display phase 3 parameters
void displayPhase3(float voltage, float current, float power, float pf, float va, float VAR) {
tft.setTextSize(1);
tft.setTextColor(WHITE, BLACK);
tft.setCursor(8, 130);
tft.println("PHASE-3 (T)");
tft.setTextColor(GREEN, BLACK);
tft.setCursor(8, 147);
tft.printf("VOLTAGE ");
tft.setCursor(82, 147);
tft.printf("%8.2f V\n", voltage);
tft.setTextColor(YELLOW, BLACK);
tft.setCursor(8, 159);
tft.printf("CURRENT ");
tft.setCursor(82, 159);
tft.printf("%8.2f A\n", current);
tft.setTextColor(CYAN, BLACK);
tft.setCursor(8, 171);
tft.printf("POWER ");
tft.setCursor(82, 171);
tft.printf("%8.2f W\n", power);
tft.setTextColor(RED, BLACK);
tft.setCursor(8, 183);
tft.printf("COS-PHI ");
tft.setCursor(82, 183);
tft.printf("%8.2f PF\n", pf);
tft.setTextColor(MINTBLUE, BLACK);
tft.setCursor(8, 195);
tft.printf("APPRNT PWR");
tft.setCursor(82, 195);
tft.printf("%8.2f VA\n", va);
tft.setTextColor(PURPLE, BLACK);
tft.setCursor(8, 207);
tft.printf("REACTV PWR");
tft.setCursor(82, 207);
tft.printf("%8.2f VAR\n", VAR);
tft.setTextColor(OLIVE, BLACK);
tft.setCursor(8, 219);
tft.printf("FREQUENCY ");
tft.setCursor(82, 219);
tft.printf("%8.2f Hz\n", calculateFrequency(voltage)); // Update this with actual frequency value
tft.setTextColor(ORANGE, BLACK);
tft.setCursor(8, 231);
tft.printf("ENERGY ");
tft.setCursor(82, 231);
tft.printf("%8.2f kWh\n", energy); // Update this with actual energy value
delay(100);
}
// Function to display aggregated three-phase parameters
void displayThreePhase(float voltage, float current, float power, float pf, float va, float VAR) {
tft.setTextSize(1);
tft.setTextColor(WHITE, BLACK);
tft.setCursor(8, 130);
tft.println("THREE-PHASE");
tft.setTextColor(GREEN, BLACK);
tft.setCursor(8, 147);
tft.printf("VOLTAGE ");
tft.setCursor(82, 147);
tft.printf("%8.2f V\n", voltage);
tft.setTextColor(YELLOW, BLACK);
tft.setCursor(8, 159);
tft.printf("CURRENT ");
tft.setCursor(82, 159);
tft.printf("%8.2f A\n", current);
tft.setTextColor(CYAN, BLACK);
tft.setCursor(8, 171);
tft.printf("POWER ");
tft.setCursor(82, 171);
tft.printf("%8.2f W\n", power);
tft.setTextColor(RED, BLACK);
tft.setCursor(8, 183);
tft.printf("COS-PHI ");
tft.setCursor(82, 183);
tft.printf("%8.2f PF\n", pf);
tft.setTextColor(MINTBLUE, BLACK);
tft.setCursor(8, 195);
tft.printf("APPRNT PWR");
tft.setCursor(82, 195);
tft.printf("%8.2f VA\n", va);
tft.setTextColor(PURPLE, BLACK);
tft.setCursor(8, 207);
tft.printf("REACTV PWR");
tft.setCursor(82, 207);
tft.printf("%8.2f VAR\n", VAR);
tft.setTextColor(OLIVE, BLACK);
tft.setCursor(8, 219);
tft.printf("FREQUENCY ");
tft.setCursor(82, 219);
tft.printf("%8.2f Hz\n", calculateFrequency(voltage)); // Update this with actual frequency value
tft.setTextColor(ORANGE, BLACK);
tft.setCursor(8, 231);
tft.printf("ENERGY ");
tft.setCursor(82, 231);
tft.printf("%8.2f kWh\n", energy); // Update this with actual energy value
delay(100);
}