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?
Learned something new there. 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. 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.
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.
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.