Skip to content

Instantly share code, notes, and snippets.

@RianWardana
Last active May 31, 2018 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RianWardana/565fc51e1ebdab0c411277d9de30c619 to your computer and use it in GitHub Desktop.
Save RianWardana/565fc51e1ebdab0c411277d9de30c619 to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads(0x48);
#define pin_relay 13
#define pin_sensor 0
float power = 0;
void setup(){
Serial.begin(9600);
pinMode(pin_relay, OUTPUT);
digitalWrite(pin_relay, HIGH);
ads.begin();
}
void loop(){
power = dayaSemu();
power = (power < 5 ? 0 : power);
Serial.print(power);
Serial.print(",");
checkSerial();
}
void checkSerial() {
if (Serial.available()) {
if (Serial.read() == '0') {
digitalWrite(pin_relay, HIGH);
} else {
digitalWrite(pin_relay, LOW);
}
}
}
float dayaSemu() {
float S;
float Vpp, Vrms, Irms;
int16_t readValue;
int16_t maxValue = 0;
int16_t minValue = 32767;
uint32_t start_time = millis();
while((millis() - start_time) < 999) {
readValue = ads.readADC_SingleEnded(0);
if (readValue > maxValue) maxValue = readValue;
if (readValue < minValue) minValue = readValue;
}
Vpp = ((maxValue - minValue) * 0.1875) / 1000.0;
Vrms = (Vpp / 2.0) * 0.7;
Irms = (Vrms * 1000) / 185;
S = Irms * 202;
return S;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment