diff --git a/index.ino b/index.ino new file mode 100644 index 0000000..b8106cf --- /dev/null +++ b/index.ino @@ -0,0 +1,181 @@ +#include +#include +#include "DHT.h" + + +// pin 8 - Serial clock out (SCLK) +// pin 9 - Serial data out (DIN) +// pin 10 - Data/Command select (D/C) +// pin 11 - LCD chip select (CS) +// pin 12 - LCD reset (RST) + + +#define RST 12 +#define CE 11 +#define DC 10 +#define DIN 9 +#define CLK 8 + + +#define voltagePin A3 +#define currentPin A4 +#define tempPin 2 + +#define DHTTYPE DHT11 + +#define loadRelay 3 +#define fanRelay 4 + +const float voltageThreshold = 17; +const double currentThreshold = 2; +const float tempThreshold = 50; + +const int toleranceTime = 1000; +//Creating the instances + +DHT dht(tempPin, DHTTYPE); +Adafruit_PCD8544 display = Adafruit_PCD8544(CLK,DIN,DC,CE,RST); + + + +float getVoltage(){ + return analogRead(voltagePin)*5.0/1024*5.7/1.414; +} + +const int sensorIn = A4; +int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module + +double currVolt = 0; +double VRMS = 0; +double AmpsRMS = 0; + +float getCurr(){ + + currVolt = getVPP(); + VRMS = (currVolt/2.0) *0.707; //root 2 is 0.707 + AmpsRMS = (VRMS * 1000)/mVperAmp; + return AmpsRMS; +} + +float getVPP() +{ + float result; + int readValue; //value read from the sensor + int maxValue = 0; // store max value here + int minValue = 1024; // store min value here + + uint32_t start_time = millis(); + while((millis()-start_time) < 1000) //sample for 1 Sec + { + readValue = analogRead(sensorIn); + // see if you have a new maxValue + if (readValue > maxValue) + { + /*record the maximum sensor value*/ + maxValue = readValue; + } + if (readValue < minValue) + { + /*record the minimum sensor value*/ + minValue = readValue; + } + } + + // Subtract min from max + result = ((maxValue - minValue) * 5.0)/1024.0; + + return result; +} + +float readTemp(){ + float t = dht.readTemperature(); + return t; + +} + +void setup(){ + Serial.begin(9600); + pinMode(loadRelay,OUTPUT); + pinMode(fanRelay,OUTPUT); + digitalWrite(loadRelay,HIGH); + digitalWrite(fanRelay,HIGH); + display.begin(); + + display.setContrast(50); + + + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(BLACK); + display.setCursor(10,15); + display.println("Transformer"); + display.println(" Protection"); + display.display(); + delay(1000); + display.clearDisplay(); + +} + +float Voltage; +float Current; +float Temp; + +bool stateOfLoad = HIGH; +bool stateOfFan = HIGH; + +void loop(){ + Voltage = getVoltage(); + Current = getCurr(); + Temp = readTemp(); + display.clearDisplay(); + display.print("V:"); + display.println(Voltage); + display.print("I:"); + + display.println(Current); + display.print("T:"); + display.println(Temp); + Serial.print(Voltage); + Serial.print("\t"); + Serial.println(Current); + + + + if(Voltage>voltageThreshold){ + if(stateOfLoad == HIGH){ + stateOfLoad = LOW; + delay(500); + digitalWrite(loadRelay,LOW); + } + display.println("\nOvervoltage !"); + delay(500); + } + if(Current>currentThreshold){ + if(stateOfLoad == HIGH){ + stateOfLoad = LOW; + delay(500); + digitalWrite(loadRelay,LOW); + } + display.println("\nOvercurrent !"); + delay(500); + } + if(Temp>tempThreshold){ + if(stateOfFan == HIGH){ + stateOfFan = LOW; + delay(500); + digitalWrite(fanRelay,LOW); + } + display.println("\nOvertemperature !"); + delay(500); + } + if((Voltage<=voltageThreshold)&&(Current<=currentThreshold)&&(Temp<=tempThreshold)){ + display.println("\nStatus:OK"); + } + display.display(); + if(Temp<=tempThreshold){ + digitalWrite(fanRelay,HIGH); + } + delay(20); + + +} diff --git a/initialTest.ino b/initialTest.ino deleted file mode 100644 index dfe651f..0000000 --- a/initialTest.ino +++ /dev/null @@ -1,137 +0,0 @@ -#include -#include -#include "DHT.h" - -//TODO:Test the entire Circuit - -// pin 8 - Serial clock out (SCLK) -// pin 9 - Serial data out (DIN) -// pin 10 - Data/Command select (D/C) -// pin 11 - LCD chip select (CS) -// pin 12 - LCD reset (RST) - - -#define RST 12 -#define CE 11 -#define DC 10 -#define DIN 9 -#define CLK 8 - - -#define voltagePin A3 -#define currentPin A4 -#define tempPin 2 - -#define DHTTYPE DHT11 - -#define loadRelay 3 -#define fanRelay 4 - -const float voltageThreshold = 17; -const float currentThreshold = 2; -const float tempThreshold = 50; - -//Creating the instances - -DHT dht(tempPin, DHTTYPE); -Adafruit_PCD8544 display = Adafruit_PCD8544(CLK,DIN,DC,CE,RST); - - - -float getVoltage(){ - return analogRead(voltagePin)*5.0/1024*5.7/1.414; -} - -float getRmsCurrent(){ - int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module - double Voltage = 0; - double Vrms = 0; - double AmpsRms = 0; - Voltage = getVPP(); - Vrms = (Voltage/2.0) * 0.707; - AmpsRms = (Vrms * 1000)/mVperAmp; - return AmpsRms; -} - -float getVPP(){ - int readValue; - int maxValue = 0; - int minValue = 1024; - - uint32_t startTime = millis(); // Start Time - while((millis()-startTime)<1000){ - readValue = analogRead(currentPin); - if(readValue>maxValue){ - maxValue = readValue; - } - if(readValue500){ - display.println("\nStatus:Not Okay"); - } - else{ - display.println("\nStatus:OK"); - } - display.display(); - delay(20); - - -}