Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);
Commenting out this line and the code works fine. But adding this line even without the rest of the code for gathering the mag data.
The sensor examples and the SD examples provided by the libraries work just fine. So the hardware is working. The whole code is below.
Could the sensor address be conflicting with the SD card? I know the sensor is I2C and the SD card is SPI. So I don't believe that the address could be the problem, but I can't think of anything else.
Code: Select all
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <Adafruit_L3GD20_U.h>
#include <Adafruit_LSM303_U.h>
#include <SD.h>
#include "RTClib.h"
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10081);
Adafruit_L3GD20_Unified gyro = Adafruit_L3GD20_Unified(20);
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
//Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);
RTC_DS1307 rtc;
File dataFile;
const int chipSelect = 10;
int count = 0;
void setup(void)
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(SS, OUTPUT);
// 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.");
// Initialize the RTC
rtc.begin();
char filename[] = "WIND300.TXT";
for (uint8_t i = 0; i < 100; i++)
{
filename[5] = i/10 + '0';
filename[6] = i%10 + '0';
if (! SD.exists(filename))
{
// only open a new file if it doesn't exist
dataFile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
//Serial.println("Pressure Sensor Test");
//Serial.println("");
/* Initialise the sensor */
if(!bmp.begin())
{
/* There was a problem detecting the BMP085 ... check your connections */
Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
while(1);
}
if(!gyro.begin())
{
/* There was a problem detecting the L3GD20 ... check your connections */
Serial.print("Ooops, no L3GD20 detected ... Check your wiring or I2C ADDR!");
while(1);
}
if(!accel.begin())
{
// There was a problem detecting the ADXL345 ... check your connections
Serial.println(F("Ooops, no LSM303 detected ... Check your wiring!"));
while(1);
}
}
void loop(void)
{
/* Get a new sensor event */
sensors_event_t bmp_event;
bmp.getEvent(&bmp_event);
sensors_event_t gyro_event;
gyro.getEvent(&gyro_event);
sensors_event_t accel_event;
accel.getEvent(&accel_event);
DateTime now = rtc.now();
String dataString = "";
dataFile.print(++count);
dataFile.print(",");
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
dataFile.print(' ');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.print(",");
dataFile.print("PRESS");
dataFile.print(",");
/* Display the results (barometric pressure is measure in hPa) */
if (bmp_event.pressure)
{
/* Display atmospheric pressure in hPa */
dataFile.print(bmp_event.pressure);
dataFile.print(",");
// dataString += string(event.pressure);
Serial.print(bmp_event.pressure);
// Serial.println();
}
else
{
Serial.println("BMP 180 Sensor error");
dataFile.print(",");
}
// Accelerometer data
dataFile.print("ACCEL (X,Y,Z)");
dataFile.print(",");
//Display the results of the accelormeter
if (accel_event.acceleration.x)
{
dataFile.print(accel_event.acceleration.x);
dataFile.print(",");
dataFile.print(accel_event.acceleration.y);
dataFile.print(",");
dataFile.print(accel_event.acceleration.z);
dataFile.print(",");
Serial.print(" ");
Serial.print(accel_event.acceleration.x);
}
else
{
dataFile.print("LSM303 Accel sensor error");
dataFile.print(",");
}
/* Gyroscopic data */
dataFile.print("GYRO (X,Y,Z)");
dataFile.print(",");
if (gyro_event.gyro.x)
{
dataFile.print(gyro_event.gyro.x);
dataFile.print(",");
dataFile.print(gyro_event.gyro.y);
dataFile.print(",");
dataFile.print(gyro_event.gyro.z);
dataFile.print(",");
Serial.print(" ");
Serial.print(gyro_event.gyro.x);
}
else
{
dataFile.print("L3GD20 Mag sensor error");
dataFile.print(",");
}
Serial.println();
dataFile.println();
dataFile.flush();
delay(1);
}