# DC voltmeter

Hi
In program #1 = DC Voltmeter with smoothing function is working ok.
I have another program where this lines are related to voltage reading on LCD. program #2
How to change this lines to implement a smoothing function ?

program #1

``````////////////////////////////////////////////////////////
#define NUM_SAMPLES 10

int sum = 0;                    // sum of samples taken
unsigned char sample_count = 0; // current sample number
float vol = 0.0;            // calculated voltage
//////////////////////////////////////////////////////////
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);

void setup() {
pinMode(PB0, INPUT_ANALOG);
lcd.begin(16, 2);
}

void loop()
{

// take a number of analog samples and add them up
while (sample_count < NUM_SAMPLES)
{
sample_count++;
}
{
vin = (vin * 3.3) / 4095.0;

lcd.setCursor(0, 0);
lcd.print(vin);

}
}
``````

program # 2, in void loop

``````  {

vout = (value * 3.3) / 4190.0; // 4200
vin = vout;

}
``````

I declared this part of the first program on the top of the program # 2

``````define NUM_SAMPLES 10

int sum = 0;                    // sum of samples taken
unsigned char sample_count = 0; // current sample number
float vol = 0.0;            // calculated voltage
``````

ted:
In program #1 = DC Voltmeter with smoothing function is working ok.

No, it does not. That is a few lines of nonsense code.

Which Arduino board do you use ?
You can take the average of a few samples. It can be placed in a function to make the sketch better looking.

The average of a few samples for 10-bit ADC of AVR boards can be like this:

``````const unsigned int n = 100;
unsigned long total = 0;
for( unsigned int i=0; i<n; i++)
{
}
total += n / 2; // half bit correction
float voltage = (float) total / (float) n; // average as float, to get extra resolution
voltage = voltage / 1024.0 * 5.0; // convert to voltage
``````

The half-bit correction is explained here: Gammon Forum : Electronics : Microprocessors : ADC conversion on the Arduino (analogRead).

For the Arduino Zero or Arduino Due, it should be "voltage / 4096.0 * 3.3".
You should not change the "4096.0", because that is the number of steps of a 12-bit ADC.
You can change the 3.3 if you know the actual voltage of the 3.3 Volt.

Is this for a ESP8266 ? Then you might need an extra factor for the resistors (if there is a voltage divider at the analog input).

Koepel:
No, it does not. That is a few lines of nonsense code.

Sorry I forgot add , the pin names are for stm32 and Voltmeter is working as should.

This is what I did, it is working, but smoothing effect is not that good as in program # 1

``````    while (sample_count < NUM_SAMPLES)
{
sample_count++;
}
{
vout = (value * 3.3) / 4190.0; // 4200
vin = vout;

}
``````

Please just post the entire sketch - snippets are almost invariably useless out of context for troubleshooting...

The second program is over 400 lines , it is doesn't fit

ted:
The second program is over 400 lines , it is doesn't fit

Then post a small but complete program that illustrates the problem.

Sorry I forgot add , the pin names are for stm32 and Voltmeter is working as should

How come you always forget the details of the board/processor that you are using ?

This is a longer piece,

``````//////////////////////////////////
void loop()
{

{
Vo = vin;
}
else
/*
{

///////////////////////////////////////////////////////////
vout = (value * 3.3) / 4190.0; // 4200
vin = vout;

}
*/
//////////////////////////////////////////

// take a number of analog samples and add them up
while (sample_count < NUM_SAMPLES)
{
sample_count++;
}
{
vout = (value * 3.3) / 4190.0; // 4200
vin = vout;

}
/////////////////////////////////////////

{
Uo =  emon1.Irms;
}
else
{
}

//emon1.calcVI(20, 2000);
emon1.calcVI(20, 2);
emon1.serialprint();

float Irms = emon1.Irms;

lcd.setCursor(0, 0);
//lcd.print("V");
lcd.print(vin);
lcd.setCursor(5, 0);      //aktualne napiecie
lcd.print("o");           // = Vo , wartosc napiecia w nonecie naciscnecia
lcd.print(Vo, 2);
lcd.setCursor(11, 0);
lcd.print (vin - Vo);      // delta napiecie

lcd.setCursor(0, 1);
lcd.print(Irms);
lcd.setCursor(5, 1);
lcd.print("o");
lcd.print(Uo);
lcd.setCursor(11, 1);
lcd.print (Irms - Uo);
``````

I don't see where you zero "sum"

Post # 0

"I declared this part of the first program on the top of the program # 2"

``````/////////////////////////////////////////////////
#define NUM_SAMPLES 20

int sum = 0;                    // sum of samples taken
unsigned char sample_count = 0; // current sample number
float vol = 0.0;            // calculated voltage
////////////////////////////////////////////////
#define SAMPLES 78
#define DEBOUNCE_DELAY 10
#define DEBOUNCE_IDLE 0
#define DEBOUNCE_ACTIVE 1

#include <libmaple/dma.h>
#include "EmonLib.h"
#include <math.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>
``````

Where do you subsequently zero "sum"?

Maybe it is in the code you didn't post.

Maybe you should stop wasting people's time.

int sum = 0;

That what I have in program #1 which is working ok.
The same line is in program #2.
I have it in the same place as in program #1

It will "work" for one iteration.

Is that your definition of "working ok"?

I just want to ensure we're singing from the same hymn-sheet.

The numbers on LCD are stable

ted:
The numbers on LCD are stable

Because they're only calculated once, if they're calculated at all

but when I am changing input voltage the numbers are changing to = the reading is proper and stable.

Ok, I'm done here.

In program #1 = DC Voltmeter with smoothing function is working ok.

If I pointed out that there is no smoothing function in the first sketch in the original post, would you believe me? I suspect not, but it is true.

You're beyond help.

This is the origin of the DC voltmeter to which I added smoothing function, the numbers on LCD are jumping.

int volt;
int i;
//int volt, avolt[50];
//int LED = PB14;
////////////////////////////
const int ResetPin = PA7; // resets Tmin and Tmax both to current temperature T
//int volt = 0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
void setup() {
pinMode(PB0, INPUT_ANALOG);
pinMode(ResetPin, INPUT);
// pinMode(PA7, INPUT_ANALOG);
// pinMode(PB14, OUTPUT);
lcd.begin(16, 2);
}

void loop() {
volt = (volt * 3.3) / 4095.0;
lcd.setCursor(0, 0);
lcd.print(volt);
delay(100);
/*
for (int i = 0; i < 50; i++)

//if (volt >= 1000 && volt <= 1300)
//if (volt <= && volt >= )
if(volt <= 1.20 && volt >= 1.00)
{
digitalWrite(PB14, HIGH);
LED = 1;

}
else
{
digitalWrite(PB14, LOW);
LED = 0;

}
*/
}

1400+ posts, and no code tags? WTF?

Two variables called "volt"?

There's no smoothing in that code either, not even the junk in the comment.

Smoothing is simple arithmetic - sum the last n readings and divide by n.

This is Jackson Pollack programming.
Go away and work through some programming examples, you're wasting our time.
If you want someone to write you code, post in Gigs.