instability analog voltage lcd

Hello
I realized with a mega arduino and a LCD 20X4, a voltmeter that measures the voltage of several batteries.
My batteries are in parallel series so I measure the highest voltage (24V) and I have a midpoint to measure the voltage of each battery. My problem is that the values ​​read on my LCD are unstable, they vary constantly.
Would you have a solution to my problem?

Do you have a idea

Please

Bumping your post within a few minutes will not gain you much sympathy... be patient, no one works here, everyone who answers is giving away personal free time ...

And double posting in different languages without any mention would definitely get you in trouble...

Said differently : WTF did you bump it here instead of answering my question to you in the French forum... stick to one thread. Hope someone else will b willing to help you out.

stev:
My problem is that the values read on my LCD are unstable, they vary constantly.

Would you have a solution to my problem?

This is a common problem.
Battery voltage from the voltage divider is compared to a reference voltage, and that happens to be the 5volt supply.
If the 5volt supply dips 5%, voltage readout goes up by 5%.

The solution is to NOT use default Aref for voltage measurements, but one of the other buildin reference voltages.
A Mega has two. A 1.1volt and a 2.56volt Aref.
Learn about it in the Help>Reference section of the IDE.

You must change your voltage divider, so that it outputs e.g. 2.56volt max, then add:
analogReference(INTERNAL2V56);
to void setup()
And use 2.56 instead of 5.0 in the maths line of your A/D value to voltage conversion code.
Post your code if you want help with that.
Read forum rules about code tags first.
Leo..

hello wawa

I'm a beginner and I do not understand what you're telling me to do.
My code is attached.

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,20,4);

float rapport1 = 7.7 ;

float rapport2 = 7.7 ;

float rapport3 = 7.7 ;

float rapport4 = 7.7 ;

float rapport5 = 7.7 ;

float rapport6 = 7.7 ;

float rapport7 = 7.7 ;

float rapport8 = 7.75 ;

float rapport9 = 4.1 ;

const int ledPin = 13 ; // the number of the LED pin

// set the LCD address to 0x3F for a 20 chars and 4 line display

