Measuring steps using the ADXL335 accelerometer

In this project I will tell you how to make a simple fitness tracker (step counter) using Arduino. In this project we will be using a simple ADXL335 accelerometer to count the number of times our hand accelerates and decelerates and count it as one step.

What does the ADXL335 do?


The ADXL335 is a triple axis accelerometer with extremely low noise and power consumption  The and has a full sensing range of +/- 3g. It has a voltage consumption of between 1.8 and 3.6Volts DC.
The ADXL335

Materials required

  • An Arduino uno
  • An ADXL335 accelerometer
  • A few male to femalejumper wires

Making the Arduino connections

The connections for this project are very easy.

  • Connect the pin labelled VCC on the ADXL335 to the pin labelled A4 on the Arduino. You can also connect it to the in labelled 3.3V on the Arduino.
  • Connect the GND on the ADXL335 to the pin labelled A0 on the Arduino. You can also connect it eo the GND of the Arduino
  • Connect the X_OUT on the ADXL335 to the pin labelled A3 on the Arduino
  • Connect the Y_OUT on the ADXL335 to the pin labelled A2 on the Arduino
  • Connect the Z_OUT on the ADXL335 to the pin labelled A1 on the Arduino

Coding

The basic structure of our code is as follows

  • On powering up the board, continuously read the data using the  accelerometer
  • Calculate the total acceleration vector with respect to starting point 
  • Acceleration vector = sqrt (x^2 + y^2 + z^2)
  • Analyze the data for setting up a threshold using trial and error
  • If acceleration vector crosses threshold , steps ++
  • display number of steps
The code

  int xpin=A3;
  int ypin=A2;
  int zpin=A1;
  int powerpin=A4;
  int gnd=A0;
  float threshhold=80.0;
  float xval[100]={0};
  float yval[100]={0};
  float zval[100]={0};
  float xavg;
  float yavg;
  float zavg;
  int steps = 0;
  int state=0;
  void setup()
  {
  Serial.begin(9600);
  pinMode(powerpin,OUTPUT);
  pinMode(gnd,OUTPUT);
  digitalWrite(powerpin,HIGH);
  digitalWrite(gnd,LOW);
  pinMode(13,OUTPUT);
  calibrate();
  }void loop()
  {
  int acc=0;
  float totvect[100]={0};
  float totave[100]={0};
  float xaccl[100]={0};
  float yaccl[100]={0};
  float zaccl[100]={0}; 
  for (int i=0;i<100;i++){
  xaccl[i]=float(analogRead(xpin));
  yaccl[i]=float(analogRead(ypin));
  zaccl[i]=float(analogRead(zpin));
  totvect[i] = sqrt(((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)) + ((zval[i] - zavg)*(zval[i] -zavg)));
  totave[i] = (totvect[i] + totvect[i-1]) / 2 ;
  Serial.println(totave[i]);
  delay(200);
  //calculating number of steps 
  if (totave[i]>threshhold && state==0){
  steps=steps+1;
  state=1;
  }
  if (totave[i] <threshhold  && state==1)
  {state=0;}
  Serial.println('\n');
  Serial.print("steps=");
  Serial.println(steps);
  };
  delay(1000);
  }
  void calibrate(){
  float sum=0;
  float sum1=0;
  float sum2=0;
  for (int i=0;i<100;i++){
  xval[i]=float(analogRead(xpin));
  sum=xval[i]+sum;
  }
  delay(100);
  xavg=sum/100.0;
  Serial.println(xavg);
  for (int j=0;j<100;j++){
  xval[j]=float(analogRead(xpin));
  sum1=xval[j]+sum1;
  }
  yavg=sum1/100.0;
  Serial.println(yavg);
  delay(100);
  for (int i=0;i<100;i++){
  zval[i]=float(analogRead(zpin));
  sum2=zval[i]+sum2;
  }
  zavg=sum2/100.0;
  delay(100);
  Serial.println(zavg); 
  }

Follow this space for more such cool projects..!! See you back soon..!!

Comments

  1. hi, i was testing the code of the pedometer and i ran into some problems.
    It turns out that the accelerometer is very sensitive and detects all movements, this is already known. The issue is that at times there is a lot of noise and I cannot distinguish in the graph between one step and another, can I explain? this sometimes causes extra steps to be added.

    ReplyDelete
    Replies
    1. Hello this is a old page of mine and I no longer follow it but I can help you out if u still need help . Here is my discord id Sherr#3741. Sorry for the late reply

      Delete
    2. Oh wait I used a wrong account

      Delete

Post a Comment