Reading analog voltage on serial port for arduino mega 2560 board

I have created adc coding for reading analog voltage varies from 0 to 5volts. I have printed the values on serial monitor. But there are some variation occurs when reading the analog voltage. For example, instead of getting 4.3volts, it is showing 4.1, 4.2, 4.4, 4.3, 4.7 etc continuously.

How to avoid these variations in programming? Pls tell me the solution.

rajansn24:
Pls tell me the solution.

First, please post your program so we have some idea what the problem is.

It is not unusal to get small variations in readings from an ADC. You could write some code that only prints a new value if it differs from the previous value by X

...R

When you measure the voltage with a multimeter (or better yet an oscilloscope) is it completely steady? If so, maybe the connection is bad.

You could use a moving average to reduce the noise. Google can help with that.

Robin2:
First, please post your program so we have some idea what the problem is.

It is not unusal to get small variations in readings from an ADC. You could write some code that only prints a new value if it differs from the previous value by X

...R

I have posted my code here. Pls read it and tell how to avoid voltage variations and how to implement in coding.

#define LCD_CS A3 // Chip Select A3
#define LCD_CD A2 // Command/Data Register Select A2

#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // LCD Reset A4

// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define WHITE 0xFFFF
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define BLUE 0x001F
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0

// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

const int analogInPin0 = A11; // Analog input pin that the potentiometer1 is attached to
const int analogInPin2 = A13; // Analog input pin that the potentiometer2 is attached to
const int analogInPin4 = A15; // Analog input pin that the potentiometer3 is attached to

int sensorValue1 = 0; // value read from the pot1
int sensorValue2 = 0; // value read from the pot2
int sensorValue3 = 0; // value read from the pot3
float battery_voltage;
float temperature = 0.0;
float input_current;
float previous_volt;
float present_volt;
float present_current;
float previous_current;
float previous_temp = 0.0;
float present_temp = 0.0;
float actual_batt_volt;
float offset_voltage = 2.44;
const int R1 = 6900;
const int R2 = 2350;
int mVperAmp = 185;
int ACSoffset = 2500;
float Amps;

void tftlcd_setup()
{
tft.reset(); // reset tft
tft.begin(0x9341); // SDFP5408
tft.setRotation(3); // Need for the Mega, please changed for your choice or rotation initial
tft.fillScreen(BLACK);
tft.setCursor(3, 24);

}

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

float present_temp = 0.0;

tftlcd_setup();

display_homepage();

while(1)
{

sensorValue1 = analogRead(analogInPin0);
sensorValue2 = analogRead(analogInPin2);
sensorValue3 = analogRead(analogInPin4);

Serial.println(sensorValue1);

//Conversion formula for voltage
battery_voltage = (sensorValue1 / 1024.0) * 5.0;

Serial.println(battery_voltage);

actual_batt_volt = (battery_voltage * (R1+R2)) / R2;

Serial.println(actual_batt_volt);

actual_batt_volt = actual_batt_volt - offset_voltage;

//Serial.println(sensorValue2);

//Conversion formula for current
input_current = (sensorValue2 / 1024.0) * 5000; // Gets you mV

//Serial.println(input_current);

Amps = ((input_current - ACSoffset) / mVperAmp);

//Serial.println(Amps);

//Amps = Amps - offset2;

Amps = Amps * 2;

//Conversion formula for temperature
temperature = (sensorValue3 * 100.0) / 1023;

present_temp = temperature;

delay(500);

} // end while

} // end main

For the future please post your code between [code] [/code] tags so your code looks like this and is easy to copy to a text editor

#define LCD_CS A3 // Chip Select A3
#define LCD_CD A2 // Command/Data Register Select A2

#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // LCD Reset A4

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define WHITE   0xFFFF
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define BLUE    0x001F
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0

// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

const int analogInPin0 = A11;  // Analog input pin that the potentiometer1 is attached to
const int analogInPin2 = A13;  // Analog input pin that the potentiometer2 is attached to             
const int analogInPin4 = A15;  // Analog input pin that the potentiometer3 is attached to

