# Average of samples for voltage measurement

**URL:** <https://forum.arduino.cc/t/average-of-samples-for-voltage-measurement/1147783>\
**Category:** Programming\
**Created:** [July 13, 2023, 6:07pm UTC](https://forum.arduino.cc/t/average-of-samples-for-voltage-measurement/1147783 "2023-07-13T18:07:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![arham1008](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arham1008/32/908115_2.png) [@arham1008](https://forum.arduino.cc/u/arham1008)\
**Post date:** [July 13, 2023, 6:07pm UTC](https://forum.arduino.cc/t/average-of-samples-for-voltage-measurement/1147783/1 "2023-07-13T18:07:33Z")

</div>

Hey Experts,  
I am trying to measure dc voltage using arduinio. But the values that I'm get are very unstable. How do I take average of values to make them stable.  
Here is the code,

```arduino

int vin;
int input=A3;

void setup()
{Serial.begin(9600);

}
void loop()
{

vin=analogRead(input);
vin=vin/2.046;
Serial.println(vin);
delay(300);

}

```

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 13, 2023, 6:14pm UTC](https://forum.arduino.cc/t/average-of-samples-for-voltage-measurement/1147783/2 "2023-07-13T18:14:00Z")

</div>

Try a digital filter:

```arduino
unsigned int smooth(unsigned int newVal) {
  static unsigned int oldVal = 0;
  unsigned long sum;
    // optimize sum = (oldVal * 3 + newVal) / 4;
    sum = ( (oldVal << 1) + oldVal + newVal) >> 2;
  }
  oldVal = sum;
  return oldVal;
}
...
  // example use
  smoothedValue = smooth( analogRead(A0) );

...

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [July 13, 2023, 6:53pm UTC](https://forum.arduino.cc/t/average-of-samples-for-voltage-measurement/1147783/3 "2023-07-13T18:53:29Z")

</div>

> [@anon57585045](#):
>
> `sum = ( (oldVal << 1) + oldVal + newVal) >> 2;`

This is the same as (with moving the concept to float instead)

```
sum = 0.75 * oldVal + 0.25 * newVal;

```

"a big part of the old value plus a small part of the new value"

where we always have two coefficients which add up to one. 0.75 and 0.25 in the above. @anon57585045 uses integers and can therefor use shifting and adding instead of multiplying, which is slick but obscures the maths a bit.

You can effect the rate at which it will reach a steady agreement with an unchanging input by being more or less aggressive about how much the new influences the old, viz:

```arduino
sum = 0.95 * oldVal + 0.05 * newVal; // slower convergence

```

or

```arduino
sum = 0.7* oldVal + 0.3 * newVal; // more rapid

```

Play with it and watch the output.

a7

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 13, 2023, 8:57pm UTC](https://forum.arduino.cc/t/average-of-samples-for-voltage-measurement/1147783/4 "2023-07-13T20:57:17Z")

</div>

Just be sure the source values are changing, not just the readings due to improper design or layout... if you average those you are basically sweeping dirt under the carpet.

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [July 13, 2023, 10:49pm UTC](https://forum.arduino.cc/t/average-of-samples-for-voltage-measurement/1147783/5 "2023-07-13T22:49:26Z")

</div>

> [@arham1008](#):
>
> `vin=vin/2.046;`

A/D value / 2.046?  
500volt supply?

You should use one of the internal references of the Arduino for voltage measurements.  
How, depends on which Arduino you're using.  
Leo..

---

<div class="post-metadata">

**Author:** ![arham1008](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arham1008/32/908115_2.png) [@arham1008](https://forum.arduino.cc/u/arham1008)\
**Post date:** [July 14, 2023, 2:52am UTC](https://forum.arduino.cc/t/average-of-samples-for-voltage-measurement/1147783/6 "2023-07-14T02:52:17Z")

</div>

Thanks @alto777!!  
Can you please structure in my code as I'm a beginner!!

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [July 14, 2023, 2:29pm UTC](https://forum.arduino.cc/t/average-of-samples-for-voltage-measurement/1147783/7 "2023-07-14T14:29:18Z")

</div>

Sure. This is your sketch (I use the IDE Autoformat tool on it, a habit you should develop), with @anon57585045's idea from post #2 above "structured in".

I did remove a stray closing brace } from that code.

When I tested it, it was more fun when I turned the speed up by using a shorter _delay_(30).

But you are getting a bit ahead of yourself. Please find a learning source and try to pick up the basics. Variable types and the use of functions, maybe after you get a grip on _if_/_else_ statements and _for_ and _while_ loops.

```arduino
int vin;
int input = A3;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  vin = smooth(analogRead(input));
  vin = vin / 2.046;
  Serial.println(vin);
  delay(30);
}

unsigned int smooth(unsigned int newVal) {
  static unsigned int oldVal = 0;
  unsigned long sum;
    // optimize sum = (oldVal * 3 + newVal) / 4;
  sum = ( (oldVal << 1) + oldVal + newVal) >> 2;

  oldVal = sum;
  return oldVal;
}

```

HTH

a7

---

<div class="post-metadata">

**Author:** ![arham1008](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arham1008/32/908115_2.png) [@arham1008](https://forum.arduino.cc/u/arham1008)\
**Post date:** [July 17, 2023, 4:03am UTC](https://forum.arduino.cc/t/average-of-samples-for-voltage-measurement/1147783/8 "2023-07-17T04:03:04Z")

</div>

Thanks mate!!!  
It works wonderfully!!!!  
Thanks for your constant support!!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 13, 2024, 4:03am UTC](https://forum.arduino.cc/t/average-of-samples-for-voltage-measurement/1147783/9 "2024-01-13T04:03:36Z")

</div>

Closed on January 13, 2024, 4:03am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
