#include <Wire.h>
#include <Adafruit_GFX.h> // Oled Display
#include <Adafruit_SSD1306.h> // Oled Display
#include <PZEM004Tv30.h> // Library PZEM-004T
#include <BlynkSimpleEsp32.h> // Library blynk
#define DISPLAY_ADDRESS 0x3C
#define DISPLAY_SDA_PIN 21 // Pin SDA pada ESP32
#define DISPLAY_SCL_PIN 22 // Pin SCL pada ESP32
#define SCREEN_WIDTH 128 // OLED display width
#define SCREEN_HEIGHT 64 // OLED display height
#define OLED_DISPLAY_RESET -1 // Reset display OLED
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_DISPLAY_RESET);
// PZEM004Tv30 pzem(Serial2, 16, 17); // Specify the serial port and the pins used for RX and TX
float voltage1, current1, energy1, frequency1, power1;
float maxVoltage = 600, minVoltage = 0; // Threshold tegangan untuk trigger relay
bool hidup = true; // Kontrol on/off relay
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, DISPLAY_ADDRESS);
display.clearDisplay();
display.setCursor(10, 0);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.println("Connecting...");
display.display();
delay(100);
}
void setupDisplay() {
// Wire.begin(DISPLAY_SDA_PIN, DISPLAY_SCL_PIN);
display.begin(SSD1306_SWITCHCAPVCC, DISPLAY_ADDRESS);
display.clearDisplay();
display.setCursor(10, 0);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.println("Hello World");
display.display();
delay(100);
}
void loop() {
voltage1 = analogRead(12);
// put your main code here, to run repeatedly:
if (voltage1 >= 500) {
digitalWrite(16, LOW);
} else {
digitalWrite(16, HIGH);
}
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.printf("Voltage : %.2f\ V\n", voltage1);
display.display();
delay(2000);
}
Biru: ESP32 (GPIO 21) -> OLED (SDA)
Kuning: ESP32 (GPIO 22) -> OLED (SCL)
See: https://randomnerdtutorials.com/esp32-ssd1306-oled-display-arduino-ide/