How to track the consumption of a system in wH ? Wattmeter

Hello i would like to make a wattmeter, an energy meter which will show me the consumption in wH of my system ( lamp, heating system of a little project )

So i would like to have the measurement of the consumption every X seconds or something like that, that will show me at the end of the day my whole consumption .

materials i have : current sensor : 185 mv/A , voltage divider

So to do that, i spent so many hours trying to make the program, but it didn't work :frowning:

I tried to use the formula, P = U*I because i use the current sensor and the voltage divider ...

But i'm still failling ...

I hope you could help me :frowning:

Thanks

/************************************************************
 Horloge Arduino
 
 Horloge simple avec un Arduino, un module breakout 
 RTC DS1307 et un afficheur LCD.
 
 Branchements du breakout RTC DS1307:
 Gnd --> GND
 Vcc --> 5 V
 Sda --> analog pin A4
 Scl --> analog pin A5
  
 Branchements de l'afficheur LCD:
 LCD RS pin --> digital pin 12
 LCD Enable pin  --> digital pin 11
 LCD D4 pin  --> digital pin 5
 LCD D5 pin  --> digital pin 4
 LCD D6 pin  --> digital pin 3
 LCD D7 pin  --> digital pin 2
 LCD R/W pin  --> ground

 
 ***************************************************/

#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 float Voltage = 0;
 float Current = 0;
 float Power = 0;
 float volts = 0;
 float amps = 0;
 int pot = A1;
 int pot2 = A0;
 float wattHours =0;
 float kwattHours =0;
void setup() 
{
  Wire.begin();
  RTC.begin();
  lcd.begin(16, 2);
  pinMode(pot, INPUT);
}

void loop() 
{
  DateTime now = RTC.now();
  lcd.setCursor(2, 0);
  lcd.print(now.hour());
  lcd.print(":");
  if (now.minute() < 10) 
  {
    lcd.print("0");
  }
  lcd.print(now.minute());
  lcd.print(":");
  if (now.second() < 10) 
  {
    lcd.print("0");
  }
  lcd.print(now.second());
  
  
  // la date sur la deuxième ligne du LCD
  
  lcd.setCursor(1, 0); 
  lcd.print(now.day()); 
  lcd.print(" "); 

  
  switch (now.month()) 
  {
  case 1:
    lcd.print("janvier");
    break;
  case 2:
    lcd.print("fevrier");
    break;
  case 3:
    lcd.print("mars");
    break;
  case 4:
    lcd.print("avril");
    break;
  case 5:
    lcd.print("mai");
    break;
  case 6:
    lcd.print("juin");
    break;
  case 7:
    lcd.print("juillet");
    break;
  case 8:
    lcd.print("aout");
    break;
  case 9:
    lcd.print("septembre");
    break;
  case 10:
    lcd.print("octobre");
    break;
  case 11:
    lcd.print("novembre");
    break;
  case 12:
    lcd.print("decembre");
    break;
  }
  lcd.print(" "); 
  lcd.print(now.year()); 
  
   Current = (analogRead(pot)*(5.0 / 1023.0));
  
   Voltage = analogRead(pot);
   volts =  Voltage * (5.0 / 1023.0);
     
   Power = Current * Voltage;
   wattHours = Power / 3600 ;
   
   
  
   lcd.setCursor(0,2);
   lcd.print("Consommation en Wh  =  ");
   lcd.print(analogRead(pot)); 
   lcd.print(wattHours);
   lcd.print("Wh");
  
  
  
  
 
   
  delay(1000);
  lcd.clear();
  
}

The code does something. You forgot to tell us what it actually does.

You expect the code to do something. You forgot to tell us what you expect.

PaulS:
The code does something. You forgot to tell us what it actually does.

You expect the code to do something. You forgot to tell us what you expect.

Apologies, First of all my code has to measure the power in watts of my system, by using the formula P = Voltage * Intensity,

The intensity is measured by the voltage sensor, and in the schema you can see i simulated the sensor voltage with a voltmeter by using 3 resistances.

The Voltage is measured by the voltage divider and you can see it in the schema .

What the code should do ( but not working well ) it should measure the Power of the system, and then convert it to Wh to know the consumption of the system.

I except to measure the consumption of my system 24h/24 ... and display it on the lcd screen

Thanks

So … does your LCD come on at all?
Does it freeze?
Does the measurement always come up as zero?
Does the measurement come up, but it's the wrong number?

PaulMurrayCbr:
So … does your LCD come on at all?
Does it freeze?
Does the measurement always come up as zero?
Does the measurement come up, but it's the wrong number?

The measurement comes up but its the wrong number ..

And the measurement is not really automatic because i have to press the potentiometer to get a number ...

First of all my code has to measure the power in watts of my system, by using the formula P = Voltage * Intensity,

You can not measure power. You calculate power.

Put the code for showing the date and time on the LCD in a function. Wading past so many lines in loop() showing the date and time makes me forget what the hell I'm looking for.

   Current = (analogRead(pot)*(5.0 / 1023.0));
 
   Voltage = analogRead(pot);

You are reading the same pin twice, and thinking you got two different things. You did not.

Ok thanks how should i do to read the other pin to get the voltage and the current ?please

Ok thanks how should i do to read the other pin to get the voltage and the current ?

First, you decide which pin you are going to connect the voltage source to. Then, you use analogRead() to read that pin, and do the computation, and store the result in a variable named Voltage (not Current, as you do now).

Then, you decide which pin you are going to connect the current sensor to. Then, you use analogRead() to read that pins, and store the result in a variable named Current. Assuming, of course, that the current sensor output is in Amps. If not, do whatever calculations are needed to convert the raw reading to Amps.

PaulS:
First, you decide which pin you are going to connect the voltage source to. Then, you use analogRead() to read that pin, and do the computation, and store the result in a variable named Voltage (not Current, as you do now).

Then, you decide which pin you are going to connect the current sensor to. Then, you use analogRead() to read that pins, and store the result in a variable named Current. Assuming, of course, that the current sensor output is in Amps. If not, do whatever calculations are needed to convert the raw reading to Amps.

Ok : First step here are the pins : Volts = 1;
Amps = 0;
Watts = 3;

Second step : analogRead(Volts)
(read that pin) Voltage = Volts * (5/1023)

analogRead(Amps)
Current = Amps * (5/1023)

analogRead(Watts)
Power = Voltage * Current

Power * 3600

is it alright please ?

My new program :

 /************************************************************
 Horloge Arduino
 
 Horloge simple avec un Arduino, un module breakout 
 RTC DS1307 et un afficheur LCD.
 
 Branchements du breakout RTC DS1307:
 Gnd --> GND
 Vcc --> 5 V
 Sda --> analog pin A4
 Scl --> analog pin A5
  
 Branchements de l'afficheur LCD:
 LCD RS pin --> digital pin 12
 LCD Enable pin  --> digital pin 11
 LCD D4 pin  --> digital pin 5
 LCD D5 pin  --> digital pin 4
 LCD D6 pin  --> digital pin 3
 LCD D7 pin  --> digital pin 2
 LCD R/W pin  --> ground

 
 ***************************************************/

#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
#include <SPI.h>
#include <SD.h>
RTC_DS1307 RTC;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 int Consumption = 1;
 int pot = A2;
 float Voltage = 0;
 float Current = 2;
 float Power = 3;
 float Watts = 6;
 float Volts = 4;
 float Amps = 5;
 int pot = A1;
 float Wattshours =7;
 File fichierSD;
void setup() 
{
  Wire.begin();
  RTC.begin();
  lcd.begin(16, 2);
  pinMode(pot, INPUT);
   if(!SD.begin(4)) {
    Serial.println(F("Initialisation impossible !"));
    return;
  }
  Serial.println(F("Initialisation OK"));
}

void loop() 
{

  {
   fichierSD = SD.open("analog.txt", FILE_WRITE);
  
  
    //Test pour écriture
    if(fichierSD) {
    Serial.println(F("Ecriture en cours"));
    //Ecriture
    fichierSD.println(sensorValue);
    fichierSD.close();
  }
  
  delay(1000);
  
  DateTime now = RTC.now();
  
  
  
  // la date sur la deuxième ligne du LCD
  
  lcd.setCursor(1, 0); 
  lcd.print(now.day()); 
  lcd.print(" "); 

  
  switch (now.month()) 
  {
  case 1:
    lcd.print("janvier");
    break;
  case 2:
    lcd.print("fevrier");
    break;
  case 3:
    lcd.print("mars");
    break;
  case 4:
    lcd.print("avril");
    break;
  case 5:
    lcd.print("mai");
    break;
  case 6:
    lcd.print("juin");
    break;
  case 7:
    lcd.print("juillet");
    break;
  case 8:
    lcd.print("aout");
    break;
  case 9:
    lcd.print("septembre");
    break;
  case 10:
    lcd.print("octobre");
    break;
  case 11:
    lcd.print("novembre");
    break;
  case 12:
    lcd.print("decembre");
    break;
  }
  lcd.print(" "); 
  lcd.print(now.year()); 
  
 
   
   analogRead(Volts)
   Volts = Voltage * (5/1023));
   
   analogRead(Amps)
   Amps = Current * (5/1023));
   
   analogRead(Watts)
   Watts = Voltage * Current;
  
   Watthours =  Watts * 3600;
  
   
   lcd.setCursor(0,1);
   lcd.print("Consumption in Wh  =  ");
   lcd.print("Wh");
   
 
  
  delay(1000);
  lcd.clear();
}

doesn't seem to work :frowning:

 float Voltage = 0;
 float Current = 2;

Strange initial values...

void loop()
{

  {
   fichierSD = SD.open("analog.txt", FILE_WRITE);
 
 
    //Test pour écriture
    if(fichierSD) {
    Serial.println(F("Ecriture en cours"));
    //Ecriture
    fichierSD.println(sensorValue);
    fichierSD.close();

On every pass through loop(), open the file and write the value of sensorValue to the file, EVEN THOUGH YOU HAVEN'T READ ANY SENSOR. Why?

Still all that code in loop() to write the data to the LCD that we have to wade past...

   analogRead(Volts)
   Volts = Voltage * (5/1023));

Read from analog pin 4, and throw the results away. Then, assign a new pin number to read from next time.

By the way, what is 5/1023? ANY answer other than 0 is wrong. 5.0/1023 is not 0. 5/1023.0 is not 0. 5/1023 IS 0.

   analogRead(Watts)
   Watts = Voltage * Current;

Read from the Watts pin, which makes no sense, and throw the results away.

Then, assign a new value for the pin to read next time, of 2 * 0.

May I suggest that you use Pin in the names of variables that are to hold pin numbers, and that you add the const keyword to the declaration of those variables, so that you don't crap on them.

May I suggest that you count the number of sensors you have - one to measure voltage and one to measure current is TWO, not three.

May I suggest that you RTFM, and learn how to use analogRead().

May I suggest that you develop a very basic understanding of electricity, and what voltage, current, and power mean BEFORE you hurt yourself.

That was harsh ....

Is it better ?

      int valuevoltage = A1;
      int valuecurrent = A2;
      int pinvoltage = 0;
      int pincurrent =0;





        {
        valuevoltage = analogRead(pinvoltage)                // the value of the voltage divider 
   
        valuecurrent = analogRead(pintcurrent)                // the value of the current sensor 
  
   
       Watts =  valuevoltage * valuecurrent;
  
       Watthours =  Watts * 3600;

        }

Is it better ?

No. The pin numbers are A1 and A2. They should be stored in const int variables with pin in the name.

const int voltagePin = A1;
const int currentPin = A2;

The values read from the pins should be stored in variables with value in the name.

int voltageValue = analogRead(voltagePin);
int currentValue = analogRead(currentPin);

Notice that these variables are NOT global. Not every variable needs to be global.

Note to that I use camelCase for the names, just like all the Arduino functions.

Ok thanks, so now if use the formula it should be right is not it ?

Or do i have to use Analogwrite ?

Ok thanks, so now if use the formula it should be right is not it ?

The formula assumes that the values are in volts and amps. I'm not convinced that the current sensor outputs a value in amps. I'd actually be surprised if it did. I can't imagine being able to measure more than 1000 amps with it.

Or do i have to use Analogwrite ?

For what?

PaulS:
The formula assumes that the values are in volts and amps. I'm not convinced that the current sensor outputs a value in amps. I'd actually be surprised if it did. I can't imagine being able to measure more than 1000 amps with it.
For what?

int voltageValue = analogRead(voltagePin);
int currentValue = analogRead(currentPin);

So now i can do , Power = Current * Voltage ????

Yes it's amps and it's not meant to measure more than 1000 amps, it's more like 20 amps