voltage/Analog input

Hi everybody, exuse me if I do some mistake with my english, i'm a french student.
I meet a weird problem, I put a voltage in the A1 pint (4.43V) and with a little program, I want to read this voltage on a LCD screen ' le deuligne / snootlab' shield.
The problem is that i read 4.50 V on the LCD screen when the arduino is powered with USB and 4.45 V with an external supply.
I've been check the voltage between A1 and GND (==>4.43V).
So, I have two questions : why I don't have the good value on my LCD screen ? and why the value change depending on the supply ?
Their is nothing between the voltage and the arduindo.
I put the code if you want. (Their is other thing that the reading of the voltage)
Thanks you.

// include the library code:
#include "Wire.h"
#include <Deuligne.h>

// initialize the library with the numbers of the interface pins
Deuligne lcd;

// variables
  int analogInput = 1;
  int refresh = 1000;
  float vout = 0.0;
  float vin = 0.0;
  float P = 0.0;
  float R1 = 1000.0;    // !! resistance of R1 !!
  float R2 = 1000.0;     // !! resistance of R2 !!
  
// variable to store the value 
  int value = 0;

// function to print float scientific version

  char * float2s(float f)
{
  return float2s(f, 2);
}

char * float2s(float f, unsigned int digits)
{
  int index = 0;
  static char s[16];   	           // buffer to build string representation
  // handle sign
  if (f < 0.0)
  {
    s[index++] = '-';
    f = -f;
  }
  // handle infinite values
  if (isinf(f))
  {
    strcpy(&s[index], "INF");
    return s;
  }
  // handle Not a Number
  if (isnan(f))
  {
    strcpy(&s[index], "NaN");
    return s;
  }

  // max digits
  if (digits > 6) digits = 6;
  long multiplier = pow(10, digits);     // fix int => long

  int exponent = int(log10(f));
  float g = f / pow(10, exponent);
  if ((g < 1.0) && (g != 0.0))      
  {
    g *= 10;
    exponent--;
  }

  long whole = long(g);                     // single digit
  long part = long((g-whole)*multiplier);   // # digits
  char format[16];
  sprintf(format, "%%ld.%%0%dld E%%+d", digits);
  sprintf(&s[index], format, whole, part, exponent);
  
  return s;
} 


void setup() {
  
    // declaration of pin modes
  pinMode(analogInput, INPUT);
  Serial.begin(9600);
  // set up the LCD's number of columns and rows: 
  lcd.init();
 
  // Print a message to the LCD.
 // lcd.print("PRESSION (mbar):");
}

void loop() {
  
  
  // read the value on analog input
  value = analogRead(analogInput);
 lcd.print(value);

  // print result over the serial port
  vout = (value * 5.0) / 1024.0;

  lcd.setCursor(10,0);
  lcd.print(vout);

  vin = vout / (R2/(R1+R2));
  P = pow(10.0,(vin - 6.0));
  
  // set the cursor to column 4, line 1
 
  lcd.setCursor(4,1);
  lcd.print(float2s(P, 1));
  
   // sleep...
  delay(refresh);
  
}

Moderator edit: tags added.

vout = (value * 5.0) / 1024.0;

Perhaps your reference is not 5 volts.

Yes, the default reference is the voltage supplied to the Arduino.

Measure that voltage, and if you use that value in your calculation instead of 5.0 it should be more accurate.

For highly accurate results, an external voltage reference can be used.

See: analogReference() - Arduino Reference

All right, thanks you for your answer !
Good day !

Apologies for digging this up. I believe my issue is related to the OP's.

I'm powering the Arduino directly through Vin @ 5V, and I realize that the analog readings have shifted up by a lot (~20%).
Why? If I power it through the USB connector, Vin is lower than 5V, and substantially lower if I'm powering additional hardware through that pin.
So I thought this was a good way of getting around that, but it seems that when I power the Arduino via Vin, the opposite happens: the microcontroller itself is being powered at lower than 5V, and this is making the analogRead() calls return unexpected (higher) readings.

So I have two questions, if someone will have the kindness to help :slight_smile:

  1. Do you suggest any other way of powering the Arduino + additional hardware without suffering this voltage drop, considering that the hardware is stacked on top of the Arduino?

  2. By default (not messing with analogReference()), if I'm powering it through Vin and if I also connect Vin to AREF:
    a) will the readings return to the expected values, since I'd be supplying 5V to the AREF pin, thus changing the reading "range" to 0-5V?
    b) won't this damage the microcontroller in the long run? Since it's being powered at a lower voltage than AREF?
    EDIT: in this case, am I good by NOT calling analogReference() ?

Thanks in advance for your time.
footswitch

footswitch:
I'm powering the Arduino directly through Vin @ 5V

You can't put 5V on Vin and get meaningful operation.