int sensorValue1 = 0;        // value read from the pot1
int sensorValue2 = 0;        // value read from the pot2
int sensorValue3 = 0;        // value read from the pot3
float battery_voltage;
float temperature = 0.0; 
float input_current;
float previous_volt; 
float present_volt;
float present_current;
float previous_current;
float previous_temp = 0.0; 
float present_temp = 0.0;
float actual_batt_volt;
float offset_voltage = 2.44;   
const int R1 = 6900;
const int R2 = 2350;
int mVperAmp = 185;
int ACSoffset = 2500;
float Amps;             

void tftlcd_setup()
{
    tft.reset();       // reset tft
    tft.begin(0x9341); // SDFP5408
    tft.setRotation(3); // Need for the Mega, please changed for your choice or rotation initial         
    tft.fillScreen(BLACK);
    tft.setCursor(3, 24);
 
}


void setup(void)
{   
    Serial.begin(9600);
 
    float present_temp = 0.0;     
 
    tftlcd_setup();   
 
    display_homepage(); 

    while(1)
    {       
     
        sensorValue1 = analogRead(analogInPin0);
        sensorValue2 = analogRead(analogInPin2);
        sensorValue3 = analogRead(analogInPin4);

        Serial.println(sensorValue1);

        //Conversion formula for voltage
        battery_voltage = (sensorValue1 / 1024.0) * 5.0;

        Serial.println(battery_voltage);

        actual_batt_volt = (battery_voltage * (R1+R2)) / R2;

        Serial.println(actual_batt_volt);

        actual_batt_volt = actual_batt_volt - offset_voltage;

        //Serial.println(sensorValue2);

        //Conversion formula for current
        input_current = (sensorValue2 / 1024.0) * 5000; // Gets you mV                       

        //Serial.println(input_current);

        Amps = ((input_current - ACSoffset) / mVperAmp);

        //Serial.println(Amps);

        //Amps = Amps - offset2;

        Amps = Amps * 2;

        //Conversion formula for temperature
        temperature = (sensorValue3 * 100.0) / 1023;

        present_temp = temperature;
                
        delay(500);                                   

    } // end while
 
} // end main

...R

You are reading values from 3 different analog pins with this

sensorValue1 = analogRead(analogInPin0);
sensorValue2 = analogRead(analogInPin2);
sensorValue3 = analogRead(analogInPin4);

Part of the problem may be due to the ADC switching between pins. Try reading each pin twice in succession like this

sensorValue1 = analogRead(analogInPin0);
sensorValue1 = analogRead(analogInPin0);
sensorValue2 = analogRead(analogInPin2);
sensorValue2 = analogRead(analogInPin2);
sensorValue3 = analogRead(analogInPin4);
sensorValue3 = analogRead(analogInPin4);

This has the effect of discarding the first reading of each pin.

...R

I am using 12v Sealed Lead-Acid Rechargeable battery and i used voltage divider circuit for converting 12V to 5v.

I have connected the battery with arduino mega 2560 board.

Take an example:

Battery voltage measured on multimeter = 11.35v. If i read this value and display it on lcd there are variations 11.32, 11.33, 11.34, 11.35, 11.36, etc going upto 11.40, 11.42 like that.

How can i make it to constant 11.35v on tft lcd.

I have modified the code as per your suggestion but still variations are there when reading the data on tft lcd.

How to overcome this issue?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

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

Do you have code that just reads the analog inputs, without any dislplay code, and serial prints them to the monitor?
Did you write your code in stages and get each stage working before combining them?

Thanks.. Tom.. :slight_smile:

rajansn24:
How to overcome this issue?

The first step is to post the latest version of your program.

...R

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

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

Do you have code that just reads the analog inputs, without any dislplay code, and serial prints them to the monitor?
Did you write your code in stages and get each stage working before combining them?

Thanks.. Tom.. :slight_smile:

Here is my code.

#define LCD_CS A3 // Chip Select A3
#define LCD_CD A2 // Command/Data Register Select A2

#define LCD_RD A0 // LCD Read goes to Analog 0 
#define LCD_RESET A4 // LCD Reset A4

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define WHITE   0xFFFF
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define BLUE    0x001F
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0

// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

