new_thea/Embedded/ICM_mag_reader/ICM_mag_reader.ino
2021-09-21 12:11:46 +01:00

53 lines
1.3 KiB
C++

#include <Adafruit_ICM20X.h>
#include <Adafruit_ICM20948.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_ICM20948 icm;
uint16_t measurement_delay_us = 65535; // Delay between measurements for testing
// // For SPI mode, we need a CS pin
// #define ICM_CS 10
// // For software-SPI mode we need SCK/MOSI/MISO pins
// #define ICM_SCK 13
// #define ICM_MISO 12
// #define ICM_MOSI 11
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
// Try to initialize!
if (!icm.begin_I2C()) {
// if (!icm.begin_SPI(ICM_CS)) {
// if (!icm.begin_SPI(ICM_CS, ICM_SCK, ICM_MISO, ICM_MOSI)) {
Serial.println("Failed to find ICM20948 chip");
while (1) {
delay(10);
}
}
}
void loop() {
// /* Get a new normalized sensor event */
sensors_event_t accel;
sensors_event_t gyro;
sensors_event_t mag;
sensors_event_t temp;
icm.getEvent(&accel, &gyro, &temp, &mag);
double mag_x = mag.magnetic.x;
double mag_y = mag.magnetic.y;
double mag_z = mag.magnetic.z;
if(mag_z > -2000 && mag_z < 2000) {
Serial.print(mag.magnetic.x);
Serial.print(";");
Serial.print(mag.magnetic.y);
Serial.print(";");
Serial.println(mag.magnetic.z);
}
delay(11);
}