Go Down

Topic: Arduino or user problem? (Read 860 times) previous topic - next topic

ReySpike

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?


Code: [Select]
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);                     
}

Grag38

#1
May 14, 2011, 02:20 pm Last Edit: May 14, 2011, 02:23 pm by Grag38 Reason: 1
add pinMode(analogOutPin, OUTPUT); in setup

but if the reads of analorRead is 0 1023 with random, there is an electrical problem...  What is the sensor ?

ReySpike

I added it, but it made no difference.  I didn't think analog outs did not need to be declared (am I mistaken?).

My sensor in this case is two resistors set up in series between 5vdc and gnd.  The analog pin is tapped between the resistors... The value should be steady.  (I went with simple when I couldn't figure out what the problem was, I also changed my sketch to the sample version on the arduino page to rule that out)

I have tried this with several different pots, several input pins and several parts of a breadboard.  That is why I was starting to think this might be a board problem (I have only used PWM pins on the board before this project, so this is the first use of these input pins).

ReySpike

Hah, so I figured out what it was... there was a dead spot on both of my bread boards (same area on both)...  grrrr. :0

Go Up