#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <LiquidCrystal_I2C.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define bitmap_height   128
#define bitmap_width    64

static const unsigned char PROGMEM bitmap[] =
{ 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x06, 0x00, 0x0C, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xC3, 0xFF, 0xFE, 0x40, 0x00, 0x00,
0x00, 0x00, 0x06, 0x00, 0x1C, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0xC3, 0xFF, 0xFE, 0x40, 0x00, 0x00,
0x00, 0x00, 0x0E, 0x00, 0x1C, 0x00, 0x30, 0x00, 0xC0, 0x00, 0x46, 0x00, 0x00, 0x40, 0x00, 0x00,
0x00, 0x00, 0x1E, 0x00, 0x3C, 0x00, 0x78, 0x00, 0xC0, 0x00, 0x46, 0x00, 0x00, 0x40, 0x00, 0x00,
0x00, 0x00, 0x32, 0x00, 0x64, 0x00, 0x48, 0x00, 0xC0, 0x00, 0x46, 0x00, 0x00, 0x40, 0x00, 0x00,
0x00, 0x00, 0x62, 0x00, 0xC4, 0x00, 0xCC, 0x00, 0xC0, 0x00, 0x46, 0x00, 0x00, 0x40, 0x00, 0x00,
0x00, 0x00, 0xC2, 0x01, 0x84, 0x00, 0x86, 0x00, 0xC0, 0x00, 0x46, 0x00, 0x00, 0x40, 0x00, 0x00,
0x00, 0x01, 0xC2, 0x03, 0x04, 0x01, 0x86, 0x00, 0xC0, 0x00, 0x46, 0x00, 0x00, 0x40, 0x00, 0x00,
0x00, 0x01, 0x82, 0x07, 0x04, 0x01, 0x03, 0x00, 0xC0, 0x00, 0xC6, 0x00, 0x00, 0x40, 0x00, 0x00,
0x00, 0x03, 0x02, 0x0E, 0x04, 0x03, 0x01, 0x00, 0xC0, 0x01, 0x86, 0x00, 0x00, 0x40, 0x00, 0x00,
0x00, 0x06, 0x02, 0x1C, 0x04, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x40, 0x00, 0x00,
0x00, 0x0C, 0x02, 0x18, 0x04, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x40, 0x00, 0x00,
0x00, 0x18, 0x02, 0x30, 0x04, 0x0C, 0x00, 0xC0, 0xC0, 0x30, 0x06, 0x00, 0x00, 0x40, 0x00, 0x00,
0x00, 0x30, 0x02, 0x60, 0x04, 0x08, 0x00, 0x40, 0xC0, 0x18, 0x06, 0x00, 0x00, 0x40, 0x00, 0x10,
0x00, 0x20, 0x03, 0xC0, 0x04, 0x18, 0x00, 0x60, 0xC0, 0x0C, 0x06, 0x00, 0x00, 0x40, 0x00, 0x10,
0x00, 0x60, 0x03, 0x80, 0x04, 0x10, 0x00, 0x30, 0xC0, 0x06, 0x06, 0x00, 0x00, 0x40, 0x00, 0x10,
0x00, 0xC0, 0x03, 0x00, 0x04, 0x30, 0x00, 0x10, 0xC0, 0x03, 0x06, 0x00, 0x00, 0x40, 0x00, 0x10,
0x01, 0x80, 0x02, 0x00, 0x04, 0x60, 0x00, 0x18, 0xC0, 0x01, 0x86, 0x00, 0x00, 0x40, 0x00, 0x10,
0x03, 0x00, 0x00, 0x00, 0x04, 0x40, 0x00, 0x08, 0xC0, 0x00, 0xC6, 0x00, 0x00, 0x40, 0x00, 0x10,
0x06, 0x00, 0x00, 0x00, 0x04, 0xC0, 0x00, 0x0C, 0xC0, 0x00, 0xC6, 0x00, 0x00, 0x40, 0x00, 0x10,
0x0C, 0x00, 0x00, 0x00, 0x04, 0x80, 0x00, 0x04, 0xC0, 0x00, 0x63, 0xFF, 0xFE, 0x7F, 0xFF, 0xF0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

LiquidCrystal_I2C lcd(0x27, 16, 2);

double I_VAL = 0; //leakage current reading value
double I_SUM = 0;
double I_AVG = 0;
double I_MAX = 0;
double I_TRIP = 30.00;
int I_PIN = A0;
int sensorReading = 0;
int I_MAX_A = 0;
bool a = true;
int prev_condition = 100;
int condition;
int RELAY_PIN = 5;

void showBitmap(void) 
{
display.clearDisplay();
display.drawBitmap(0, 0, bitmap, bitmap_height, bitmap_width, WHITE);
display.display();
delay(1000);
}

void setup() {
  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
  lcd.init();                      // initialize the lcd
  lcd.backlight();
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  // Clear the buffer
  display.clearDisplay();
  showBitmap();
  lcd.print("Initializing");
  delay(3000);
  display.clearDisplay();
  lcd.clear();
  bool a = 0;
}
void loop() 
{  
  if(a)
  {
    sensorReading = analogRead(I_PIN);

    if (sensorReading >= 0)
    {
      condition = 1;
    }
    if (sensorReading >=102)
    {
      condition = 2;
    }
    if (sensorReading >=348)
    {
      condition = 3;
    }
    if (sensorReading >=920)
    {
      condition = 4;
    }
     switch (condition)
  {
    case 1:
    {
      lcd.clear();
      display.clearDisplay();
      lcd.setCursor(2,0);
      lcd.print("CHECK WIRING");
      lcd.setCursor(2,1);
      lcd.print("CHECK SENSOR");
      display.clearDisplay();
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.print("CHECK WIRING");
      delay(500);
      display.display();
      digitalWrite(RELAY_PIN, HIGH);
      break;
      }
    case 2:
    {
        digitalWrite(RELAY_PIN, LOW);
      
        for (int i = 0; i <= 9; i++)
        {
          sensorReading = analogRead(I_PIN);
          if (sensorReading > I_MAX_A)
          {
            I_MAX_A = sensorReading;
          }
          I_VAL = double(sensorReading) * 0.1222 - 12.517;
          I_SUM += I_VAL; 

        }
        I_MAX = double(I_MAX_A) * 0.1222 - 12.517;
        I_AVG = I_SUM / 10;
        I_SUM = 0;
        delay(5);
        lcd.clear();
        display.clearDisplay();
        lcd.setCursor(0,0);
        lcd.print("Current ");
        lcd.setCursor(8,0);
        lcd.print(I_AVG);
        lcd.setCursor(0,1);
        lcd.print("MAX ");
        lcd.setCursor(4,1);
        lcd.print(I_MAX);
        display.clearDisplay();
        display.setTextSize(2);
        display.setTextColor(WHITE);
        display.setCursor(0, 10);
        display.print("I=");
        display.setCursor(30, 10);
        display.print(I_AVG);
        display.setCursor(90,10);
        display.print("mA");
        display.setCursor(0, 50);
        display.print("Im=");
        display.setCursor(40, 50);
        display.print(I_MAX);
        display.setCursor(100,50);
        display.print("mA");
        display.display();
        delay(500);
        break;
      }
   
    case 3:
    {
      digitalWrite(RELAY_PIN, HIGH);
      lcd.clear();
      display.clearDisplay();
      lcd.setCursor(3,0);
      lcd.print("PROTECTION");
      lcd.setCursor(4,1);
      lcd.print("TRIPPED");
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0, 10);
      display.print("PROTECTION TRIPPED"); 
      display.display();    
      delay(500);
      a = !a;
      break;
      }

    case 4:
    {
      digitalWrite(RELAY_PIN, HIGH);
      lcd.clear();
      display.clearDisplay();
      lcd.setCursor(3,0);
      lcd.print("OVERFLOW");
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0, 10);
      display.print("OVERFLOW"); 
      display.display();
      delay(1000);  
      lcd.clear();
      display.clearDisplay();
      lcd.setCursor(0,0);
      lcd.print("CHECK SENSOR");
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0, 10);
      display.print("CHECK"); 
      display.setCursor(0, 30);
      display.print("SENSOR"); 
      display.display();
    }
      }
  }
    else
    {
      lcd.clear();
      display.clearDisplay();
      lcd.setCursor(2,0);
      lcd.print("RESTART");
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.print("RESTART");
      delay(500);
      display.display();
    }

 
delay(1000);
lcd.clear();

  }
NOCOMNCVCCGNDINLED1PWRRelay Module