Pages: 1 [2] 3 4   Go Down
Author Topic: Three voltage measurements + output to a LCD screen  (Read 2657 times)
0 Members and 1 Guest are viewing this topic.
South UK
Offline Offline
God Member
*****
Karma: 2
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

right, I'm down to this now which is displaying OK but the readings are not sensible, any idea how I can get a true reading?

Thanks

Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int analoginput = 0;
float vin = 0.0;


void setup(){
 
 // declaration of pin modes
 pinMode(analoginput, INPUT);
 lcd.begin(16, 2);
delay(100);
}



void loop(){
 // read the value on analog input
lcd.setCursor(0,0);
lcd.print ("Vin:");
vin = analogRead(analoginput)/200;
lcd.print(vin, 3);
lcd.setCursor(9,0);
lcd.print ("V");
 
delay(1400);

}
Logged

Dallas, Texas
Offline Offline
God Member
*****
Karma: 0
Posts: 861
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I read this thread pretty thoroughly but I might have missed an important bit.

First off there is no need to initialize an analog input unless you are going to use it as digital so this:
Code:
pinMode(analoginput, INPUT);
is unnecessary. Fortunately it doesn't do any harm. It initializes the serial Rx line to an input which it is anyway.

Then, this line:
Code:
vin = analogRead(analoginput)/200;
while giving approximately the right value should really be:
Code:
analogRead(analoginput)/1023*5.00;
unless you are trying to do some kind of scaling.

Would that give you sensible readings? You didn't say how they weren't sensible.
Logged

South UK
Offline Offline
God Member
*****
Karma: 2
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

thanks for the reply. What bits have I not made clear? Apologies, I am brand new to the arduino  smiley When i was testing earlier the voltage readings didn't make sense - with an input voltage of 4v i think it was displaying 3.5v but if i dropped the voltage to 3v it would be sensible.

I have just edited the code following your advice, the display is now reading 0v and does not increase when I connect a voltage to the analog pin?

One odd thing I noticed is I pulled the usb cable and the arduino LED stayed on, this was because of the voltage I was inputting to analoge pin 0, (it was about 3v) is this normal behavior lol?

This is what I have now, am I missing something crucial?

Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int analoginput = 0;
float vin = 0.0;


void setup(){
  
 // declaration of pin modes
 lcd.begin(16, 2);
delay(100);
}



void loop(){
 // read the value on analog input
lcd.setCursor(0,0);
lcd.print ("Vin:");
vin = analogRead(analoginput)/1023*5.00;
lcd.print(vin, 3);
lcd.setCursor(9,0);
lcd.print ("V");
 
delay(1400);

}

Thanks
« Last Edit: July 29, 2011, 06:20:37 pm by dtokez » Logged

South UK
Offline Offline
God Member
*****
Karma: 2
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

just playing around and as soon as I divide the input by 1023 I get a static display of 0v  smiley-confuse
Logged

Dallas, Texas
Offline Offline
God Member
*****
Karma: 0
Posts: 861
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Whoops! I hate it when that happens. I haven't fetched my Arduino out of storage yet and it's been two years since I played with this stuff so I'm a little rusty. Let's pretend we're still in first grade and make sure all the data types match because I can't remember if you can mix floats and ints in an expression. If this works, then you can play with it and you'll have a working model to revert to. You'll have to tell me what happens because, as I said, I don't have anything to test the code on.

Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int analoginput = A0;
int vin = 0;
float Voltage = 0.0;


void setup()
{
// declaration of pin modes
    lcd.begin(16, 2);
    delay(100);
}



void loop()
{

  vin = analogRead(analoginput);  //read the value on analog input A0
 
  lcd.setCursor(0,0);
  lcd.print("V(int): ");
  lcd.print("    ");              //clear out previous integer (4 spaces)
  lcd.setCursor(7,0);             //backspace
  lcd.print(vin);                 //print integer value of analog input
  lcd.println (" V");             //print units and advance to next line of display
 
  Voltage = float(vin);           //cast vin to float
  Voltage = Voltage/1023.0*5.0;   //scale to 5V full scale
 
  lcd.print("V(float): ");
  lcd.print("    ");             //clear out previous float
  lcd.setCursor(9,0);           //backspace
  lcd.print(Voltage,2);
  lcd.println(" V");



 
delay(1400);

}

