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");
}
}