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? reference here

  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() {
  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
  digitalWrite(groundpin, LOW);
  digitalWrite(powerpin, HIGH);
  pinMode(stpin, OUTPUT);
  digitalWrite(stpin, LOW);

void loop() {
  • 6
    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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.