ArduinoGeneral

How to write SHT20 I2C sensor with waterproof probe humidity and temperature values to sd card

userHead Account cancelled 2019-08-24 12:20:08 2488 Views1 Replies
Dear friends, I have connected an SHT20 and can see the values in the serial monitor of Uno R3 with builtin wifi Rev2. I have also connected a sd card, How can I write the sensor values to the sd card preferably as a csv. thanks
2019-08-26 14:36:00 Hey ,
Great question. You can actually check out another users blog post who writes temperature data to their attached SD card. You can find that post here: https://www.dfrobot.com/blog-900.html
Some key things to note is the need to use the Arduino SD library, which can be imported at the top of your file by simply writing #include <SD.h> . You then to initialize the card in the setup area, and then in your main loop you can begin writing to whatever file you choose. The basic outline of your code will look like:

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

//This chipSelect value is determined by your wiring setup (essentially where you put the chipSelect pin output to)
const int chipSelect = 4;

void setup() {

Serial.begin(9600);

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:
return;
}
Serial.println("card initialized.");
}

void loop() {
// make a string for assembling the data to log:
String dataString = "";

//This is what data you want to write to your SD card
dataString += (String)yourData

// 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("data.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening data.txt");
}
}
userHeadPic robert