/* DC wattmeter by GCSCREATIONS */ #include #include // Set the LCD address to 0x3F for a 16 chars and 2 line display,Address may vary,find out your address. LiquidCrystal_I2C lcd(0x3F, 16, 2); // end of settings for LCD1602 with I2C /* #include LiquidCrystal lcd(2, 3, 4, 5, 6, 7);*///if you are using without I2C module int analogInput1 = 1;//For measuring voltage int analogInput2=2;//For measuring current float vout = 0.0;//output from voltage divider circuit. float vin = 0.0;//Actual value of voltage float R1 = 220000.0; // resistance of R1 (220K) float R2 = 100000.0; // resistance of R2 (100K) double current=0;//Actual value of current double rawvoltage=0;//output from current sensor float power=0;//For calculating power void setup() { Serial.begin(9600); pinMode(analogInput1, INPUT); pinMode(analogInput2,INPUT); lcd.begin(); lcd.setCursor(2, 0); lcd.print("DC WATTMETER"); lcd.setCursor(2, 1); lcd.print("0-16V/0-20A"); delay(2000); lcd.clear(); } void loop() //--------------------------------------------------------------------------------Voltage measurement { for(int j=0;j<1000;j++) { vout = (vout +(analogRead(analogInput1)*5.0)/1024); delay(0.2); } vout=vout/1000; vin = vout / (R2 / (R1 + R2)); vin=vin+0.2;// For calibration,you can change this value according to your voltage deviation. Serial.println(vin); if (vin <= 0.4) { vin = 0.0; } //------------------------------------------------------------------------------------Current measurement for(int i = 0; i < 1000; i++) { rawvoltage = (rawvoltage + (analogRead(analogInput2)*5.0)/1024); // (5 V / 1024 (Analog) = 0.0049) which converter Measured analog input voltage to 5 V Range delay(0.8); } rawvoltage = rawvoltage /1000; current = (rawvoltage - 2.49)/ 0.100; // Sensed voltage is converted to current current=current-0.025;//For calibration,you can change this value according to your current deviation. if(current<0.00) { current=0; } else { current=current+0.00;//For calibration,you can change this value according to your current deviation. } //-------------------------------------------------------------------------------------Calculating power power=vin*current;//For calculating power. //-------------------------------------------------------------------------------------Displaying parameters lcd.setCursor(0,0); lcd.print("V:"); lcd.print(vin,2);//Displaying Voltage. lcd.setCursor(7,0); lcd.print("V"); /*lcd.setCursor(9,0); lcd.print("v:"); lcd.print(vout,2);*///For troubleshooting the output from the voltage divider circuit. lcd.setCursor(0,1); lcd.print("I:"); lcd.print(abs(current),3);//Displaying Current. lcd.print("A"); /*lcd.setCursor(9,0); lcd.print("i:"); lcd.print(rawvoltage,2);*///For troubleshooting the output from the current sensor. lcd.setCursor(9,1); lcd.print("P:"); lcd.print(power);//Displaying Power lcd.print("W"); delay(1000); }