#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
#include <ZMPT101B.h>
#define SENSITIVITY 500.0f
// and the voltage source frequency is 50 Hz.
int vsensor = A1;
const int ACPin = A0;
ZMPT101B voltageSensor(vsensor, 50.0);
float correctionfactor = 20;
float Current_Value = 0;
#define ACTectionRange 20; //set Non-invasive AC Current Sensor tection range (5A,10A,20A)
#define VREF 3.3
char strf[20];
char units[8];
unsigned long last_time = 0;
unsigned long current_time = 0;
float Wh = 0;
void setup() {
Serial.begin(9600);
u8g2.begin();
u8g2.setContrast(50);
u8g2.setFlipMode(0);
pinMode(ACPin, INPUT);
voltageSensor.setSensitivity(SENSITIVITY);
delay(1000);
u8g2.clearBuffer();
u8g2.drawRFrame(0, 0, 128, 64, 5);
u8g2.setFont(u8g2_font_t0_22b_mf);
u8g2.drawStr(9, 18, "date");
u8g2.drawStr(20, 38, "time");
// u8g2.drawStr((128 - u8g2.getUTF8Width(123.5)) / 2, 58, 123.5);
u8g2.sendBuffer();
delay(300);
}
void loop() {
float ACcurrent = 0;
float current_value = readACCurrentValue();
ACcurrent = current_value;
delay(1000);
//Serial.print(ACcurrent);
//Serial.println("Current: ");
float acvoltage = voltageSensor.getRmsVoltage();
Serial.println(acvoltage);
float P = acvoltage * ACcurrent;
last_time = current_time;
current_time = millis();
Wh = Wh + P * ((current_time - last_time) / 3600000.0);
u8g2.clearBuffer();
u8g2.drawRFrame(0, 0, 128, 64, 5);
u8g2.setFont(u8g2_font_t0_18b_mf); // ZeichenBreite = 9
u8g2.drawStr(5, 15, "U:");
snprintf(units, 8, "%s", dtostrf(acvoltage, 7, 5, strf));
u8g2.drawStr(32, 15, units);
u8g2.drawStr(95, 15, " V");
u8g2.drawStr(5, 30, "I:");
snprintf(units, 8, "%s", dtostrf(ACcurrent, 7, 5, strf));
u8g2.drawStr(32, 30, units);
u8g2.drawStr(95, 30, " A");
u8g2.drawStr(5, 45, "P:");
snprintf(units, 8, "%s", dtostrf(P, 7, 5, strf));
u8g2.drawStr(32, 45, units);
u8g2.drawStr(95, 45, " W");
u8g2.drawStr(5, 60, "E:");
snprintf(units, 8, "%s", dtostrf(Wh, 7, 5, strf));
u8g2.drawStr(32, 60, units);
u8g2.drawStr(95, 60, " Wh");
u8g2.sendBuffer();
delay(500);
}
float readACCurrentValue() {
float ACCurrentValue = 0;
float peakVoltage = 0;
float voltageVirtualValue = 0; //Vrms
for (int i = 0; i < 5; i++) {
peakVoltage += analogRead(ACPin); //read peak voltage
Serial.println(ACPin);
peakVoltage = peakVoltage + correctionfactor;
delay(1);
}
peakVoltage = peakVoltage / 5;
voltageVirtualValue = peakVoltage * 0.707; //change the peak voltage to the Virtual Value of voltage
/*The circuit is amplified by 2 times, so it is divided by 2.*/
voltageVirtualValue = (voltageVirtualValue / 1023 * VREF) / 2;
ACCurrentValue = voltageVirtualValue * ACTectionRange;
return ACCurrentValue;
}Loading
ssd1306
ssd1306