I’m using 32GB SD card.
This is my code trying to store these data in the SD card. But I’m not able to initiialize the SD card
#include <Arduino.h>
#include <TFT_eSPI.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include <SD.h>
#define SEALEVELPRESSURE_HPA (1013.25)
#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C3)
#include "esp_wifi.h"
#include "WiFi.h"
const char *ntpServer = "time.cloudflare.com";
const char *ssid = "Adith";
const char *password = "aadithsp";
#endif
#include "I2C_BM8563.h"
#include "NotoSansBold15.h"
I2C_BM8563 rtc(I2C_BM8563_DEFAULT_ADDRESS, Wire);
Adafruit_BME680 bme; // Create a BME680 sensor object for I2C
I2C_BM8563_TimeTypeDef timeStruct;
I2C_BM8563_DateTypeDef dateStruct;
//TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
TFT_eSPI tft = TFT_eSPI();
File myFile;
TFT_eSprite face = TFT_eSprite(&tft);
#define CLOCK_X_POS 10
#define CLOCK_Y_POS 10
#define CLOCK_FG TFT_SKYBLUE
#define CLOCK_BG TFT_NAVY
#define SECCOND_FG TFT_RED
#define LABEL_FG TFT_GOLD
#define CLOCK_R 230.0f / 2.0f // Clock face radius (float type)
#define H_HAND_LENGTH CLOCK_R / 2.0f
#define M_HAND_LENGTH CLOCK_R / 1.4f
#define S_HAND_LENGTH CLOCK_R / 1.3f
#define FACE_W CLOCK_R * 2 + 1
#define FACE_H CLOCK_R * 2 + 1
// Calculate 1 second increment angles. Hours and minute hand angles
// change every second so we see smooth sub-pixel movement
#define SECOND_ANGLE 360.0 / 60.0
#define MINUTE_ANGLE SECOND_ANGLE / 60.0
#define HOUR_ANGLE MINUTE_ANGLE / 12.0
// Sprite width and height
#define FACE_W CLOCK_R * 2 + 1
#define FACE_H CLOCK_R * 2 + 1
// Time h:m:s
uint8_t h = 0, m = 0, s = 0;
float time_secs = h * 3600 + m * 60 + s;
// Time for next tick
uint32_t targetTime = 0;
// =========================================================================
// Setup
// =========================================================================
void setup() {
Serial.begin(115200);
// Initialise the screen
tft.init();
// Ideally set orientation for good viewing angle range because
// the anti-aliasing effectiveness varies with screen viewing angle
// Usually this is when screen ribbon connector is at the bottom
tft.setRotation(0);
tft.fillScreen(TFT_BLACK);
// Create the clock face sprite
//face.setColorDepth(8); // 8 bit will work, but reduces effectiveness of anti-aliasing
face.createSprite(FACE_W, FACE_H);
// Only 1 font used in the sprite, so can remain loaded
face.loadFont(NotoSansBold15);
// Draw the whole clock - NTP time not available yet
renderFace(time_secs);
#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C3)
WiFi.begin(ssid, password);
for (int a = 0; a < 10; a++) {
if (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("\nWaiting for WiFi Connection...");
}
delay(500);
}
delay(1000);
if (WiFi.status() == WL_CONNECTED)
{
Serial.print("\nWiFi Successfully Connected!");
}
else
{
Serial.print("\nWiFi Connection Failed");
}
delay(1000);
configTime(8 * 3600, 0, ntpServer);
struct tm timeInfo;
if (getLocalTime(&timeInfo)) {
timeStruct.hours = timeInfo.tm_hour;
timeStruct.minutes = timeInfo.tm_min;
timeStruct.seconds = timeInfo.tm_sec;
rtc.setTime(&timeStruct);
// dateStruct.weekDay = timeInfo.tm_wday;
// dateStruct.month = timeInfo.tm_mon + 1;
// dateStruct.date = timeInfo.tm_mday;
// dateStruct.year = timeInfo.tm_year + 1900;
// rtc.setDate(&dateStruct);
}
#endif
Wire.begin();
rtc.begin();
syncTime();
//
while (!Serial);
Serial.println(F("\nBME688 test"));
delay(1000);
if (!bme.begin())
{
Serial.println("\nCould not find a valid BME688 sensor, check wiring!");
while (1); // Stay in a loop if the sensor is not found
}
// Configure sensor settings
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320°C for 150 ms
///////////////////////////////////////////////////////////////////
// Open serial communications and wait for port to open:
while(!Serial);
Serial.print("Initializing SD card...");
pinMode(D2, OUTPUT);
if (!SD.begin(D2)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("/test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("/test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
// =========================================================================
// Loop
// =========================================================================
void loop() {
sensor();
// Update time periodically
if (targetTime < millis()) {
// Update next tick time in 100 milliseconds for smooth movement
targetTime = millis() + 100;
// Increment time by 100 milliseconds
time_secs += 0.100;
// Midnight roll-over
if (time_secs >= (60 * 60 * 24)) time_secs = 0;
// All graphics are drawn in sprite to stop flicker
renderFace(time_secs);
// syncTime();
}
}
void sensor()
{
//////////////////////////////////////////
I2C_BM8563_DateTypeDef dateStruct;
I2C_BM8563_TimeTypeDef timeStruct;
// Get RTC
rtc.getDate(&dateStruct);
rtc.getTime(&timeStruct);
// Print RTC
#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C3)
Serial.print("\nYear = ");
Serial.print(dateStruct.year);
Serial.print("\nMonth = ");
Serial.print(dateStruct.month);
Serial.print("\nDate = ");
Serial.print(dateStruct.date);
Serial.print("\nHours = ");
Serial.print(timeStruct.hours);
Serial.print("\nMinutes = ");
Serial.print(timeStruct.minutes);
Serial.print("\nSeconds = ");
Serial.print(timeStruct.seconds);
Serial.println();
#endif
delay(1);
//////////////////////////////////////////
// Check if the BME680 sensor reading was successful
if (!bme.performReading())
{
Serial.println("Failed to perform reading :(");
return;
}
// Display temperature in degrees Celsius
Serial.print("Temperature = ");
Serial.print(bme.temperature);
Serial.println(" °C");
// Display pressure in hPa (hectopascals)
Serial.print("Pressure = ");
Serial.print(bme.pressure / 100.0);
Serial.println(" hPa");
// Display humidity in percentage
Serial.print("Humidity = ");
Serial.print(bme.humidity);
Serial.println(" %");
// Calculate Dew Point
float dewPoint = bme.temperature - ((100 - bme.humidity) / 5);
Serial.print("Dew Point = ");
Serial.print(dewPoint);
Serial.println(" °C");
// Display gas resistance in KOhms
Serial.print("Gas = ");
Serial.print(bme.gas_resistance / 1000.0);
Serial.println(" KOhms");
// Calculate and display approximate altitude
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
// Print a blank line for better readability
Serial.println();
Serial.println();
// Delay for 2 seconds before the next reading
delay(1000);
}
// =========================================================================
// Draw the clock face in the sprite
// =========================================================================
static void renderFace(float t) {
float h_angle = t * HOUR_ANGLE;
float m_angle = t * MINUTE_ANGLE;
float s_angle = t * SECOND_ANGLE;
// The face is completely redrawn - this can be done quickly
face.fillSprite(TFT_BLACK);
// Draw the face circle
face.fillSmoothCircle(CLOCK_R, CLOCK_R, CLOCK_R, CLOCK_BG);
// Set text datum to middle centre and the colour
face.setTextDatum(MC_DATUM);
// The background colour will be read during the character rendering
face.setTextColor(CLOCK_FG, CLOCK_BG);
// Text offset adjustment
constexpr uint32_t dialOffset = CLOCK_R - 10;
float xp = 0.0, yp = 0.0; // Use float pixel position for smooth AA motion
// Draw digits around clock perimeter
for (uint32_t h = 1; h <= 12; h++) {
getCoord(CLOCK_R, CLOCK_R, &xp, &yp, dialOffset, h * 360.0 / 12);
face.drawNumber(h, xp, 2 + yp);
}
// Add text (could be digital time...)
face.setTextColor(LABEL_FG, CLOCK_BG);
face.drawString("DePaul University", CLOCK_R, CLOCK_R * 0.75);
// Draw minute hand
getCoord(CLOCK_R, CLOCK_R, &xp, &yp, M_HAND_LENGTH, m_angle);
face.drawWideLine(CLOCK_R, CLOCK_R, xp, yp, 6.0f, CLOCK_FG);
face.drawWideLine(CLOCK_R, CLOCK_R, xp, yp, 2.0f, CLOCK_BG);
// Draw hour hand
getCoord(CLOCK_R, CLOCK_R, &xp, &yp, H_HAND_LENGTH, h_angle);
face.drawWideLine(CLOCK_R, CLOCK_R, xp, yp, 6.0f, CLOCK_FG);
face.drawWideLine(CLOCK_R, CLOCK_R, xp, yp, 2.0f, CLOCK_BG);
// Draw the central pivot circle
face.fillSmoothCircle(CLOCK_R, CLOCK_R, 4, CLOCK_FG);
// Draw cecond hand
getCoord(CLOCK_R, CLOCK_R, &xp, &yp, S_HAND_LENGTH, s_angle);
face.drawWedgeLine(CLOCK_R, CLOCK_R, xp, yp, 2.5, 1.0, SECCOND_FG);
face.pushSprite(5, 5, TFT_TRANSPARENT);
}
// =========================================================================
// Get coordinates of end of a line, pivot at x,y, length r, angle a
// =========================================================================
// Coordinates are returned to caller via the xp and yp pointers
#define DEG2RAD 0.0174532925
void getCoord(int16_t x, int16_t y, float *xp, float *yp, int16_t r, float a) {
float sx1 = cos((a - 90) * DEG2RAD);
float sy1 = sin((a - 90) * DEG2RAD);
*xp = sx1 * r + x;
*yp = sy1 * r + y;
}
void syncTime(void) {
targetTime = millis() + 100;
rtc.getTime(&timeStruct);
time_secs = timeStruct.hours * 3600 + timeStruct.minutes * 60 + timeStruct.seconds;
}
NotoSansBold15.h