Build a step counter with the LCD

A shock switch feels every step, the 1602 LCD shows steps and distance, and a printed plate with the 9 V battery makes it a gadget you can carry.
  • Level:Intermediate
  • Duration: 1 h
  • Ages 11+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Build a step counter with the LCD – EEMENTOR STEM™ lesson

Fitness trackers count steps by feeling the small bump every time your foot lands. You can do the same with the shock switch from the sensor kit: a tiny spring inside touches a contact when it is shaken. In this lesson the UNO counts those bumps, ignores the ones that come too quickly to be steps, and shows your steps and the distance on the 1602 LCD. With the 9 V battery and a printed plate you can carry it around the house or the playground.

What you'll learn

  • How a shock (vibration) switch works
  • How to filter out bounces that are too fast to be real steps
  • How to work out distance from steps and stride length
  • How to run the UNO from a battery

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • Shock switch module (45-in-1 Sensor Kit)
  • 1602 LCD display, 10 kΩ potentiometer and 1 resistor, 220 Ω (UNO R3 Starter Kit)
  • 1 push button (UNO R3 Starter Kit)
  • 9 V battery and battery snap (UNO R3 Starter Kit)
  • The printed parts from the 3D printing box below (project plate), or the cardboard version described there
  • Breadboard and jumper wires (UNO R3 Starter Kit)

The LCD has 16 connections in a row. If yours has no pins, solder 16 pins from the 40-pin male header in the kit first, so it plugs into the breadboard. Ask an adult to help if you're new to soldering.

Wiring

Breadboard wiring diagram: Build a step counter with the LCD
Breadboard view. Check each connection against the wiring table below.
From To
LCD VSS, RW and K GND
LCD VDD 5V
LCD V0 Middle pin (W) of the potentiometer
LCD RS UNO pin 12
LCD E UNO pin 11
LCD D4, D5, D6, D7 UNO pins 5, 4, 3, 2
LCD A (backlight +) 220 Ω resistor, then 5V
Potentiometer outer pins 5V and GND
Shock switch module S UNO pin 7
Shock switch module + and − 5V and GND
Reset button, one leg UNO pin 8
Reset button, the diagonally opposite leg GND

Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.

The sketch

The LiquidCrystal library is built into the Arduino IDE. Measure your stride (ten normal steps, divided by ten) and put it into STRIDE_CM.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int SHOCK_PIN = 7;
const int RESET_PIN = 8;
const int STRIDE_CM = 60;              // your step length in centimetres
const unsigned long MIN_GAP = 300;     // two steps are at least 0.3 s apart

int restReading;
unsigned long steps = 0;
unsigned long lastStep = 0;

void show() {
  lcd.setCursor(0, 0);
  lcd.print("Steps: ");
  lcd.print(steps);
  lcd.print("     ");
  lcd.setCursor(0, 1);
  float metres = steps * STRIDE_CM / 100.0;
  if (metres < 1000) {
    lcd.print(metres, 0);
    lcd.print(" m        ");
  } else {
    lcd.print(metres / 1000.0, 2);
    lcd.print(" km       ");
  }
}

void setup() {
  pinMode(SHOCK_PIN, INPUT_PULLUP);
  pinMode(RESET_PIN, INPUT_PULLUP);
  lcd.begin(16, 2);
  lcd.print("Step counter");
  delay(1000);
  restReading = digitalRead(SHOCK_PIN);
  lcd.clear();
  show();
}

void loop() {
  if (digitalRead(SHOCK_PIN) != restReading) {        // the spring touched the contact
    unsigned long now = millis();
    if (now - lastStep > MIN_GAP) {
      steps++;
      show();
    }
    lastStep = now;                                    // bounces keep pushing the gap back
  }
  if (digitalRead(RESET_PIN) == LOW) {
    steps = 0;
    lcd.clear();
    show();
    delay(300);
  }
}

Walk with it

  1. Upload the sketch and turn the LCD's potentiometer until the text is clear.
  2. Unplug the USB cable and connect the 9 V battery to the UNO's round power jack.
  3. Hold the plate flat in front of you, or put it in a backpack, and walk 20 steps. Compare the count with your own count.
  4. If it counts too many, make MIN_GAP bigger (try 400); if it misses steps, hold it so it bumps a little more with each step.

How it works

When you shake the shock switch, its spring hits the contact many times in a few milliseconds. Counting every touch would give hundreds of "steps". The sketch counts a touch only if the previous touch was at least MIN_GAP ago, and every touch, counted or not, pushes lastStep forward. So one bump with lots of bouncing counts once, and the next real step, about half a second later, counts again.

Distance is steps times stride length. Real fitness trackers use an accelerometer like the MPU-6050 and much cleverer filters, but the idea is the same.

Try this next

  • Save the steps in EEPROM so they survive switching off.
  • Show steps per minute, a measure of how fast you walk.
  • Use the MPU-6050 instead of the shock switch and compare which counts more accurately.

Troubleshooting

  • The LCD shows only blocks or nothing: turn the potentiometer slowly from one end to the other.
  • It counts while lying still: the shock switch is very sensitive; put the plate on something soft, or make MIN_GAP bigger.
  • Nothing happens on battery: check the battery snap is in the round jack, not the USB port, and the battery isn't flat.
  • A printed part is too tight or too loose: printers differ by a few tenths of a millimetre. Sand or file a tight hole, and fix a loose part with a drop of hot glue or a strip of tape.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the kit