I'm relatively new to arduino, but am working with a microSD card and an accelerometer. When I test my sensors individually, they work fine, but when I combine them, the code seems to stop detecting my accelerometer. Here's my code, and for reference it stops during the Adafruit sensor test section of setup.
#include <SPI.h>
#include <SD.h>
#include <Adafruit_ICM20649.h>
#include <Adafruit_Sensor.h>
Adafruit_ICM20649 icm;
const int chipSelect = 5;
uint16_t measurement_delay_us = 65535;
#define ICM_CS 6
#define ICM_SCK 7
#define ICM_MISO 8
#define ICM_MOSI 9
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
Serial.println("Adafruit ICM20649 test!");
// 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 ICM20649 chip");
while (1) {
delay(10);
}
}
}
Serial.println("ICM20649 Found!");
// icm.setAccelRange(ICM20649_ACCEL_RANGE_4_G);
Serial.print("Accelerometer range set to: ");
switch (icm.getAccelRange()) {
case ICM20649_ACCEL_RANGE_4_G:
Serial.println("+-4G");
break;
case ICM20649_ACCEL_RANGE_8_G:
Serial.println("+-8G");
break;
case ICM20649_ACCEL_RANGE_16_G:
Serial.println("+-16G");
break;
case ICM20649_ACCEL_RANGE_30_G:
Serial.println("+-30G");
break;
}
// icm.setGyroRange(ICM20649_GYRO_RANGE_500_DPS);
Serial.print("Gyro range set to: ");
switch (icm.getGyroRange()) {
case ICM20649_GYRO_RANGE_500_DPS:
Serial.println("500 degrees/s");
break;
case ICM20649_GYRO_RANGE_1000_DPS:
Serial.println("1000 degrees/s");
break;
case ICM20649_GYRO_RANGE_2000_DPS:
Serial.println("2000 degrees/s");
break;
case ICM20649_GYRO_RANGE_4000_DPS:
Serial.println("4000 degrees/s");
break;
}
// icm.setAccelRateDivisor(4095);
uint16_t accel_divisor = icm.getAccelRateDivisor();
float accel_rate = 1125 / (1.0 + accel_divisor);
Serial.print("Accelerometer data rate divisor set to: ");
Serial.println(accel_divisor);
Serial.print("Accelerometer data rate (Hz) is approximately: ");
Serial.println(accel_rate);
// icm.setGyroRateDivisor(255);
uint8_t gyro_divisor = icm.getGyroRateDivisor();
float gyro_rate = 1100 / (1.0 + gyro_divisor);
Serial.print("Gyro data rate divisor set to: ");
Serial.println(gyro_divisor);
Serial.print("Gyro data rate (Hz) is approximately: ");
Serial.println(gyro_rate);
Serial.println();
}
void loop() {
// make a string for assembling the data to log:
sensors_event_t accel;
sensors_event_t gyro;
sensors_event_t temp;
icm.getEvent(&accel, &gyro, &temp);
Serial.print("\t\tTemperature ");
Serial.print(temp.temperature);
Serial.println(" deg C");
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("\t\tAccel X: ");
Serial.print(accel.acceleration.x);
Serial.print(" \tY: ");
Serial.print(accel.acceleration.y);
Serial.print(" \tZ: ");
Serial.print(accel.acceleration.z);
Serial.println(" m/s^2 ");
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("\t\tGyro X: ");
Serial.print(gyro.gyro.x);
Serial.print(" \tY: ");
Serial.print(gyro.gyro.y);
Serial.print(" \tZ: ");
Serial.print(gyro.gyro.z);
Serial.println(" radians/s ");
Serial.println();
delay(100);
String dataString = "";
String temperature = "";
String gyroscopeX = "";
String gyroscopeY = "";
String gyroscopeZ = "";
String accelerometerX = "";
String accelerometerY = "";
String accelerometerZ = "";
temperature += "\t\tTemperature ", temp.temperature," deg C";
gyroscopeX += "\t\tGyro X: ", gyro.gyro.x;
gyroscopeY += " \tGyro Y: ", gyro.gyro.y;
gyroscopeZ += " \tGyro Z: ", gyro.gyro.z;
accelerometerX += "\t\tAccel X: ", accel.acceleration.x;
accelerometerY += " \tAccel Y: ", accel.acceleration.y;
accelerometerZ += " \tAccel Z: ", accel.acceleration.z;
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(temperature);
dataFile.println(gyroscopeX);
dataFile.println(gyroscopeY);
dataFile.println(gyroscopeZ);
dataFile.println(accelerometerX);
dataFile.println(accelerometerY);
dataFile.println(accelerometerZ);
dataFile.close();
// print to the serial port too:
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
6 posts - 3 participants