# Why is there no smooth value from ADXL335?

I have written this code to read x-acceleration from ADXL335.

1. Why is the value toggling between 342, 343, 344 even at rest? Is there anything I need to do to get a smooth curve? 2. How to increase resolution? (I'm using a Mega 2560)

``````int Xcurr,Ycurr,Zcurr;
int Xpre=0,Ypre=0,Zpre=0;
int res=0;
// these constants describe the pins. They won't change:
const int groundpin = A1;
// analog input pin 4 -- ground
const int powerpin = A0;
// analog input pin 5 -- voltage
const int xpin = A3;
// x-axis of the accelerometer
const int ypin = A2;
// y-axis
const int zpin = A4;
// z-axis (only on 3-axis models)
const int stpin = A5;

void setup() {
Serial.begin(9600);
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
pinMode(stpin, OUTPUT);
digitalWrite(stpin, LOW);
}

void loop() {
Xcurr=analogRead(xpin);
Serial.print(Xcurr);Serial.print("\n");
delay(100);
}
``````
• It's called LSB noise, and it is what the ADC does. Just average the values over time (rolling average) to smooth it. – Majenko Mar 19 '17 at 9:44

## 1 Answer

It is called moving average, the calculations you can use to prevent such problems can be found at wikipedia

In principle, what you do is take the average over the last x measurements to take away the peaks.