Pages: [1] 2   Go Down
Author Topic: voltage/Analog input  (Read 1481 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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: [code] [/code] tags added.
« Last Edit: May 23, 2012, 07:17:37 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25712
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
vout = (value * 5.0) / 1024.0;
Perhaps your reference is not 5 volts.
Logged

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

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 32
Posts: 1866
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: May 23, 2012, 11:17:59 am by terryking228 » Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
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
« Last Edit: June 05, 2012, 09:24:32 pm by footswitch » Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 05, 2012, 10:42:56 pm by James C4S » Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

?
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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.
« Last Edit: June 05, 2012, 10:51:09 pm by footswitch » Logged

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 46
Posts: 2865
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 32
Posts: 1866
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Majenko,

Nice! 

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

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

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Pages: [1] 2   Go Up
Jump to: