Arduino 101 CurieIMU Step Count

With this tutorial you learn to use the step counting feature of the IMU.

With this tutorial you learn to use the step counting feature of the IMU. You also learn to set the step detection mode and the interrupt based step counting. As an alternative, you can use a timed reading loop (polling) for a more relaxed output on the Serial monitor.

Hardware Required

The Circuit

genuino101fzz

image developed using Fritzing. No additional hardware is needed to use this tutorial.

Software Essentials

Libraries

CurieIMU.h is the library that gives access to all the parameters, features and readings of the IMU chip of the 101 board. This unit contains a three axes accelerometer and a three axes gyroscope. This library is part of the 101 board core and it is loaded together with the core files for Arduino 101. In this tutorial we set up the Step detection feature and read the steps either with interrupt or polling.

Functions

updateStepCount() - This function reads, when called, the number of steps counted since the initialisation of the IMU sensor. If the number is the same of the last reading, nothing happens, else the new value is printed on the Serial monitor and stored.

Code

This sketch supports both interrupt driven and polling step counting. The Sketch is set up for event driven (interrupt) reading, but you may go to 1s polling changing this line from

boolean stepEventsEnabeled = true;
to
boolean stepEventsEnabeled = false;
. The callback function that manages the interrupt, simply calls the updateStepCount() function at every step. This solution offers a more responsive reporting on the Serial monitor.

1/*
2
3 * Copyright (c) 2016 Intel Corporation. All rights reserved.
4
5 * See the bottom of this file for the license terms.
6
7 */
8
9/*
10
11 This sketch example demonstrates how the BMI160 accelerometer on the
12
13 Intel(R) Curie(TM) module can be used as a Step Counter (pedometer)
14
15*/
16
17#include "CurieIMU.h"
18
19/* To get an interrupt notification for every step detected,
20
21 set stepEventsEnabeled to true. Otherwise, the main loop will
22
23 poll for the current step count.
24
25 By design, the step counter does not immediately update on every step detected.
26
27 Please refer to Section 2.7 of the BMI160 IMU SensorData Sheet
28
29 for more information on this feature.
30
31*/
32
33const int ledPin = 13;
34
35bool stepEventsEnabeled = true; // whether you're polling or using events
36long lastStepCount = 0; // step count on previous polling check
37
38bool blinkState = false; // state of the LED
39
40void setup() {
41
42 Serial.begin(9600); // initialize Serial communication
43
44 while(!Serial) ; // wait for serial port to connect.
45
46 // pinMode(13, OUTPUT);
47
48 // initialize the sensor:
49
50 CurieIMU.begin();
51
52 // turn on step detection mode:
53
54 CurieIMU.setStepDetectionMode(CURIE_IMU_STEP_MODE_NORMAL);
55
56 // enable step counting:
57
58 CurieIMU.setStepCountEnabled(true);
59
60 if (stepEventsEnabeled) {
61
62 // attach the eventCallback function as the
63
64 // step event handler:
65
66 CurieIMU.attachInterrupt(eventCallback);
67
68 CurieIMU.interrupts(CURIE_IMU_STEP); // turn on step detection
69
70 Serial.println("IMU initialisation complete, waiting for events...");
71
72 }
73}
74
75void loop() {
76
77 /* Instead of using step detection event notifications,
78
79 we can check the step count periodically */
80
81 if (!stepEventsEnabeled) {
82
83 updateStepCount();
84
85 }
86
87 digitalWrite(13, blinkState);
88
89 blinkState = !blinkState;
90
91 delay(1000);
92}
93
94static void updateStepCount() {
95
96 // get the step count:
97
98 int stepCount = CurieIMU.getStepCount();
99
100 // if the step count has changed, print it:
101
102 if (stepCount != lastStepCount) {
103
104 Serial.print("Step count: ");
105
106 Serial.println(stepCount);
107
108 // save the current count for comparison next check:
109
110 lastStepCount = stepCount;
111
112 }
113}
114
115static void eventCallback(void) {
116
117 if (CurieIMU.stepsDetected())
118
119 updateStepCount();
120}
121
122/*
123
124 Copyright (c) 2016 Intel Corporation. All rights reserved.
125
126 This library is free software; you can redistribute it and/or
127
128 modify it under the terms of the GNU Lesser General Public
129
130 License as published by the Free Software Foundation; either
131
132 version 2.1 of the License, or (at your option) any later version.
133
134 This library is distributed in the hope that it will be useful,
135
136 but WITHOUT ANY WARRANTY; without even the implied warranty of
137
138 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
139
140 Lesser General Public License for more details.
141
142 You should have received a copy of the GNU Lesser General Public
143
144 License along with this library; if not, write to the Free Software
145
146 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
147
148*/

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.