I am planning to create a device (that is kinda like an adapter) that monitors the voltage used by an appliance and cuts off the power (which turns off the appliance) automatically when the voltage consumption of the said appliance exceeds a certain value.
I am very new to Arduino so I wanted to have at least a rough plan (and code) first before actually buying the materials for this project. I stumbled upon a video by Bas on Tech (VOLTAGE SENSOR (0-25V) - Arduino tutorial #17 - YouTube), I copied the code he used but modified some parts. I just wanted to ask if this would theoretically work.
Here is the code
float vIn; // measured voltage (3.3V = max. 16.5V, 5V = max 25V)
float vOut;
float voltageSensorVal; // value on pin A3 (0 - 1023)
const float factor = 5.128; // reduction factor of the Voltage Sensor shield
const float vCC = 5.00; // Arduino input voltage (measurable by voltmeter)
float totV = 0; // Where the total voltage value will be stored
void setup() {
Serial.begin(9600);
}
void loop() {
voltageSensorVal = analogRead(voltageSensorPin); // read the current sensor value (0 - 1023)
vOut = (voltageSensorVal / 1024) * vCC; // convert the value to the real voltage on the analog pin
vIn = vOut * factor; // convert the voltage on the source by multiplying with the factor
totV += vIn;
Serial.print("Current Voltage = ");
Serial.print(vIn);
Serial.println("V\n");
Serial.println("Total Voltage = ");
Serial.println(totV);
Serial.println("V");
if (totV >= 120) {
cut();
}
delay(1000);
}
void cut() { // the function to cut power supply
}
His code gives off a continuous stream of Voltage values so I created a variable totV to store and add up those values. And if it reaches 120 (in this example), I would then execute the cut() function which should cut the power. Also, if you have any ideas on ways to cut the current, any help would be appreciated.