-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesp32mpu9250.txt
54 lines (42 loc) · 1.41 KB
/
esp32mpu9250.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//I used the https://github.com/asukiaaa/MPU9250_asukiaaa - it was the easiest to use, I had to change the SDA and SCL defines for my LOLIN32 board from the default
//#define SDA_PIN 21 #define SCL_PIN 22
#include <MPU9250_asukiaaa.h>
#ifdef _ESP32_HAL_I2C_H_
#define SDA_PIN 21
#define SCL_PIN 22
#endif
MPU9250 mySensor;
void setup() {
while(!Serial);
Serial.begin(115200);
Serial.println("started");
#ifdef _ESP32_HAL_I2C_H_
// for esp32
Wire.begin(SDA_PIN, SCL_PIN); //sda, scl
#else
Wire.begin();
#endif
mySensor.setWire(&Wire);
mySensor.beginAccel();
mySensor.beginMag();
// you can set your own offset for mag values
// mySensor.magXOffset = -50;
// mySensor.magYOffset = -55;
// mySensor.magZOffset = -10;
}
void loop() {
mySensor.accelUpdate();
Serial.println("print accel values");
Serial.println("accelX: " + String(mySensor.accelX()));
Serial.println("accelY: " + String(mySensor.accelY()));
Serial.println("accelZ: " + String(mySensor.accelZ()));
Serial.println("accelSqrt: " + String(mySensor.accelSqrt()));
mySensor.magUpdate();
Serial.println("print mag values");
Serial.println("magX: " + String(mySensor.magX()));
Serial.println("maxY: " + String(mySensor.magY()));
Serial.println("magZ: " + String(mySensor.magZ()));
Serial.println("horizontal direction: " + String(mySensor.magHorizDirection()));
Serial.println("at " + String(millis()) + "ms");
delay(500);
}