I trying to use the mega256 board as a voltmeter to measure and display voltages between the range of 4.30V to 5.25V's. The output is going to a 16x2. I have searched the web and found some arduino voltmeter projects, however the sketches provided with those projects are having me go in circle as their output is not what is expected it is off by 1V or by 45V s.
I am new to programming so I would like some guidance, advice, and help. What I am using for this project,, is a variable power supply set to 5V's, a dmm, a voltage divider, R1=99700.k, R2=10k. On the audrino I am using pin A15, which a wire from the middle of the voltage divider is plugged into and the other end of the voltage divider I am using it as positive and negative. I have the power supply connected to the breadboard supplying the 5V's and the dmm is connected to the output of the voltage divider.
I setup the power supply to 5.00V's by using the dmm so I know what my starting point is. When I run the sketch below my expected output should be 5.00V but I am getting 4.32V's. As I decrease the voltage, the voltage should be exact as what is on the dmm, however, no matter what I change the output voltage is off by either .08V or sometimes 2V's. I see this on the lcd and the serial monitor.
To check if my logic is correct and to see if I am going though the correct if statements I used the serial monitor. Which brings up a few questions that I have using the serial monitor. What is the difference in using Serial.print() vs. Serial.printLn()? I have searched the web, but was unable to find a good explanation. Also, how can I display a single line of information on the serial monitor? For example, when I use this statement:
"Serial.print(analogRead(analogInput)); Serial.println(" This is A15"); //added for testing of A15"
the output is printed on two lines, instead of one.
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int analogInput = 15; // this is for analog pin A15
const int UNDERVOLTAGE = 4.30; //test lower voltage
const int NOMINAL = 4.85; //s/b this--do we need this?
const int OVERVOLTAGE = 5.25; //test for overvoltage--can we do this?
const int INPUTVOLTAGE = 4.95; //true input voltage
float voltage = 0.0;
float vIn = 0.0;
float R1 = 99700.0; //measured resistor
float R2 = 10000.0; //measured resistor
int lcd_refresh = 500;
int over = 0;
int under = 0;
void setup()
{
pinMode(analogInput, INPUT); //declaration of pin modes--does analog need this??
lcd.clear(); //clr dipslay
lcd.begin(16, 2); //LCD's number of columns and rows:
Serial.begin(9600); //for testing to see what is happening
}
void loop()
{
int sensorValue = analogRead(analogInput); // read input on pin 15
Serial.print(analogRead(analogInput)); Serial.println(" This is A15"); //added for testing of A15
voltage = sensorValue * (INPUTVOLTAGE/ 1023.0); //scales sampled voltage
Serial.print(voltage); Serial.println(" voltage converted"); //print out the value you read:
vIn = voltage * (R2/(R1+R2)); //volt divider= vin * ((R2)/(R1+R2))
if (vIn < UNDERVOLTAGE) // Undervoltage check
{
under = under + 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("V under"); + lcd.print(vIn);
lcd.setCursor(0, 1);
lcd.print(" UV ="); + lcd.print(under);
lcd.print(" OV ="); + lcd.print(over);
delay (lcd_refresh);
//added for testing to see if logic is correct
Serial.print(vIn); Serial.println(" V1"); Serial.println(" V1 only"); //this prn's to see if logic is correct
delay (lcd_refresh);
return;
}
if (vIn > OVERVOLTAGE) // Overvoltage check **ONLY TEST THIS WITH A DIVIDER!!
{
over = over + 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("V OVER"); + lcd.print(vIn);
lcd.setCursor(0, 1);
lcd.print(" UV ="); + lcd.print(under);
lcd.print(" OV ="); + lcd.print(over);
delay (lcd_refresh);
//added for testing
Serial.print(vIn); Serial.println(" VO"); Serial.println(" V2 over"); //this prn's to see if logic is correct
delay (lcd_refresh);
return;
}
else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Nom: "); + lcd.print(vIn);
delay (lcd_refresh);
//added for testing
Serial.print(vIn); Serial.println(" V3"); Serial.println(" V3 Nom"); //this prn's to see if logic is correct
delay (lcd_refresh);
return;
}
}

