Am I dooing it right? Outputting dc voltage on a LCD

Hello fellow Arduino’s.

This is my first post here.

I have mad a DC generator with a output @ 30V DC and max 1.5A AMP. I have the made a Voltage divider outputting 5V to be handled by the analog pin A0 on my arduino board (freeduino V1.6 W. atmega 168 chip)

My question is, well it’s more a sort of confirmation than a question, as follow:
A’m I dooing it right?
Can I in any way “upgrade” my code?

my code, so far, is as follows… And any comments is more that welcome :slight_smile:

#define NUM_SAMPLES 10
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x63,20,4);


char buffer[10];
char buffer_amp[10];
int sum = 0;                    
unsigned char sample_count = 0; 
float voltage = 0.0000000;      

void gotoXY(int x, int y)
{
  Wire.beginTransmission(0x63);
  Wire.write(1);
  Wire.write(3);
  Wire.write(x);
  Wire.write(y);
  Wire.endTransmission();
}

void setup()
{
  Wire.begin();
  Wire.beginTransmission(0x63);
  Wire.write(1);
  Wire.write(12);
  Wire.write(19);
  Wire.write(4);
  Wire.endTransmission();

  Serial.begin(9600);

}

void loop()
{
    // take a number of analog samples and add them up
    while (sample_count < NUM_SAMPLES) {
        sum += analogRead(A0);
        sample_count++;
        delay(10);
    }
    
    voltage = ((float)sum / (float)NUM_SAMPLES * 5.00) / 1024.0 * 9.56;     
 
    itoa(voltage, buffer, 10);                                              // takes the float string and converts it to a char string
    
    Serial.print(voltage);
    Serial.println (" Volt");
    sample_count = 0;
    sum = 0;

    gotoXY(1,1);
    Wire.beginTransmission(0x63);
    Wire.write(1);
    Wire.write("Str");
    Wire.write(242);
    Wire.write("mforsynning M14");
    Wire.endTransmission();


    gotoXY(2,1);
    Wire.beginTransmission(0x63);
    Wire.write(1);
    Wire.write(buffer);
    Wire.write(" Volt");
    Wire.endTransmission();

    
    delay(300);
}

I have looked around the internet, getting hints and tricks for my code. and @ the time of writing. The voltage is beeing displayed at the lcd diplay. But It’s kind of bouncing between 3 numbers all the time. for example if I set the DC output that, lets say, 25V DC the display could show 23,24,23,25,24,23… and so on

hope I have’nt left something out, if so, just ask. As I said this is my firts time posting here :wink:

Best regards
Frederik from Denmark :slight_smile:

The 5 should be in the numerator not the denominator of the calculation. and I don't know what the 9.54 is for.

Oh sorry :slight_smile:
The 9,56 is the calibreted voltage divided value, but when I put in 5 (as in 5V) It did not work. So this number is what I ended up with, by just trying.

  • Frederik

What value resistors are you using for your divider?