Problems with analogRead()

I have a Seeeduino mega.

I want to read several analog values. (Sharp IR, Adxl312, LDR, Microphon)

When I Read the Values they switch very fast.

I 'll give you an exmample. My ADXL312 (Accelerometer) lays flat on the table and the value switches very fast (501, 512, 521, 498, 504, 517.....)

You know what i want to explain?

In only one case it works. If I use a potentiometer.

I hope my english is not soo bad, that you can't understand what I mean.

Thanks, Chris.

Without seeing your code, it is difficult to say. One reason could be because you don't have a delay in your code after taking a reading and then displaying. It is taking readings and updating as fast as it can. The other answer could be that you may need to use a capacitor and resistor on your accelerometer output to "slow it down". The accelerometer output can change pretty fast. I had experimented with one a while back and I wound up using a capacitor and resistor from the output of the accelerometer to ground. By sizing the cap and resistor appropriately, you can keep the output from changing so quickly.

Discussed over on Adafruit...

Delay between the readings isn't the right answer. The Reading only goes slower but it switches still too much.

Have you read this one?:-

Yes and i’ve tried with delay.

If i disconnect all other sensors and only read the X-axis from the accelleromter in doesn’t work.

 int sensorPin = 0;    
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  // turn the ledPin on
  if (sensorValue<=493 || sensorValue>=510) {
  digitalWrite(ledPin, HIGH);  
  // stop the program for <sensorValue> milliseconds:
  else {      
  digitalWrite(ledPin, LOW);

The LED is blinking all the time, because the value switches back and forward.

You could try averaging the readings, either with a running average or taking say 10 samples and dividing the total by 10.

I thinks it’s failure of the sensor. I think i have too give it back to the store.



  Reads repeatedly from an analog input, calculating a running average
  and printing it to the computer.  Keeps ten readings in an array and 
  continually averages them.
  The circuit:
    * Analog sensor (potentiometer will do) attached to analog input 0

  Created 22 April 2007
  By David A. Mellis  <>


// Define the number of samples to keep track of.  The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input.  Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 30;

int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = 0;

void setup()
  // initialize serial communication with computer:
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;          

void loop() {
  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
    // ...wrap around to the beginning: 
    index = 0;                           

  // calculate the average:
  average = total / numReadings;         
  // send it to the computer (as ASCII digits) 
  Serial.println(average, DEC); 

if (average<=493 || average>=510) {
  digitalWrite(13, HIGH);  
  // stop the program for <sensorValue> milliseconds:
  else {      
  digitalWrite(13, LOW);

Now it works, but I don’t think that 's normal. There are so many different howtos for this accellerometer and they don’t use any of this tweaks.

Thanks Chris