analogRead give more than 0-1023????

I am playing with my first Arduino (Freeduino actually) and I have a number of different sketches working but the analogRead command isn't giving me a output that matches what the manual says it should. From what I am reading on the Arduino.cc site it should be limited to the range of 1-1023 but I am getting way more than that. It seems to be a range as high as 9999 and at the top end and the low it jumps around as if it was looping. I have tried this with more than one analog input and they seem to all act the same. The following is the simple sketch that I am using right now to test.

/*
Analog Input
*/
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(1, 6, 5, 4, 3, 2);

int sensorPin = 0;
int ledPin = 13;
int sensorValue = 0;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);

 lcd.setCursor(0, 1);
 // print the number of seconds since reset:
  lcd.print(sensorValue);
}

Am I missing something simple as a noob to this or ??????? Could the A/D be bad?

Yes! I can actually help someone :smiley:

You want to have the sensor pin equal to "A0." Analog pins go A0, A1, A2, etc. Digital pins are 0, 1, 2, etc.

Good luck.

John

Learned something new there. :slight_smile: But I changed that and still got the same results? Have tried two different pots at this point on 2 different inputs and they are the same.

Oh, you also haven't set the pin mode.

Place this into the setup().

pinMode(A0, INPUT);

When in analog mode, pinMode doesnt matter.

Oh. Okay. Well, I got my Arduino on the 5th, so I am learning as well. I have only used one potentiometer, but that was to control the display brightness of my LCD screen.

You want to have the sensor pin equal to "A0." Analog pins go A0, A1, A2

There's nothing at all wrong with using 0,1,2,3,4,5 for analogue pin numbers when used with analogRead.
The "A0" etc constants are a convenience and aide-memoire - the same constant can be used for the pin as analogue and digital.

Okay. -.- I thought I was helping. Then do you think the problem stands with the hardware?

No, I suspect the software - maybe leading digits that aren't erased.
A few simple serial prints should very quickly show the problem.

Try adding some markers & spaces around the print to see the sensorValue
lcd.print(" [");lcd.print(sensorValue);lcd.print("] ");

Very interesting, that is what it was.

robtillaart:
Try adding some markers & spaces around the print to see the sensorValue
lcd.print(" [");lcd.print(sensorValue);lcd.print("] ");

I can't say I understand what is going on but it worked, even if I just do the following it works.
lcd.print("[");lcd.print(sensorValue);lcd.print("]");

Confirming that it was getting a correct reading I then put in the following:
sensorValue = sensorValue * 0.00488;
and I was able to get the major voltage i.e. 1 ,2 ,3, 4, just need to figure out how to get the minor of it, .25 etc, so a total voltage reading of 3.25 as a example.

But knowing that I have the analogRead working really helps. Thanks robtillaart and all.

phonnold:
and I was able to get the major voltage i.e. 1 ,2 ,3, 4, just need to figure out how to get the minor of it, .25 etc, so a total voltage reading of 3.25 as a example.

Another total noob here but I would think changing sensorValue to a float instead of a int would get you what you want.

Glenn

Well that worked as well. Making sensorValue a float allowed me to use my first lcd.print command of:

lcd.print(sensorValue);

The values just had a .00 after them, which is no big deal as I really wanted to just use it to find voltage anyway. Having it as a float gives me what I was looking for anyway.

Can't say I understand why it being a int causes it a problem??????????

Have not to this point played with analogue in and I am not a electronics guy,
but the equation sensorValue = sensorValue * 0.00488; should give a float type value.

Wonder what is going on.

What type of parameter is lcd.print expecting?

Maybe it does not like to recieve only an int ?

If you look at the reference for lcd.print(), you will find that it expects an integer type or a string. It cannot directly print a float.

When you print values like:
1019 then 109 then 19 then 9 you will have on your display 9999 because the right part is not erased.

better do something like this:

int sensorValue;
char outb[8];
sprintf(outb,"%4d", sensorValue);
lcd.print(outb);

for the float visualization this could be appropriate:

float sensorValue;
sensorValue=analogRead(0);
sensorValue  *=  (5.0/1024.0);
char outb[10];
sprintf(outb,"%d.%03d V", (int)floor(sensorValue),(int)((sensorValue-floor(sensorValue))*1000));
lcd.print(outb);

hope that helps