Go Down

Topic: voltage/Analog input (Read 1 time) previous topic - next topic

scootapy

May 23, 2012, 02:14 pm Last Edit: May 23, 2012, 02:17 pm by AWOL Reason: 1
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.



Code: [Select]

// 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: [font=Verdana][[/font]code] [font=Verdana][[/font]/code] tags added.

AWOL

Code: [Select]
vout = (value * 5.0) / 1024.0;
Perhaps your reference is not 5 volts.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

terryking228

#2
May 23, 2012, 03:04 pm Last Edit: May 23, 2012, 06:17 pm by terryking228 Reason: 1
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: http://arduino.cc/it/Reference/AnalogReference

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

scootapy

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

footswitch

#4
Jun 06, 2012, 04:23 am Last Edit: Jun 06, 2012, 04:24 am by footswitch Reason: 1
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 :)
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

James C4S

#5
Jun 06, 2012, 05:23 am Last Edit: Jun 06, 2012, 05:42 am by James C4S Reason: 1

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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

footswitch

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".

?

James C4S


I need to power that hardware through Vin

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


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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

footswitch

#8
Jun 06, 2012, 05:48 am Last Edit: Jun 06, 2012, 05:51 am by footswitch Reason: 1
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 :)

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.

Docedison

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
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

majenko

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.

terryking228

Hi Majenko,

Nice! 

May I link/abstract that for the arduino-info Wiki??
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

majenko

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

footswitch

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

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...

James C4S


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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Go Up