Saving data from Sensor onto SD card - Teensy 3.5

Hi,

I am looking to save data from the AHT20 onto an SD card with the teensy 3.5. However after days of trying to combine separate pieces of code, and being unsuccessful, I think it is a good opportunity to use Arduino Forum.

Both pieces of code seem to work well individually.

This is my code for AHT20 humidity and temperature sensor:

#include <Adafruit_AHTX0.h>

Adafruit_AHTX0 AHT20;

void setup()

{

Serial.begin(115200); // serial communication speed
Serial.println("Adafruit AHT20"); // telling programme what sensor in use

if (! AHT20.begin()) {
Serial.println("AHT20 Not Found");
while (1) delay(10);
}
Serial.println("AHT20 Found");

}

void loop()

{

sensors_event_t humidity, temperature; // allows for sensor to give units

AHT20.getEvent(&humidity, &temperature); // takes data for humidity and temperature

Serial.print("Temperature: "); Serial.print(temperature.temperature); Serial.println(" Degrees Celsius");
Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% Humidity");

delay(1000); //every 1000ms data is recorded

}

This is my code for saving to the SD card:

#include <SD.h>
#include <SPI.h>
#include <Adafruit_AHTX0.h>

Adafruit_AHTX0 AHT20 = Adafruit_AHTX0();
const int chipSelect = BUILTIN_SDCARD;

void setup()
{

Serial.begin(115200);
while (!Serial) {

}
Serial.print("Initializing SD card...");

if (!SD.begin(chipSelect)) {
Serial.println("Card not present");
while (1) {
}
}
Serial.println("card initialized.");
}
}

void loop() {

String dataString = "AHT20";

// read 7 sensors and append to the string:

for (int analogPin = 0; analogPin < 3; analogPin++) {

int AHT20 = analogRead(analogPin);

dataString += String(AHT20);

if (analogPin < 2) {

dataString += ",";

}

}

File dataFile = SD.open("Data.txt", FILE_WRITE);

if (dataFile) {

dataFile.println(dataString);

dataFile.close();

Serial.println(dataString);

}
else {

Serial.println("error opening Data.txt");
}
}

Please edit your post to add code tags.

after days of trying

Post your best attempt, using code tags.

Forum members are happy to help you figure out particular problems with code, but if you want someone to write code for you, post on the Gigs and Collaborations forum section. You may be asked to pay for the help.

{UPDATE} My issue was two devices on the SPI bus so it doesn't to you. However you might keep it in mind if you use two devices on the SPI bus.

I ran into a similar problem when using an SD card with another device on the SPI bus. It seems that all SD breakout boards I could find (even the Adafruit which I expected more from) do not tristate the output pin when the board in inhibited.

I recommend you disconnect the AHT20 and just get the SD working. Then disconnect the SD and get the AHT20 working. From there you can see if you have the same issue as I do.

Here is a photo of what I had to do to my board:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.