|
| 1 | +/* |
| 2 | + * voltage_and_current_monitor.ino |
| 3 | + * |
| 4 | + * Created 27 April 2020 (amid CoViD-19 outbreaks) |
| 5 | + * by Zulns |
| 6 | + * @Gorontalo, Indonesia |
| 7 | + * |
| 8 | + * This example code is in the public domain. |
| 9 | + * |
| 10 | + * https://github.com/ZulNs/PowerMonitor |
| 11 | + */ |
| 12 | + |
| 13 | +#include <PowerMonitor.h> |
| 14 | + |
| 15 | +const byte VOLTAGE_SENSOR_PIN = 0; |
| 16 | +const byte CURRENT_SENSOR_PIN = 1; |
| 17 | +const float VOLTAGE_CALIBRATION = 155.0; |
| 18 | +const float CURRENT_CALIBRATION = 3.06; |
| 19 | + |
| 20 | +PowerMonitor pmon; |
| 21 | + |
| 22 | +void setup() { |
| 23 | + int i; |
| 24 | + int16_t * samplesV, * samplesI; |
| 25 | + |
| 26 | + Serial.begin(9600); |
| 27 | + while (!Serial); |
| 28 | + |
| 29 | + Serial.println(); |
| 30 | + Serial.println("*** AC Voltage, Current and Power Monitor ***"); |
| 31 | + Serial.println(); |
| 32 | + |
| 33 | + pmon.initVoltageSensor(VOLTAGE_SENSOR_PIN, VOLTAGE_CALIBRATION); |
| 34 | + pmon.initCurrentSensor(CURRENT_SENSOR_PIN, CURRENT_CALIBRATION); |
| 35 | +} |
| 36 | + |
| 37 | +void loop() { |
| 38 | + float freq = pmon.getFrequency(); |
| 39 | + float Vcc = pmon.getVcc() / 1000.0; |
| 40 | + |
| 41 | + pmon.sampleAndCalculate(); |
| 42 | + |
| 43 | + if (freq > 0.0 && pmon.sampleCount > 0) { |
| 44 | + Serial.println("Vcc (ADC ref voltage)\t= " + String(Vcc, 3) + " V"); |
| 45 | + Serial.println("Out VT (Vrms)\t\t= " + String(pmon.Vrms / VOLTAGE_CALIBRATION, 3) + " V"); |
| 46 | + Serial.println("Out CT (Vrms)\t\t= " + String(pmon.Irms / CURRENT_CALIBRATION, 3) + " V"); |
| 47 | + Serial.println(); |
| 48 | + Serial.println("Frequency\t\t= " + String(freq, 3) + " Hz"); |
| 49 | + Serial.println("Vrms\t\t\t= " + String(pmon.Vrms, 3) + " V"); |
| 50 | + Serial.println("Irms\t\t\t= " + String(pmon.Irms, 3) + " A"); |
| 51 | + Serial.println("Real power\t\t= " + String(pmon.realPower, 3) + " W"); |
| 52 | + Serial.println("Apparent power\t\t= " + String(pmon.apparentPower, 3) + " VA"); |
| 53 | + Serial.println("Power factor\t\t= " + String(pmon.powerFactor, 3)); |
| 54 | + } |
| 55 | + else |
| 56 | + Serial.println("No AC voltage..."); |
| 57 | + Serial.println(); |
| 58 | + delay(500); |
| 59 | +} |
0 commit comments