Hi!
We want to log the temperature at 8 spots. Further, we want to save the data to the SD card and also show it on the screen continuously. When we only include one of the above in the code, it works. Meaning it writes to the SD card when the writing to screen is excluded and it writes to the screen when the SD-writing is excluded. When we include both, the code stops at the SD.begin. We have tried both SDFat and SD libraries as the code shows.
We us these type of components:
Clock: ds3231 at24c32 iic RTC
Screen: 2.8 tft spi 240x320 v1.2
Amplifier: MAX31850K
Level-shifter: BSS138
Green R = 1200 ohm
Yellow R = 1800 ohm
Blue R = 56 ohm
Connections:
Arduino - SD card reader:
D4 - SD_CS;
D11 - SD_MOSI;
D12: SD_MISO;
D13 - SD_CLK.
Arduino - Screen:
3V3 - VCC
GND - GND
D10 → green R → CS
→ yellow R → GND
D8 → green R → RST
→ yellow R → GND
D13 → green R → CLK
→ yellow R→ GND
D3 → greenR → MOSI
→ yellow R → GND
D9 → green R → DC
→ yellow R → GND
5V → blue R → LED
D5 → MISO
Clock:
A5 → SCL
A4 → SDA
3v3 → VCC
GND → GN
Code:
//Libraries included
//-----------------------------------------------------------------------------------------------------
#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#include <Sodaq_DS3231.h>
#include <Wire.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h>
//#include <SdFat.h>
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 5
#define TFT_MOSI 3
#define TFT_CLK 13
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
//Definitions
/---------------------------------------------------------------------------------------------------------/
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
float maxTemperature=0;
float minTemperature=200;
char charMinTemperature[10];
char charMaxTemperature[10];
char timeChar[100];
char dateChar[50];
char temperatureChar[10];
float temperature = 0;
float previousTemperature = 0;
uint32_t old_ts;
String dateString;
int minuteNow=0;
int minutePrevious=0;
const int chipSelect = 4;
//SdFat sd;
//SdFile myFile;
File myFile;
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer; // arrays to hold device addresses
/---------------------------------------------------------------------------------------------------------/
void setup(){
//Setup
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
Serial.begin(9600);
tft.begin();
tft.setRotation(0);
tft.fillScreen(ILI9341_BLACK);
Wire.begin();
rtc.begin();
sensors.begin();
//HEADLINES
//printText(char *text, uint16_t color, int x, int y,int textSize)
printText("Sensor:", ILI9341_PINK, 1, 75, 5/2);
printText("Value:", ILI9341_YELLOW, tft.width()/2, 75, 5/2);
//Sensors
printText("TEMP 1", ILI9341_WHITE, 1, 100, 2);
printText("TEMP 2", ILI9341_WHITE, 1, 125, 2);
printText("TEMP 3", ILI9341_WHITE, 1, 150, 2);
printText("TEMP 4", ILI9341_WHITE, 1, 175, 2);
printText("TEMP 5", ILI9341_WHITE, 1, 200, 2);
printText("TEMP 6", ILI9341_WHITE, 1, 225, 2);
printText("VOLT 1", ILI9341_WHITE, 1, 250, 2);
printText("VOLT 2", ILI9341_WHITE, 1, 275, 2);
//clock
//setRTCTime();
// Find Thermometer adresses
if (!sensors.getAddress(insideThermometer, 0))
if (!sensors.getAddress(insideThermometer, 1))
if (!sensors.getAddress(insideThermometer, 2))
if (!sensors.getAddress(insideThermometer, 3))
if (!sensors.getAddress(insideThermometer, 4))
if (!sensors.getAddress(insideThermometer, 5))
if (!sensors.getAddress(insideThermometer, 6))
if (!sensors.getAddress(insideThermometer, 7))
// set the resolution to 9 bit
sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
}
void loop()
{
sensors.requestTemperatures();
DateTime now = rtc.now(); //get the current date-time
uint32_t ts = now.getEpoch();
if (old_ts == 0 || old_ts != ts) {
old_ts = ts;
minuteNow = now.minute();
if (minuteNow!=minutePrevious){
// dateString = getDayOfWeek(now.dayOfWeek())+", ";
dateString = String(now.date())+"/"+String(now.month());
dateString= dateString+"/"+ String(now.year());
minutePrevious = minuteNow;
String hours = String(now.hour());
if(now.minute()<10)
{
hours = hours+":0"+String(now.minute());
}else
{
hours = hours+":"+String(now.minute());
}
hours.toCharArray(timeChar,100);
tft.fillRect(00,00,tft.width(),70,ILI9341_BLACK);
printText(timeChar, ILI9341_RED,90,25,2);
dateString.toCharArray(dateChar,50);
printText(dateChar, ILI9341_GREEN,65,1,2);
}
}
//Initializing SD card:
//SD.begin(4);
// myFile.println("testing 1, 2, 3.");
// myFile.close();
//SD.begin(4);
myFile= SD.open("TEST.txt", FILE_WRITE);
for (uint8_t i = 0; i < 8; i++)
myFile.print(sensors.getTempCByIndex(i));
myFile.close();
tft.fillRect(tft.width()/2,100,90,90,ILI9341_BLACK);
tft.setCursor(tft.width()/2,100);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.print(sensors.getTempCByIndex(0));
tft.setCursor(tft.width()/2+70,100);
tft.print("C");
tft.fillRect(tft.width()/2,125,90,90,ILI9341_BLACK);
tft.setCursor(tft.width()/2,125);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.print(sensors.getTempCByIndex(1));
tft.setCursor(tft.width()/2+70,125);
tft.print("C");
tft.fillRect(tft.width()/2,150,90,90,ILI9341_BLACK);
tft.setCursor(tft.width()/2,150);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.print(sensors.getTempCByIndex(2));
tft.setCursor(tft.width()/2+70,150);
tft.print("C");
}
void printText(char *text, uint16_t color, int x, int y,int textSize)
{
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextSize(textSize);
tft.setTextWrap(true);
tft.print(text);
}
void setRTCTime()
{
DateTime dt(2019, 2, 20, 23, 59, 0, 3); // Year, Month, Day, Hour, Minutes, Seconds, Day of Week
rtc.setDateTime(dt); //Adjust date-time as defined 'dt' above
}