void setup()
{ delay(500);
lcd.init();
lcd.backlight();
pinMode(ledPin, OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop ()
{
{
// read the input on analog pin 0:
int sensorValue0 = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage1 = (sensorValue0 * (5.0 / 1023.0))* rapport1;
// print out the value you read:
lcd.setCursor(0,0);
lcd.print(voltage1);

lcd.setCursor(6,0);
lcd.print("B");
lcd.print("1");

// read the input on analog pin 1:
int sensorValue1 = analogRead(A1);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage2 = (sensorValue1 * (5.0 / 1023.0))* rapport2 - voltage1 ;
// print out the value you read:
lcd.setCursor(0,1);
lcd.print(voltage2);

lcd.setCursor(6,1);
lcd.print("B");
lcd.print("2");

// read the input on analog pin 2:
int sensorValue2 = analogRead(A2);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage3 = (sensorValue2 * (5.0 / 1023.0))* rapport3 ;
// print out the value you read:
lcd.setCursor(0,2);
lcd.print(voltage3);

lcd.setCursor(6,2);
lcd.print("B");
lcd.print("3");

// read the input on analog pin 3:
int sensorValue3 = analogRead(A3);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage4 = (sensorValue3 * (5.0 / 1023.0))* rapport4 - voltage3 ;
// print out the value you read:
lcd.setCursor(0,3);
lcd.print(voltage4);

lcd.setCursor(6,3);
lcd.print("B");
lcd.print("4");

// read the input on analog pin 5:
int sensorValue5 = analogRead(A5);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage5 = (sensorValue5 * (5.0 / 1023.0))* rapport5 ;
// print out the value you read:
lcd.setCursor(11,0);
lcd.print(voltage5);

lcd.setCursor(17,0);
lcd.print("B");
lcd.print("5");

// read the input on analog pin 4:
int sensorValue4 = analogRead(A4);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage6 = (sensorValue4 * (5.0 / 1023.0))* rapport6 - voltage5 ;
// print out the value you read:
lcd.setCursor(11,1);
lcd.print(voltage6);

lcd.setCursor(17,1);
lcd.print("B");
lcd.print("6");

// read the input on analog pin 7:
int sensorValue7 = analogRead(A7);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage7 = (sensorValue7 * (5.0 / 1023.0))* rapport7 ;
// print out the value you read:
lcd.setCursor(11,2);
lcd.print(voltage7);

lcd.setCursor(17,2);
lcd.print("B");
lcd.print("7");

// read the input on analog pin 6:
int sensorValue6 = analogRead(A6);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage8 = (sensorValue6 * (5.0 / 1023.0))* rapport8 - voltage7 ;
// print out the value you read:
lcd.setCursor(11,3);
lcd.print(voltage8);

lcd.setCursor(17,3);
lcd.print("B");
lcd.print("8");

delay(1000);

}

{

// read the input on analog pin 8:
int sensorValue8 = analogRead(A8);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage8 = (sensorValue8 * (5.0 / 1023.0))* rapport9 ;
if (voltage8 < 7.4)
{
digitalWrite(ledPin, HIGH);
}

}
}

I do not know where to put my file so I put it directly in the message

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?

Thanks. Tom.. :slight_smile:

#include <Wire.h>                      

 #include <LiquidCrystal_I2C.h>

 LiquidCrystal_I2C lcd(0x3F,20,4);  

 float rapport1 = 7.7 ;

 float rapport2 = 7.7 ;

 float rapport3 = 7.7 ;

 float rapport4 = 7.7 ;

 float rapport5 = 7.7 ;

 float rapport6 = 7.7 ;

 float rapport7 = 7.7 ;

 float rapport8 = 7.75 ;

 float rapport9 = 4.1 ;

 const int ledPin = 13 ;   // the number of the LED pin
 
 // set the LCD address to 0x3F for a 20 chars and 4 line display

void setup() 
{   delay(500);
   lcd.init();
   lcd.backlight();
   pinMode(ledPin, OUTPUT);
   // initialize serial communication at 9600 bits per second:
   Serial.begin(9600);    
}

// the loop routine runs over and over again forever:
void loop () 
{
{  
 // read the input on analog pin 0:
 int sensorValue0 = analogRead(A0);
 // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
 float voltage1 = (sensorValue0 * (5.0 / 1023.0))* rapport1;
 // print out the value you read:
 lcd.setCursor(0,0);
 lcd.print(voltage1);
 
 lcd.setCursor(6,0);
 lcd.print("B"); 
 lcd.print("1"); 
 
 
 // read the input on analog pin 1:
 int sensorValue1 = analogRead(A1);
 // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
 float voltage2 = (sensorValue1 * (5.0 / 1023.0))* rapport2 - voltage1 ;
 // print out the value you read:
 lcd.setCursor(0,1);
 lcd.print(voltage2);
 
 lcd.setCursor(6,1);
 lcd.print("B");
 lcd.print("2");
 
 
 // read the input on analog pin 2:
 int sensorValue2 = analogRead(A2);
 // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
 float voltage3 = (sensorValue2 * (5.0 / 1023.0))* rapport3 ;
 // print out the value you read:
 lcd.setCursor(0,2);
 lcd.print(voltage3);
 
 lcd.setCursor(6,2);
 lcd.print("B");
 lcd.print("3");
 

 // read the input on analog pin 3:
 int sensorValue3 = analogRead(A3);
 // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
 float voltage4 = (sensorValue3 * (5.0 / 1023.0))* rapport4 - voltage3 ;
 // print out the value you read:
 lcd.setCursor(0,3);
 lcd.print(voltage4);
 
 lcd.setCursor(6,3);
 lcd.print("B");
 lcd.print("4");
 

 // read the input on analog pin 5:
 int sensorValue5 = analogRead(A5);
 // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
 float voltage5 = (sensorValue5 * (5.0 / 1023.0))* rapport5 ;
 // print out the value you read:
 lcd.setCursor(11,0);
 lcd.print(voltage5);
 
 lcd.setCursor(17,0);
 lcd.print("B");
 lcd.print("5");
 

 // read the input on analog pin 4:
 int sensorValue4 = analogRead(A4);
 // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
 float voltage6 = (sensorValue4 * (5.0 / 1023.0))* rapport6 - voltage5 ;
 // print out the value you read:
 lcd.setCursor(11,1);
 lcd.print(voltage6);
 
 lcd.setCursor(17,1);
 lcd.print("B");
 lcd.print("6");
 

 // read the input on analog pin 7:
 int sensorValue7 = analogRead(A7);
 // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
 float voltage7 = (sensorValue7 * (5.0 / 1023.0))* rapport7 ;
 // print out the value you read:
 lcd.setCursor(11,2);
 lcd.print(voltage7);
 
 lcd.setCursor(17,2);
 lcd.print("B");
 lcd.print("7");
 

 // read the input on analog pin 6:
 int sensorValue6 = analogRead(A6);
 // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
 float voltage8 = (sensorValue6 * (5.0 / 1023.0))* rapport8 - voltage7 ;
 // print out the value you read:
 lcd.setCursor(11,3);
 lcd.print(voltage8);
 
 lcd.setCursor(17,3);
 lcd.print("B");
 lcd.print("8");


 delay(1000);

}

{

 // read the input on analog pin 8:
 int sensorValue8 = analogRead(A8);
 // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
 float voltage8 = (sensorValue8 * (5.0 / 1023.0))* rapport9 ;
 if (voltage8 < 7.4)
 {
  digitalWrite(ledPin, HIGH);
 }

}
}[code]

Arduino.pdf (18 KB)

Hi,
OPs circuit.
battmes.jpg

Tom... :slight_smile:

Hi,
Have you tried putting 0.1uF capacitors between A0 and gnd and A1 and gnd?

Also when you read an analog input do it twice, this because there is only one AtoD converter, and it has to switch to each input pin.
The AtoD has a capacitor on its input and it needs time to charge to the new input as it is switched.

Reading the pin twice gives the capacitor time to charge.

// read the input on analog pin 1
 int sensorValue1 = analogRead(A1);
     sensorValue1 = analogRead(A1);
 // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V)
 float voltage2 = (sensorValue1 * (5.0 / 1023.0))* rapport2 - voltage1 ;
 // print out the value you read:
 lcd.setCursor(0,1);

Do this double read of each of your analogRead.
Tom... :slight_smile:

So I test and I come back to you

Since the source impedance of the divider is about 220 ohms there is no point double reading or adding capacitors - those are workarounds for when the source impedance is >> 10k

so I tested the new sketch and now it's ok the stability is better

#include <Wire.h>

  #include <LiquidCrystal_I2C.h>

  LiquidCrystal_I2C lcd(0x3F,20,4);

  float rapport1 = 7.51 ;

  float rapport2 = 4.93 ;

  float rapport3 = 7.51 ;

  float rapport4 = 4.93 ;

  float rapport5 = 7.51 ;

  float rapport6 = 4.93 ;

  float rapport7 = 6.15 ;

  float rapport8 = 4.93 ;

  float rapport9 = 4.1 ;

  const int ledPin = 13 ;   // the number of the LED pin
  
  // set the LCD address to 0x3F for a 20 chars and 4 line display

void setup() 
{   delay(500);
    lcd.init();
    lcd.backlight();
    pinMode(ledPin, OUTPUT);
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
    analogReference(INTERNAL2V56);
}

// the loop routine runs over and over again forever:
void loop () 
{
{  
  // read the input on analog pin 0:
  int sensorValue0 = analogRead(A0);
      sensorValue0 = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage1 = ((sensorValue0 * (2.56 / 1023.0))* rapport1);
  // print out the value you read:
  lcd.setCursor(0,0);
  lcd.print(voltage1);
  
  lcd.setCursor(6,0);
  lcd.print("B"); 
  lcd.print("1"); 
  
  
  // read the input on analog pin 1:
  int sensorValue1 = analogRead(A1);
      sensorValue1 = analogRead(A1);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage2 = ((sensorValue1 * (2.56 / 1023.0))* rapport2) ;
  // print out the value you read:
  lcd.setCursor(0,1);
  lcd.print(voltage2);
  
  lcd.setCursor(6,1);
  lcd.print("B");
  lcd.print("2");
  
  
  // read the input on analog pin 2:
  int sensorValue2 = analogRead(A2);
      sensorValue2 = analogRead(A2);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage3 = ((sensorValue2 * (2.56 / 1023.0))* rapport3) ;
  // print out the value you read:
  lcd.setCursor(0,2);
  lcd.print(voltage3);
  
  lcd.setCursor(6,2);
  lcd.print("B");
  lcd.print("3");
  

  // read the input on analog pin 3:
  int sensorValue3 = analogRead(A3);
      sensorValue3 = analogRead(A3);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage4 = ((sensorValue3 * (2.56 / 1023.0))* rapport4) ;
  // print out the value you read:
  lcd.setCursor(0,3);
  lcd.print(voltage4);
  
  lcd.setCursor(6,3);
  lcd.print("B");
  lcd.print("4");
  

  // read the input on analog pin 4:
  int sensorValue4 = analogRead(A4);
      sensorValue4 = analogRead(A4);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage5 = ((sensorValue4 * (2.56 / 1023.0))* rapport5) ;
  // print out the value you read:
  lcd.setCursor(11,0);
  lcd.print(voltage5);
  
  lcd.setCursor(17,0);
  lcd.print("B");
  lcd.print("5");
  

  // read the input on analog pin 5:
  int sensorValue5 = analogRead(A5);
      sensorValue5 = analogRead(A5);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage6 = ((sensorValue5 * (2.56 / 1023.0))* rapport6) ;
  // print out the value you read:
  lcd.setCursor(11,1);
  lcd.print(voltage6);
  
  lcd.setCursor(17,1);
  lcd.print("B");
  lcd.print("6");
  

  // read the input on analog pin 6:
  int sensorValue6 = analogRead(A6);
      sensorValue6 = analogRead(A6);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage7 = ((sensorValue6 * (2.56 / 1023.0))* rapport7) ;
  // print out the value you read:
  lcd.setCursor(11,2);
  lcd.print(voltage7);
  
  lcd.setCursor(17,2);
  lcd.print("B");
  lcd.print("7");
  

  // read the input on analog pin 7:
  int sensorValue7 = analogRead(A7);
      sensorValue7 = analogRead(A7);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage8 = ((sensorValue7 * (2.56 / 1023.0))* rapport8) ;
  // print out the value you read:
  lcd.setCursor(11,3);
  lcd.print(voltage8);
  
  lcd.setCursor(17,3);
  lcd.print("B");
  lcd.print("8");


  delay(1000);

}

{
 
  // read the input on analog pin 8:
  int sensorValue8 = analogRead(A8);
      sensorValue8 = analogRead(A8);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage8 = ((sensorValue8 * (2.56 / 1023.0))* rapport9) ;
  if (voltage8 < 7.4)
  {
   digitalWrite(ledPin, HIGH);
  }

}
}