const int analogInPin0 = A11;  // Analog input pin that the potentiometer1 is attached to
const int analogInPin2 = A13;  // Analog input pin that the potentiometer2 is attached to             
const int analogInPin4 = A15;  // Analog input pin that the potentiometer3 is attached to

int sensorValue1 = 0;        // value read from the pot1
int sensorValue2 = 0;        // value read from the pot2
int sensorValue3 = 0;        // value read from the pot3
float battery_voltage;
float temperature = 0.0;  
float input_current;
float previous_volt;  
float present_volt; 
float present_current;
float previous_current; 
float previous_temp = 0.0;  
float present_temp = 0.0; 
float actual_batt_volt;
float offset_voltage = 2.44;   
const int R1 = 6900;
const int R2 = 2350;
int mVperAmp = 185; 
int ACSoffset = 2500;
float Amps;             

void tftlcd_setup()
{
  tft.reset();       // reset tft
  tft.begin(0x9341); // SDFP5408
  tft.setRotation(3); // Need for the Mega, please changed for your choice or rotation initial          
  tft.fillScreen(BLACK);
  tft.setCursor(3, 24);
  
}


void setup(void) 
{   
  Serial.begin(9600);
  
  float present_temp = 0.0;     
  
  tftlcd_setup();    
  
  display_homepage();  

while(1)
{          
    sensorValue1 = analogRead(analogInPin0);
    sensorValue1 = analogRead(analogInPin0);
    sensorValue2 = analogRead(analogInPin2);
    sensorValue2 = analogRead(analogInPin2);
    sensorValue3 = analogRead(analogInPin4);
    sensorValue3 = analogRead(analogInPin4);

    Serial.println(sensorValue1);

    //Conversion formula for voltage
    battery_voltage = (sensorValue1 / 1024.0) * 5.0;

    Serial.println(battery_voltage);

    actual_batt_volt = (battery_voltage * (R1+R2)) / R2;

    Serial.println(actual_batt_volt);

    actual_batt_volt = actual_batt_volt - offset_voltage;

    //Serial.println(sensorValue2);

    //Conversion formula for current
    input_current = (sensorValue2 / 1024.0) * 5000; // Gets you mV                        

    //Serial.println(input_current);

    Amps = ((input_current - ACSoffset) / mVperAmp);

    //Serial.println(Amps);

    //Amps = Amps - offset2;

    Amps = Amps * 2;

    //Conversion formula for temperature
    temperature = (sensorValue3 * 100.0) / 1023;
           
    delay(500);                                   

} // end while  
} // end main

[\code]

Robin2:
The first step is to post the latest version of your program.

...R

Here is updated version of the program.

#define LCD_CS A3 // Chip Select A3
#define LCD_CD A2 // Command/Data Register Select A2

#define LCD_RD A0 // LCD Read goes to Analog 0 
#define LCD_RESET A4 // LCD Reset A4

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define WHITE   0xFFFF
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define BLUE    0x001F
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0

// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

const int analogInPin0 = A11;  // Analog input pin that the potentiometer1 is attached to
const int analogInPin2 = A13;  // Analog input pin that the potentiometer2 is attached to             
const int analogInPin4 = A15;  // Analog input pin that the potentiometer3 is attached to

int sensorValue1 = 0;        // value read from the pot1
int sensorValue2 = 0;        // value read from the pot2
int sensorValue3 = 0;        // value read from the pot3
float battery_voltage;
float temperature = 0.0;  
float input_current;
float previous_volt;  
float present_volt; 
float present_current;
float previous_current; 
float previous_temp = 0.0;  
float present_temp = 0.0; 
float actual_batt_volt;
float offset_voltage = 2.44;   
const int R1 = 6900;
const int R2 = 2350;
int mVperAmp = 185; 
int ACSoffset = 2500;
float Amps;             

void tftlcd_setup()
{
  tft.reset();       // reset tft
  tft.begin(0x9341); // SDFP5408
  tft.setRotation(3); // Need for the Mega, please changed for your choice or rotation initial          
  tft.fillScreen(BLACK);
  tft.setCursor(3, 24);
  
}


