Skip to content

Commit 8ffc731

Browse files
committed
Create example5_faster_I2C.ino
1 parent b6f6e07 commit 8ffc731

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
example5-faster_I2C
3+
4+
This example shows how to retrieve accelerometer and gyroscopic data as fast
5+
as the I2C bus will allow.
6+
7+
If you want to run the accel and gyro at 416Hz, you need to use 400kHz I2C.
8+
See the Wire.setClock(400000); at the start of setup().
9+
10+
This example also allows you to run the accel and gyro at different rates,
11+
should you want to.
12+
13+
Tested on ATmega328P (16MHz). Please see this issue for more detail:
14+
https://github.com/sparkfun/SparkFun_6DoF_ISM330DHCX_Arduino_Library/issues/7#issuecomment-1295767690
15+
16+
Please refer to the header file for more possible settings, found here:
17+
..\SparkFun_6DoF_ISM330DHCX_Arduino_Library\src\sfe_ism330dhcx_defs.h
18+
19+
Written by Paul Clark @ SparkFun Electronics, October 2022
20+
Based on original work by Elias Santistevan
21+
22+
Product:
23+
24+
https://www.sparkfun.com/products/19764
25+
26+
Repository:
27+
28+
https://github.com/sparkfun/SparkFun_6DoF_ISM330DHCX_Arduino_Library
29+
30+
SparkFun code, firmware, and software is released under the MIT
31+
License (http://opensource.org/licenses/MIT).
32+
*/
33+
34+
#include <Wire.h>
35+
#include "SparkFun_ISM330DHCX.h"
36+
37+
SparkFun_ISM330DHCX myISM;
38+
39+
// Structs for X,Y,Z data
40+
sfe_ism_data_t accelData;
41+
sfe_ism_data_t gyroData;
42+
43+
void setup()
44+
{
45+
46+
Wire.begin();
47+
48+
// Uncomment the next line to use 400kHz I2C. Essential when running the accel and gyro at 416Hz or faster.
49+
//Wire.setClock(400000);
50+
51+
Serial.begin(115200);
52+
53+
while (!myISM.begin())
54+
{
55+
Serial.println("ISM did not begin. Please check the wiring...");
56+
delay(1000);
57+
}
58+
59+
// Reset the device to default settings. This if helpful is you're doing multiple
60+
// uploads testing different settings.
61+
myISM.deviceReset();
62+
63+
// Wait for it to finish reseting
64+
while (!myISM.getDeviceReset())
65+
{
66+
delay(1);
67+
}
68+
69+
Serial.println("Reset.");
70+
Serial.println("Applying settings.");
71+
delay(100);
72+
73+
myISM.setDeviceConfig();
74+
myISM.setBlockDataUpdate();
75+
76+
// Set the output data rate and precision of the accelerometer
77+
// Note: we're using 208Hz for accel and 104Hz for gyro
78+
myISM.setAccelDataRate(ISM_XL_ODR_208Hz);
79+
myISM.setAccelFullScale(ISM_4g);
80+
81+
// Set the output data rate and precision of the gyroscope
82+
// Note: we're using 208Hz for accel and 104Hz for gyro
83+
myISM.setGyroDataRate(ISM_GY_ODR_104Hz);
84+
myISM.setGyroFullScale(ISM_250dps);
85+
86+
// Turn on the accelerometer's filter and apply settings.
87+
myISM.setAccelFilterLP2();
88+
myISM.setAccelSlopeFilter(ISM_LP_ODR_DIV_100);
89+
90+
// Turn on the gyroscope's filter and apply settings.
91+
myISM.setGyroFilterLP1();
92+
myISM.setGyroLP1Bandwidth(ISM_MEDIUM);
93+
}
94+
95+
void loop()
96+
{
97+
float totalAccelX = 0; // Sum the accel readings
98+
float totalAccelY = 0;
99+
float totalAccelZ = 0;
100+
float countA = 0; // Count the readings
101+
float totalGyroX = 0; // Sum the gyro readings
102+
float totalGyroY = 0;
103+
float totalGyroZ = 0;
104+
float countG = 0; // Count the readings
105+
106+
Serial.println("Recording accel and gyro data for 10 seconds...");
107+
Serial.println();
108+
delay(10); // Wait for the Serial.print to complete
109+
110+
unsigned long startTime = millis(); // Keep track of time
111+
112+
// Note: we can't do prints inside the while loop. They slow things down too much...
113+
114+
while (millis() < (startTime + 10000)) // Loop for 10 seconds
115+
{
116+
// Check if accel data is available.
117+
if (myISM.checkAccelStatus())
118+
{
119+
myISM.getAccel(&accelData); // Read the accel data
120+
totalAccelX += accelData.xData; // Sum it
121+
totalAccelY += accelData.yData;
122+
totalAccelZ += accelData.zData;
123+
countA += 1; // Increment the count
124+
}
125+
126+
// Check if gyro data is available.
127+
if (myISM.checkGyroStatus())
128+
{
129+
myISM.getGyro(&gyroData); // Read the gyro data
130+
totalGyroX += gyroData.xData; // Sum it
131+
totalGyroY += gyroData.yData;
132+
totalGyroZ += gyroData.zData;
133+
countG += 1; // Increment the count
134+
}
135+
}
136+
137+
Serial.print("Read ");
138+
Serial.print(countA, 0);
139+
Serial.println(" Accel values.");
140+
Serial.println("Accel averages are:");
141+
Serial.println(totalAccelX / countA);
142+
Serial.println(totalAccelY / countA);
143+
Serial.println(totalAccelZ / countA);
144+
Serial.println();
145+
Serial.print("Read ");
146+
Serial.print(countG, 0);
147+
Serial.println(" Gyro values.");
148+
Serial.println("Gyro averages are:");
149+
Serial.println(totalGyroX / countG);
150+
Serial.println(totalGyroY / countG);
151+
Serial.println(totalGyroZ / countG);
152+
Serial.println();
153+
154+
Serial.println("Final readings were:");
155+
Serial.print("Accel: ");
156+
Serial.print("X: ");
157+
Serial.print(accelData.xData);
158+
Serial.print(" ");
159+
Serial.print("Y: ");
160+
Serial.print(accelData.yData);
161+
Serial.print(" ");
162+
Serial.print("Z: ");
163+
Serial.print(accelData.zData);
164+
Serial.println(" ");
165+
Serial.print("Gyro: ");
166+
Serial.print("X: ");
167+
Serial.print(gyroData.xData);
168+
Serial.print(" ");
169+
Serial.print("Y: ");
170+
Serial.print(gyroData.yData);
171+
Serial.print(" ");
172+
Serial.print("Z: ");
173+
Serial.println(gyroData.zData);
174+
Serial.println();
175+
176+
delay(1000);
177+
}

0 commit comments

Comments
 (0)