A simple voltage divider(5:1 i.e. for 5 Volts of input the output should be 1 Volt) is used to Keep the measuring voltage range i.e 0-30 volt within Arduino's ADC input voltage range, i.e. 0-5 Volt. Another Resistance of 1 Ohm is used to drop the voltage across it, which is proportional to the current through it. This voltage across the 1 Ohm resistor is measured by the ADC of the Arduino.
(Note: This is not a very reliable setup, as the current is limited to a maximum of 5 Ampere Theoretically, however in practice if your ammeter will drop about 5 Volt then of course the voltage source won't be very helpful. But for the small current circuit, it can be implemented very well.
#include "LiquidCrystal.h"
LiquidCrystal lcd(6, 5, 7, 4, 3, 2);
int VoltMeterProbe = A4;
int AmmeterProbe = A5;
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Energy Meter ");
lcd.setCursor(0, 1);
lcd.print(" by Debashish ");
delay(3000);
lcd.clear();
}
void loop() {
float VoltMeterReading = analogRead(VoltMeterProbe);
delay(50);
VoltMeterReading = analogRead(VoltMeterProbe);
//float Voltage = map(VoltMeterReading, 0, 1023, 0.00, 20.20);
float Voltage = mapfloat(VoltMeterReading, 0, 1023, 0.00, 20.20);
float AmmeterReading = analogRead(AmmeterProbe);
delay(50);
AmmeterReading = analogRead(AmmeterProbe);
//float Current = map(AmmeterReading, 0, 1023, 0, 5000);
float Current = mapfloat(AmmeterReading, 0, 1023, 0, 5000);
lcd.setCursor(0, 0);
lcd.print("Volts: ");
lcd.setCursor(7, 0);
lcd.print(" ");
lcd.setCursor(7, 0);
lcd.print(Voltage);
lcd.setCursor(14, 0);
lcd.print("V");
lcd.setCursor(0, 1);
lcd.print("Amps: ");
lcd.setCursor(6, 1);
lcd.print(" ");
lcd.setCursor(6, 1);
lcd.print(Current);
lcd.setCursor(14, 1);
lcd.print("mA");
delay(900);
}
//A floating Point Map function
float mapfloat(long x, long in_min, long in_max, long out_min, long out_max)
{
return (float)(x - in_min) * (out_max - out_min) / (float)(in_max - in_min) + out_min;
}