Current and voltage measurement fluctuating

I want to measure current and voltage with my arduino uno rev3 and then calculate watt and watt hour. the whole system id working perfectly exept for the voltage and current that the arduino measures varies by alot(by alot i mean 1-2 amps and 0.5-3 volts) while the actual curent and voltage stays perfectly constant. It is not the sensors i have already checked those for problems it is either in my code or the arduino itself.
I have attached my code.

VAW.ino (3.4 KB)

Do you have a schematic?

Ferdinand, yes, you need to provide more details about your hardware, but from your code I can only assume you are using a Alegro Hall effect device to measure the current, maybe a 712 or 758 type device, and that the device is a bi-directional device, not uni-directional.

Not sure where to start with your code, but it needs a fair amount of work and it would help you if you could document it far better than you have. Even document each line.

But, the code you have as follows;
SA = ((((analogRead(1)) * (5.0 / 1023.0) - 2.5) * 20) * -1);Will give you the problem you are experiencing. The reason is that you are trying to null out the Vcc รท 2 offset of the current sensor, which, when the ADC is hovering around 512 counts, being zero current, will go make your variable of SA, being a double, go negative.

A simple fix would be to do a check of the ADC value, and if it was around 512, then do not null out the offset.
Only null out the offset of 2.5 while the ADC count is greater than 512.

The above is not the best method as you will find that you will in practice get different values for each current sensor that will not be exactly 512, but will instead float around this area due to noise and temperature and your Arduino ADC Vref accuracy.

Also, why do you have delays in your function void calculations() ?
I see no reason for them, avoid using the delay statement, rather, if you need delays or times events, then look at the example 'blink without delay'

Another thing is that you are constantly writing to the LCD in each loop cycle, which may create issues as your program grows.

What size solar system are you working with ?


Paul

Yes i am using a Alegro hall effect sensor.
I agree with adding comment lines i just haven't got round to doping it yet.

Will using the map() function work better: SA = map(analogRead(0), 0, 1023, -50.0, 50.0);

My system is pretty small at the moment but want to expand it. At the moment: Solar panel: 20W
Battery: 54Wh(4.5Ah at 12V)
Inverter: 300W

Thanks Ferdie

Ferdinand wrote:

Yes i am using a Alegro hall effect sensor.

Well, out of the few hundred they have, which one do you have?

Again, I am going to guess, and I guess you have a current sensor that goes from -50 Amps to +50 Amps, ja ?

Please, give as much detail as you can, or it is a hard guessing game for everyone, and I know for me, I quickly give up when I notice this.

Yes, you can use the map command, or you can do it with a few simple statements as well, so you will know how it works.

It doesn't matter if you system is small or large, the same functions apply in terms of having accurate data.
One thing I noticed in your code is that I believe you will not get a valid value for the accumulated Watt hours.
You need a different approach.

I will try to look at what I have.


Paul

It is a -50 to positive 50Amps current sensor.
This is the part number: ACS758LCB-050B-PFF-T

Ferdie

Schematic? Do you have a bypass capacitor on Aref? Are you using the default, which is to use Vcc as Vref? If so, any noise on Vcc shows up in the analog readings.

Ferdinand:
I want to measure current and voltage with my arduino uno rev3 and then calculate watt and watt hour. the whole system id working perfectly exept for the voltage and current that the arduino measures varies by alot(by alot i mean 1-2 amps and 0.5-3 volts) while the actual curent and voltage stays perfectly constant. It is not the sensors i have already checked those for problems it is either in my code or the arduino itself.
I have attached my code.

No, your current and voltage signals are probably very noisy, but when measured with a multimeter
(which integrates over timescales of around 1s) you average out the noise so you think its steady.

Expect a lot of noise on a 50A hall sensor, hall sensors are inherently noisy. Voltage measurement
ought to be better, but we don't know what the load is (switch mode load will make the voltage
bounce about like crazy).

You need to integrate your measurements over time (voltage, current and power separately),
then you can see average values clearly. You may benefit from some low-pass filtering on
the analog inputs to lose the high frequency noise (cutoff freq = sampling freq / 2)

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom..... :slight_smile:
(Sorry I couldn't resist, everybody else has tried.)

I'm doing that in this design

what goes in A0 is a voltage measurement, this is a battery charger actually. For a current measurement you could look at my nanoamp meter It is the same idea, you measure the voltage across a resistor.