The Vin node is connected to a regulator that provides 5volts to the 5V node. The regulator needs at least 7Volts to be stable.

If you are going to provide 5volts directly, put it on the 5V node.

Measuring Vin while powered through USB or the 5V is meaningless.

From what you're saying, I understand that the 5V pin will power the Arduino directly.

But something isn't making sense here for me:
Since I have additional hardware stacked on top of the Arduino, and I need to power that hardware through Vin, I suppose I can't connect 5V to both the 5V pin and Vin. It seems like the direct feed and the voltage regulator's output would "collide".

?

footswitch:
I need to power that hardware through Vin

Hardware relying on Vin should expect it to be greater than 7volts.

footswitch:
I suppose I can't connect 5V to both the 5V pin and Vin. It seems like the direct feed and the voltage regulator's output would "collide".

No, you can not provide power on both 5V and Vin.

Ok, sorry for insisting, but is it bad in any way to power the Arduino with 5V through Vin?
Apart from the analogRead() value shifting - and I have tried connecting Vin to AREF, which seems to solve the issue - it's been working flawless for the last 4 hours.

It's the "You can't put 5V on Vin and get meaningful operation" part that's scaring me :slight_smile:

EDIT: the hardware I'm using has its own regulator for 3.7V. So it can take as many voltage as the Arduino, but doesn't need at least 7V to work properly.

Use an external 7.5 to 9V 1A supply, I do as a plugin to my breadboards and I have yet to have a problem, I don't use the board 3V3 or 5V sources just the ground. one day I changed an analog thermometer from a dark to a backlit LCD and my voltage readings shifted by about 15%. After I had rechecked everything (I thought) I plugged in an AC supply as I needed to take my computer down for an upgrade... and the issue was fixed again.

Doc

I have written a little blog post on how to make accurate readings with the Arduino ADC. It pretty well covers what you want to do:

http://hacking.majenko.co.uk/making-accurate-adc-readings-on-arduino

It involves using the internal 1.1V reference voltage to measure your Vcc voltage before then reading your ADC voltage and using that calculated Vcc reading as your reference when calculating the incoming voltage.

Hi Majenko,

Nice!

May I link/abstract that for the arduino-info Wiki??

Sure. Most of the inspiration, and the actual code, came from TinkerIt.

This is indeed great for precision reading :slight_smile: it only makes me question myself about every time I used analogRead() in the past :stuck_out_tongue:

But guys, can you help me? Will my Arduino eventually "explode" by being powered with these 4V?
(powering with 5V directly on Vin)

The device that is powering the Arduino has a 5V output. It's an all-in-one solution for unlimited power supply (solar+battery+mains power).
There's no way around this...

footswitch:
Ok, sorry for insisting, but is it bad in any way to power the Arduino with 5V through Vin?

Yes, it will result in strange random behavior like value shifting on analogRead.

Will it cause damage? No, probably not. Personally, I like when my circuits work reliably. We seem to disagree on that point.

The behavior is not strange nor random. It makes perfect sense, and the previous replies just confirmed that.

I'm not sure what you mean by "reliable". Because that's what my question is about: since everything is working well right now, will it be reliable in the long run? You seem to think it won't, but you also say that it "probably" won't damage the microcontroller. So I'm confused.
But thanks for your input nonetheless.

There's confused on what reliable means.

footswitch:
I'm not sure what you mean by "reliable".

Initially I meant reliable in regards to the regulator's output and the effect on the other components on the Arduino board, namely, the ATmega328. You are operating the regulator outside of its specs. Its output will not be reliable. If board's temperature changes its output will change. If the load on the regulator increases, its output will change (go down). If the input voltage drops so will the regulator. The whole reason for using a regulator is to prevent this change from happening.

footswitch:
Because that's what my question is about: since everything is working well right now, will it be reliable in the long run?

No. At some point you will see problems. Just because something works today doesn't mean it isn't damaged and will fail later.

footswitch:
but you also say that it "probably" won't damage the microcontroller

What I did not bring up before, but you should consider are other aspects of the ATmega328.

For example, the Absolute Input Voltage for any input before damage occurs is rated as 0.5volts + Vcc. In your case, you put 5volts into a regulator and you are getting less out; probably 4.4volts. So 0.5volts + 4.4volts = 4.9volts. If you are applying 5volts on Vin and that same 5volts to any other pin on the ATmega328, you are going to eventually damage it.

This is where "probably" comes in. You're doing a bunch of borderline things and asking for a definitive answer. So I will just keep repeating myself: Do not put 5volts on Vin.

Will my Arduino eventually "explode"

No most die in their sleep, but keep abusing it like this and it will be sooner rather than later.

Oh. Thank you for explaining :slight_smile:

Since 5V is my only option, I'll just find a way to split the additional hardware, so that I can power the Arduino alone on the USB header, and the rest I will power directly. All good? :smiley:

Thanks again.