void setup(void) 
{   
  Serial.begin(9600);
  
  float present_temp = 0.0;     
  
  tftlcd_setup();    
  
  display_homepage();  

while(1)
{          
    sensorValue1 = analogRead(analogInPin0);
    sensorValue1 = analogRead(analogInPin0);
    sensorValue2 = analogRead(analogInPin2);
    sensorValue2 = analogRead(analogInPin2);
    sensorValue3 = analogRead(analogInPin4);
    sensorValue3 = analogRead(analogInPin4);

    Serial.println(sensorValue1);

    //Conversion formula for voltage
    battery_voltage = (sensorValue1 / 1024.0) * 5.0;

    Serial.println(battery_voltage);

    actual_batt_volt = (battery_voltage * (R1+R2)) / R2;

    Serial.println(actual_batt_volt);

    actual_batt_volt = actual_batt_volt - offset_voltage;

    //Serial.println(sensorValue2);

    //Conversion formula for current
    input_current = (sensorValue2 / 1024.0) * 5000; // Gets you mV                        

    //Serial.println(input_current);

    Amps = ((input_current - ACSoffset) / mVperAmp);

    //Serial.println(Amps);

    //Amps = Amps - offset2;

    Amps = Amps * 2;

    //Conversion formula for temperature
    temperature = (sensorValue3 * 100.0) / 1023;
           
    delay(500);                                   

} // end while  
} // end main

Which program are we to study - the one in Reply #9 or in Reply #10?

It is surely inconceivable that you posted an identical program twice?

...R

Sorry that i have send the same code to both the members.

rajansn24:
Sorry that i have send the same code to both the members.

As you can see things don't "go to members". They just appear on the Forum where everyone can see them so there is no need to repeat stuff - it just causes confusion.

You can use a single Reply to respond to several members when it makes sense to do so.

...R

Exactly which variable is showing the jitter?

Does it appear in the values that come directly from analogRead() or does it appear after you have done some calculations on those values?

...R

The sketch won't compile.

Build options changed, rebuilding all
sketch_dec29a:20: error: 'Adafruit_TFTLCD' does not name a type
 Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
 ^

Even if I get past that, where do I find loop()?

Hi,
The code structure is;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

What IDE are you using to program your controller?
What model controller is it?

Tom... :slight_smile:

Robin2:
Exactly which variable is showing the jitter?

Does it appear in the values that come directly from analogRead() or does it appear after you have done some calculations on those values?

...R

Consider the code:

sensorValue1 = analogRead(analogInPin0); (for 12v battery - voltage divider output pin 0-5v)
sensorValue2 = analogRead(analogInPin2); (for ACS712 current sensor output pin)
sensorValue3 = analogRead(analogInPin4); (for potentiometer 0-5v for temperature)

Serial.println(sensorValue1);
Serial.println(sensorValue2);
Serial.println(sensorValue1);

I have connected 3 sensors for monitor the voltage, current, temperature of the battery.

For monitor the battery voltage, a voltage divider circuit is connected with the battery which gives the output of 0-5Volts range.

Once i connect the battery output to analogInPin0, On serial monitor, i could see the variations while reading
the digital values.

Same thing, for monitor the current, ACS712 5Amps current sensor output is connected to analogInPin2. This
also gives the variations while reading the digital values on serial monitor.

For temperature monitor, i just used potentiometer output connected to analogInPin4. In this, there is no much variations, showing almost constant value compare to voltage and current.

But one more thing, in arduino mega 2560 board, there is 5volts output is available on the pin. My board is showing only 4.24 Volts instead of 5Volts. Whether this may cause the problem?

Because for 5v analog data, the digital value should be 1023. But In my board, for 4.24Volts i am getting 1023.

So what is the exact reason for showing this variations?

But also reading the analog data and after calculations i got the variations in output.

pls provide me some good solution to sort it out this variations.

johnwasser:
The sketch won't compile.

Build options changed, rebuilding all

sketch_dec29a:20: error: 'Adafruit_TFTLCD' does not name a type
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
^



Even if I get past that, where do I find loop()?

There is no issue on lcd it is working working properly on my arduino mega 2560 board.

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Including power supplies.

Thanks.. Tom.. :slight_smile: