SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By Matsume
#196433
Hey Guys,

I've attached my circuit diagram below. I'm trying to build an energy logger which measures both voltage and current, but I've been experiencing some difficulties with measuring both of them.

Both the voltage and current keeps changing. I tried to get an average of the samples but it still doesn't remain constant. Maybe my program is wrong, I'm not sure what's wrong anymore.

I've placed the code below, Please check it out & help me find the main problem with it & the circuit diagram.

Thanks,

Here's my code:
/*
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
*/
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
int volt=A2;

float vin=0,num,input,ana,out;


void setup()
{
// put your setup code here, to run once:
lcd.begin(16, 2);



}

void loop()
{
// put your main code here, to run repeatedly:

voltage();

}
void voltage()
{
lcd.clear();
lcd.print("voltage:");

num = analogRead(A2);
input=(5*num)/1023.0;

lcd.setCursor(10,0);
lcd.print(input);
lcd.setCursor(0,1);
lcd.print("Current:");
ana=analogRead(A0);
lcd.setCursor(10,1);
lcd.print(ana);
delay(500);

}
You do not have the required permissions to view the files attached to this post.
User avatar
By DanV
#196434
Make sure you have a delay after analogRead(A2) - before you analogRead(A0).
The Arduino.cc site says :
"It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second. "

EDIT:
I just noticed that you're measuring current from an AC source.
That's likely to vary in magnitude especially if your filter cap is sized poorly.

And you're measuring voltage from a rectified AC source.
You surely can't expect that to not fluctuate widely without applying some smoothing capacitors.
By Anthony Fremont
#196476
jremington wrote:See the great tutorial on the OpenEnergyMonitor site for other ideas on how to do AC line energy monitoring, safely and properly.
https://learn.openenergymonitor.org/ele ... gy-monitor
Great isn't a big enough word. That looks like a thesis for someone working on a PHD. I spent hours pouring through that. Thanks

Sent from my Moto G (4) using Tapatalk
By Matsume
#196695
Hey guys, Could you please help me out with the following:

1. How do I connect the load(Light-bulb) accurately so I can measure It's Voltage & Current.
2. Is my Current Sensor connected correctly?
3. And lastly but not least, How do I remove the error that keeps occurring when I run the simulation.

Please find attached the Circuit Diagram and the code below.

Thanks.
You do not have the required permissions to view the files attached to this post.