I have it connected to pin A0, 5v and gnd and I wrote a sketch to read the analog input but sometimes the serial prints 0 and other numbers with little consistency. I am going to assume user error since this is my first time mounting a sensor. But can't seem to fig out what is wrong.
int sensorPin = A0; // input pin for the IR sensor
int sensorValue = 0; // variable to store the value coming from the sensor
void setup(void)
{
Serial.begin(9600); //set baud rate
}
void loop()
{
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue, DEC);
delay(1000);
}
Any ideas why it is not reading the correct values?
A0 is a predefined constant (or macro?) and you have assigned an int of the same name, I am not sure that will work correctly? Easy to try just a 0 and see.
I had to use A7 for the input pins when I was programming my analog buttons on the same controller (different project) so I assume I'd use the same format for these analog buttons.
That may be normal for your sensor/wiring setup. Wire a jumper from ground to analog pin 0 and run your program, do you get a steady 0? Then wire it +5vdc, do you then get a steady 1023?