0% found this document useful (0 votes)
19 views

Arduino Voltage Sensor1

This document contains code for calibrating and obtaining voltage measurements from an analog sensor. It defines variables for the minimum and maximum sensor readings during calibration, along with the RMS voltage measured by a multimeter. In setup, it calculates the peak voltage from the RMS value. The get_voltage function maps sensor readings to voltages, calculates the RMS over multiple samples, and returns the voltage.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Arduino Voltage Sensor1

This document contains code for calibrating and obtaining voltage measurements from an analog sensor. It defines variables for the minimum and maximum sensor readings during calibration, along with the RMS voltage measured by a multimeter. In setup, it calculates the peak voltage from the RMS value. The get_voltage function maps sensor readings to voltages, calculates the RMS over multiple samples, and returns the voltage.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

// Calibration values obtained from the sketch: volt_ac_cal

int adc_max = 637; // Maximum sensor value during calibration

int adc_min =384; // Minimum sensor value during calibration

float volt_multi = 237; // RMS voltage obtained from a multimeter

float volt_multi_p; // Peak voltage

float volt_multi_n; // Negative peak voltage

void setup() {

Serial.begin(9600);

volt_multi_p = volt_multi * 1.4142; // Peak voltage = RMS voltage * 1.4142 (Single-phase current)

volt_multi_n = -volt_multi_p; // Negative peak voltage

void loop() {

float Grid_Voltage = get_voltage(); // Root Mean Square voltage (V-RMS)

float Eps_Voltage = (Grid_Voltage)-3.00;

float Load_Current = (Grid_Voltage)-5.00;

// Serial.print("Grid_Voltage: ");

Serial.print(Grid_Voltage);

// Serial.print(",");

// Serial.print("Eps_Voltage: ");

// Serial.print(Eps_Voltage);

// Serial.print(",");

// Serial.print("Load_Current: ");

// Serial.print(Load_Current);
Serial.print("\t");

Serial.print("\n");

//Serial.print("Load_Current: ");

//Serial.println(200.00);

//Serial.println(" VAC");

// Delay for a certain interval if needed

//delay(100);

float get_voltage() {

float adc_sample;

float volt_inst = 0;

float sum = 0;

float volt;

long init_time = millis();

int N = 0;

while ((millis() - init_time) < 500) { // Duration of 0.5 seconds (Approximately 30 cycles of 60Hz)

adc_sample = analogRead(A1); // Sensor voltage

volt_inst = map(adc_sample, adc_min, adc_max, volt_multi_n, volt_multi_p);

sum += sq(volt_inst); // Sum of Squares

N++;

delay(1);

//Serial.print("N: ");

//Serial.println(N);

volt = sqrt(sum / N); // RMS equation


return volt;

You might also like