#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
#define VOLTAGE_IN_PIN A0
#define CURRENT_IN_PIN A1
// Floats for ADC voltage & Input voltage
float current_adc_voltage,voltage_adc_voltage = 0.0;
float in_voltage,in_current = 0.0;
// Floats for resistor values in divider (in ohms)
float R1 = 30000.0;
float R2 = 7500.0;
// Float for Reference Voltage
float ref_voltage = 5.0;
// Integer for ADC value
int adc_value_current,adc_value_voltage = 0;
void setup()
{
// Setup Serial Monitor
Serial.begin(9600);
Serial.println("DC Voltage Test");
int status;
status = lcd.begin(LCD_COLS, LCD_ROWS);
if (status) // non zero status means it was unsuccesful
{
hd44780::fatalError(status); // does not return
}
// Print a message to the LCD
lcd.setCursor(0, 0); //Set cursor to character 2 on line 0
lcd.print("WELCOME");
lcd.setCursor(0, 1); //Move cursor to character 2 on line 1
lcd.print("POWER MNGR.");
delay(2000);
lcd.clear();
}
int c=0;
float totalPower, power, powerHour=0;
unsigned long last_time,time;
void loop(){
// Read the Analog Input
adc_value_voltage = analogRead(VOLTAGE_IN_PIN);
delay(500);
unsigned int x=0;
float Samples=0.0;
for (int x = 0; x < 150; x++){ //Get 150 samples
adc_value_current = analogRead(CURRENT_IN_PIN); //Read current sensor values
Samples = Samples + adc_value_current; //Add samples together
delay (3); // let ADC settle before next sample 3ms
}
adc_value_current=Samples/150.0;//Taking Average of Samples
// Determine voltage at ADC input
current_adc_voltage = (adc_value_current * ref_voltage) / 1024.0;
voltage_adc_voltage = (adc_value_voltage * ref_voltage) / 1024.0;
// Calculate voltage at divider input
in_voltage = voltage_adc_voltage / (R2/(R1+R2)) ;
in_current = (current_adc_voltage -(25/10)) / 0.066;
if (in_current < 0.16) {
in_current = 0;
}
// Print results to Serial Monitor to 2 decimal places
Serial.print("Input Voltage = ");
Serial.println(in_voltage, 2);
Serial.println(adc_value_voltage);
Serial.println(voltage_adc_voltage);
// Short delay
delay(500);
lcd.clear();
lcd.setCursor(0, 0); //Set cursor to character 2 on line 0
lcd.print("V:");
lcd.print(in_voltage);
lcd.print(" I:");
lcd.print(in_current);
power=in_voltage*in_current;
if (c=0 && power>0.1){
last_time=millis();
c=1;
}
if (power<0.1){
c=0;
}
lcd.setCursor(0, 1); //Set cursor to character 2 on line 0
power=in_voltage*in_current;
lcd.print(power);
lcd.print("W ");
if(c=1){
totalPower=power+totalPower;
time=millis()-last_time;
time = (time/1000);
Serial.print("Time Passed:");
Serial.println(time);
powerHour =totalPower*time;
lcd.print(powerHour);
lcd.print("Wh");
}
// Print results to Serial Monitor to 2 decimal places
Serial.print("Input Current = ");
Serial.println(in_current, 2);
Serial.println(adc_value_current);
Serial.println(current_adc_voltage);
// Short delay
delay(500);
Serial.print("Power = ");
Serial.println(power, 2);
Serial.print("Power Hour = ");
Serial.println(powerHour, 2);
}Voltage Sensor Simulator
Current Sensor Simulator