#include <ACS712.h>
// Define pins
const int relayPin = 7;
const int voltagePin = A0;
const int currentPin = A1;
const float voltageCalibration = 11.0; // Adjust based on your voltage sensor
const float currentCalibration = 30.0; // Adjust based on your current sensor
// Initialize current sensor
ACS712 sensor(ACS712_30A, A1);
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Start with the relay off
sensor.calibrate();
}
void loop() {
// Read voltage
int voltageReading = analogRead(voltagePin);
float voltage = voltageReading * (voltageCalibration / 1023.0);
// Read current
float current = sensor.getCurrentAC();
// Calculate power
float power = voltage * current;
// Print values
Serial.print("Voltage: "); Serial.print(voltage); Serial.print(" V, ");
Serial.print("Current: "); Serial.print(current); Serial.print(" A, ");
Serial.print("Power: "); Serial.print(power); Serial.println(" W");
// Control relay based on power consumption
float powerThreshold = 100.0; // Set your power threshold here in watts
if (power > powerThreshold) {
digitalWrite(relayPin, LOW); // Turn off appliance
Serial.println("Appliance turned off due to high power consumption");
} else {
digitalWrite(relayPin, HIGH); // Turn on appliance
}
delay(1000); // Adjust delay as needed
}