I have a ATMega1280 and am seeing varying values when using the analogRead command from a pot without moving it. To isolate the problem, I replaced the pot with two series 10k ohm resistors between +5Vdc and GND with A8 tapping in between the two resistors
Running the below sketch, the sensorValue seems to randomly change between 0 and 1023 instead of being a constant value like I would expect. This doesnt make sense to me, so I thought I would check here to see if I am missing something or if I have a bad board.
I have tried several input pins, a few different resitor values and have checked the physical set up several times.
What am I missing?
const int analogInPin = A8; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 3; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print(sensorValue);
delay(500);
}