Arduino 101 CurieIMU Accelerometer Orientation

With this tutorial you convert the accelerometer readings into an orientation on the 101 board.

With this tutorial you convert the accelerometer readings into an orientation. The orientation is then printed on the Serial monitor as text that tells you the position of the processor, the USB connector and the pins as reference.

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 read the raw accelerometer values.

Functions

None

Code

The orientation of the board is calculated using the orientation of the three axes. The Z axis passes through the pcb: component side upwards is positive, while going below is negative. The X axis goes from USB connector to Bluetooth® antenna if positive, while Y axis goes from analog and power pins to digital pins when positive. Using the three axes signs the sketch calculates the orientation of the whole board.

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 on the
12
13 Intel(R) Curie(TM) module can be used to read accelerometer data
14
15 and translate it to an orientation
16
17*/
18
19#include "CurieIMU.h"
20
21int lastOrientation = - 1; // previous orientation (for comparison)
22
23void setup() {
24
25 Serial.begin(9600); // initialize Serial communication
26
27 while (!Serial); // wait for the serial port to open
28
29 // initialize device
30
31 Serial.println("Initializing IMU device...");
32
33 CurieIMU.begin();
34
35 // Set the accelerometer range to 2G
36
37 CurieIMU.setAccelerometerRange(2);
38}
39
40void loop() {
41int orientation = - 1; // the board's orientation
42
43 String orientationString; // string for printing description of orientation
44
45 /*
46
47 The orientations of the board:
48
49 0: flat, processor facing up
50
51 1: flat, processor facing down
52
53 2: landscape, analog pins down
54
55 3: landscape, analog pins up
56
57 4: portrait, USB connector up
58
59 5: portrait, USB connector down
60
61 */
62
63 // read accelerometer:
64
65 int x = CurieIMU.readAccelerometer(X_AXIS);
66
67 int y = CurieIMU.readAccelerometer(Y_AXIS);
68
69 int z = CurieIMU.readAccelerometer(Z_AXIS);
70
71 // calculate the absolute values, to determine the largest
72
73 int absX = abs(x);
74
75 int absY = abs(y);
76
77 int absZ = abs(z);
78
79 if ( (absZ > absX) && (absZ > absY)) {
80
81 // base orientation on Z
82
83 if (z > 0) {
84
85 orientationString = "up";
86
87 orientation = 0;
88
89 } else {
90
91 orientationString = "down";
92
93 orientation = 1;
94
95 }
96
97 } else if ( (absY > absX) && (absY > absZ)) {
98
99 // base orientation on Y
100
101 if (y > 0) {
102
103 orientationString = "digital pins up";
104
105 orientation = 2;
106
107 } else {
108
109 orientationString = "analog pins up";
110
111 orientation = 3;
112
113 }
114
115 } else {
116
117 // base orientation on X
118
119 if (x < 0) {
120
121 orientationString = "connector up";
122
123 orientation = 4;
124
125 } else {
126
127 orientationString = "connector down";
128
129 orientation = 5;
130
131 }
132
133 }
134
135 // if the orientation has changed, print out a description:
136
137 if (orientation != lastOrientation) {
138
139 Serial.println(orientationString);
140
141 lastOrientation = orientation;
142
143 }
144}
145
146/*
147
148 Copyright (c) 2016 Intel Corporation. All rights reserved.
149
150 This library is free software; you can redistribute it and/or
151
152 modify it under the terms of the GNU Lesser General Public
153
154 License as published by the Free Software Foundation; either
155
156 version 2.1 of the License, or (at your option) any later version.
157
158 This library is distributed in the hope that it will be useful,
159
160 but WITHOUT ANY WARRANTY; without even the implied warranty of
161
162 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
163
164 Lesser General Public License for more details.
165
166 You should have received a copy of the GNU Lesser General Public
167
168 License along with this library; if not, write to the Free Software
169
170 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
171
172*/

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.