Let me know what happens.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6889
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

just playing around and as soon as I divide the input by 1023 I get a static display of 0v  smiley-confuse

Sorry I dropped the ball. I'm in a conference (arduino workshop two days from today and more on physics teaching!).

The reason you got zero was because you divided your reading by 1023. Both your analog reading and 1023 are integers so their division was handled as integer. Only quotient was reported, which is almost always 0, while the remainder is lost. Say you have 512 from analog input, you divide it by 1023. You get 0 as quotient and 512 as remainder but integer division only reports quotient, 0. Then any subsequent multiplication or division is zero.

You want to do this instead: result=analogRead(sensor)*5.00/1024;

The above way multiplies an integer (analogRead) with a float 5.00. The operation requires converting integer to float. Then the result is divided by an integer 1024, which also requires the integer be converted into floats. Dividing floats won't be as inaccurate as dividing integers so you will get the desired scaling effect you want. BTW, it should be 1024, although 1023 is almost as good. smiley-wink
Logged


Dallas, Texas
Offline Offline
God Member
*****
Karma: 0
Posts: 861
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good catch liudr  smiley, I knew it had something to do with mixing the data types. I couldn't remember what to do to get the compiler to cooperate, hence my suggestion to convert the input explicitly to a float.

That's right, there ARE 1024 steps.
Logged

South UK
Offline Offline
God Member
*****
Karma: 2
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys smiley that worked a treat!

I have just edited it so it displays a second voltage on the second line of the screen  smiley-cool

when the inputs are not connected the float like crazy, is there a way to pull them down with software? I've just tried a 1m ohm resistor (all i found around) but I don't think that's enough resistance? For some reason it brings the voltage reading up to a shade under 5v, I can't get my head around why  smiley-confuse

Thanks
« Last Edit: July 30, 2011, 03:17:52 pm by dtokez » Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6889
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

As far as I know you don't pull up or down an analog signal. You change the signal if you pull it to 5V.
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
when the inputs are not connected the float like crazy, is there a way to pull them down with software?
Sure. Just don't call analogRead().

Quote
I've just tried a 1m ohm resistor (all i found around) but I don't think that's enough resistance?
I think you mis-spelled "too much"
Logged

South UK
Offline Offline
God Member
*****
Karma: 2
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, what do you mean to not call analogRead(), I thought that would be a fundamental part of the code?

haha thanks, I'll try some lower resistances  smiley-cool

Got it working Ok now thanks to you guys, only thing is the measurements are reading around 200 mv to much compared to a calibated Agilent multimeter smiley-sad

Any ideas how I could fix this?

thanks you lot are great!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
when the inputs are not connected the float like crazy, is there a way to pull them down with software?
Why are you trying to read from the pins if there is nothing connected? The way to avoid problems with reading from pins with nothing connected is to not read from pins with nothing connected.
Logged

South UK
Offline Offline
God Member
*****
Karma: 2
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

there will not always be an voltage connected to all three inputs, in which case id rather them read 0v than some phantom reading  smiley
got them pulling down now via a 47k resistor and it seems to work nicely

I still have the problem of the measurements being off slightly though smiley-sad is it possible to somehow calibrate it?
« Last Edit: August 01, 2011, 08:56:19 am by dtokez » Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6889
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What if you don't pull with a resistor? Do you still have offset?
If you don't connect input to analog channels, you should let it float. Forcing input is not the way to go. If you so insist it, use 500k.
Logged


South UK
Offline Offline
God Member
*****
Karma: 2
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I have been experimenting with some values, and it seems to be that the highest resistance I can go to without float is about 300k.

Id rather not pull them at al but its going to be used as test equipment so It needs to be clear if something has a fault or its just the input floating around.

I have also now built the circuit on a proto-sheild (lcd wiring etc) and it seems to be a bit more accurate... about 20mv off so I could probably get away with that smiley might have been picking up some noise or something on the breadboard I guess?

The other thing, input no.1 will be measuring 5v, at the moment my range is 0-5v so I guess it would be more sensible to measure the range of 0-10v so 5v sits in the middle.

Could anyone please advise me on how to achieve this?

Many thanks  smiley-mr-green

Logged

Pages: 1 [2] 3 4   Go Up
Jump to: