Build a DIY Arduino Pedometer using Accelerometer

Source and idea from Circuit Digest.

Your fitness is your most important asset. It is even more crucial during this pandemic. While you can find a lot of fitness bands and other gadgets designed to help you measure certain aspects of health, you can’t always have a cheaper solution. So in this post, we are sharing a cost-effective solution with a DIY Arduino pedometer so you can keep your physical activity in check. With this easy-to-use device, you can count your steps and keep track of your physical activity without any technical hassle.

To make things even more effective, you can easily integrate it with the Arduino smartwatch.

So let’s get started.

Step 1: Gather all the components

For this project, you’ll need:

Before we get into the process, let’s go over the key component and its functionality.

ADXL335 Accelerometer

This is a 3-axis analog accelerometer that uses capacity senses to function. It is a thin, small, and low-power module that comes with a polysilicon surface-micro machined sensor that prompts conditioning circuitry. The ADXL335 can measure both static and dynamic acceleration. In this project, it will function as a pedometer sensor.

An accelerometer measures the movement of a vibrating body. We are using a capacitive accelerometer that measures the acceleration of a surface using its capacitive sensing function. See the images below.

The following images show the front and back of the accelerometer:

Pin description for ADXL335

Here are the pin connections for the accelerometer:

⦁ X-out will give analog output in the x-direction.
⦁ VCC should connect to the 5V supply.
⦁ Y-out will give analog output in the y-direction
⦁ GND is the Ground pin.
⦁ ST pin will be used to measure the sensitivity of the sensor.
⦁ Z-out will give analog output in the z-direction.

Step 2: Use the circuit diagram for connections

Below is the circuit diagram for the DIY Arduino pedometer:

As shown in the circuit diagram, you can interface ADXL335 with Arduino Nano. The X, Y, and Z pins will connect with analog pins A1, A2, and A3 of Arduino. The 12C module will be used to interface the LCD module with Arduino.

The A5 and A4 Arduino Nano pins will connect to the SCL and SDA pins of the 12C module respectively. See the complete connections list below:

ADXL335Arduino Nano
GNDGND
VCC3.3V
ZA3
YA2
XA1
LCD I2C ModuleArduino Nano
GNDGND
SCLA5
SDAA4
VCC5V

Step 3: Build the DIY Arduino Pedometer setup

First, you can build the setup on a breadboard, and after testing move to the perfboard as shown in the images below:

Here, it is important to explain the working of a pedometer so you can easily understand how our components will interact.

A conventional pedometer measures the total number of steps taken by a person using 3 different components of motion known as vertical, forward, and side. The accelerometer measures these motions. In this project, the accelerometer will continuously update the max and min values of the 3-axis acceleration based on the defined samples. The average 3-axis value is known as the dynamic threshold level, and it is used to measure whether a step was taken. This threshold can be measured by dividing the maximum and the minimum values by 2. The DIY Arduino pedometer works through the following steps:

1) As soon as the power is supplied, the pedometer will start the calibration.

2) Through the void loop function, it will get continuous data from the X, Y, and Z-axis.

3) Then, it will calculate the total acceleration vector from the beginning point.

4) Acceleration vector is calculated by taking the square root of the X, Y, and Z-axis values (x^2+y^2+z^2).

5) After that, the pedometer will compare the average and threshold values to count the steps.

6) If the acceleration vector is above the threshold value, it increases the step counts; if not, it ignores the invalid vibrations.

Step 4: Writing the code for the pedometer

Now that we understand the working of our DIY Arduino Pedometer, it’s time to write the code.

We’ll start by including all the necessary libraries. Note that ADXL335 does not require any specific libraries as it offers analog output.

#include <LiquidCrystal_I2C.h>

Next, define the Arduino pins.

const int xpin = A1;
const int ypin = A2;
const int zpin = A3;

In the next line, define the threshold value. This value will be compared to the acceleration vector to measure the number of steps taken.

float threshold = 6;

Define the calibration inside the void setup function as shown below.

calibrate();

Next, in the void loop function, define the X, Y, and Z values for a hundred samples. See the code below.

for (int a = 0; a < 100; a++)
  {
    xaccl[a] = float(analogRead(xpin) - 345);
    delay(1);
    yaccl[a] = float(analogRead(ypin) - 346);
    delay(1);
    zaccl[a] = float(analogRead(zpin) - 416);
    delay(1);

After you have the 3-axis values, measure the total acceleration vector. You will do so by taking the square root of the 3 axis (X, Y, and Z).

totvect[a] = sqrt(((xaccl[a] - xavg) * (xaccl[a] - xavg)) + ((yaccl[a] - yavg) * (yaccl[a] - yavg)) + ((zval[a] - zavg) * (zval[a] - zavg)));

Now calculate the average of the max and min acceleration vector values.

totave[a] = (totvect[a] + totvect[a - 1]) / 2 ;

Next, compare the average with the threshold as shown below. If the average is greater than the threshold value, increase the step count and raise the flag.

if (totave[a] > threshold && flag == 0)
    {
      steps = steps + 1;
      flag = 1; }

If the average is more than the threshold value but the flag is raised, take no further action.

else if (totave[a] > threshold && flag == 1)
    {
      // Don’t Count
    }

If the average is less than the threshold value and the flag is raised, put the flag down.

if (totave[a] < threshold   && flag == 1)
    {
      flag = 0;
    }

As the final step, print the total number of steps on the LCD and the serial monitor.

Serial.println(steps );
lcd.print("Steps: ");
lcd.print(steps);

Step 5: Test your pedometer

Once the code and the hardware are ready, it’s time to connect the Arduino to your personal computer or laptop. Upload the code, grab the pedometer, and start walking. If everything has been done right, you should be able to see the number of steps appear on the LCD.

Your final apparatus should look something like this:

Note: it may show more or less steps if the pedometer vibrates rapidly or slowly.

Below is the complete code for DIY Arduino pedometer.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int xpin = A1;
const int ypin = A2;
const int zpin = A3;
byte p[8] = {
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F
};
float threshold = 6;
float xval[100] = {0};
float yval[100] = {0};
float zval[100] = {0};
float xavg, yavg, zavg;
int steps, flag = 0;
void setup()
{
  Serial.begin(9600);
  lcd.begin();
  lcd.backlight();
  lcd.clear();
  calibrate();
}
void loop()
{
  for (int w = 0; w < 16; w++) {
    lcd.write(byte(0));
    delay(500);
  }
  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 a = 0; a < 100; a++)
  {
    xaccl[a] = float(analogRead(xpin) - 345);
    delay(1);
    yaccl[a] = float(analogRead(ypin) - 346);
    delay(1);
    zaccl[a] = float(analogRead(zpin) - 416);
    delay(1);
    totvect[a] = sqrt(((xaccl[a] - xavg) * (xaccl[a] - xavg)) + ((yaccl[a] - yavg) * (yaccl[a] - yavg)) + ((zval[a] - zavg) * (zval[a] - zavg)));
    totave[a] = (totvect[a] + totvect[a - 1]) / 2 ;
    Serial.println("totave[a]");
    Serial.println(totave[a]);
    delay(100);
    if (totave[a] > threshold && flag == 0)
    {
      steps = steps + 1;
      flag = 1;
    }
    else if (totave[a] > threshold && flag == 1)
    {
      // Don't Count
    }
    if (totave[a] < threshold   && flag == 1)
    {
      flag = 0;
    }
    if (steps < 0) {
      steps = 0;
    }
    Serial.println('\n');
    Serial.print("steps: ");
    Serial.println(steps);
    lcd.print("Steps: ");
    lcd.print(steps);
    delay(1000);
    lcd.clear();
  }
  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) - 345);
    sum = xval[i] + sum;
  }
  delay(100);
  xavg = sum / 100.0;
  Serial.println(xavg);
  for (int j = 0; j < 100; j++)
  {
    yval[j] = float(analogRead(ypin) - 346);
    sum1 = yval[j] + sum1;
  }
  yavg = sum1 / 100.0;
  Serial.println(yavg);
  delay(100);
  for (int q = 0; q < 100; q++)
  {
    zval[q] = float(analogRead(zpin) - 416);
    sum2 = zval[q] + sum2;
  }
  zavg = sum2 / 100.0;
  delay(100);
  Serial.println(zavg);
}

Get started with your Arduino Nano from Amazon here.

We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: