Arduino Nano (1.8 TFT Display and SD Card logger shield) Code Conflict

hello and good day everyone ,

I am currently working on a project involving an Arduino Nano, a TFT display, and an SD card logger. Individually, both the SD card and TFT display codes function without issues. However, when I attempt to merge these codes, the TFT display begins to blink continuously and fails to show any readings.

From my understanding, this could be a conflict in the SPI communication between the two devices (pin 11, 13). I would greatly appreciate any guidance or resources you could provide to resolve this issue. If there are any code adjustments that could facilitate the simultaneous operation of both the TFT display and the SD card logger on the Arduino Nano, it would be immensely helpful.

Thank you for your time and assistance.

Physical connections

Data Logging Module - RTC and Micro SD Card

SD card attached to SPI bus as follows:

/**************************************************************************
 Arduino interface with ST7735 color TFT (128x160 pixel) display and
 DHT11 digital humidity and temperature sensor.
  **************************************************************************/
#include <Adafruit_GFX.h>     // include Adafruit graphics library
#include <Adafruit_ST7735.h>  // include Adafruit ST7735 TFT library
#include <DHT.h>              // include DHT library
#include <SD.h>
#include <RTClib.h>  // Real Time Clock Library

/**********************TFT 1.8 Dislay*********************************/
#define TFT_RST 9  // TFT RST pin is connected to arduino pin 9
#define TFT_CS 10  // TFT CS  pin is connected to arduino pin 10
#define TFT_DC 8   // TFT DC  pin is connected to arduino pin 8
// nano SCL->13, SDA->11
// initialize ST7735 TFT library
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

/**********************DHT Sensor*************************************/
#define DHTPIN 2             // DHT11 data pin is connected to Arduino digital pin 2
#define DHTTYPE DHT11        // DHT11 sensor is used
DHT dht11(DHTPIN, DHTTYPE);  // initialize DHT library

/********************* SD Card & RTC ********************************/
const int chipSelect = 4;  // Sd card pin selection
File myFile;
char filename[] = "yyyymmdd.txt";  // filename (without extension) should not exceed 8 chars
/*
SD card attached to SPI bus  
CS connected to pin 4 for Nano
*/
RTC_DS1307 rtc;  //call library for clock

/******************* Update Frequently *****************************/
const unsigned long eventInterval = 1000;  //Time interval
unsigned long previousTime = 0;


void setup(void) {
  dht11.begin();                  // initialize DHT11 sensor
  pinMode(4, OUTPUT);            // make sure that the default SD chip select pin is set to output, even if you don't use it:

  tft.initR(INITR_BLACKTAB);     // initialize a ST7735S chip, black tab
  tft.fillScreen(ST7735_BLACK);  // fill screen with black color
  tft.setTextColor(ST7735_GREEN, ST7735_BLACK);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    tft.setCursor(30, 65);
    tft.print("Card failed");
    tft.setCursor(15, 85);
    tft.print(" or not present");

    while (1)
      ;  // don't do anything more:
  }
  tft.setCursor(10, 65);
  tft.print("card initialized...");
  delay(2000);

  tft.fillScreen(ST7735_BLACK);                        // fill screen with black color
  tft.drawRoundRect(0, 0, 128, 130, 5, ST77XX_BLUE);   //Draw round rectangular (C,R,W,H,ROUND, COLOR )
  tft.drawFastHLine(0, 45, tft.width(), ST7735_BLUE);  // draw horizontal blue line at position (0, 45)
  tft.drawFastHLine(0, 88, tft.width(), ST7735_BLUE);  // draw horizontal blue line at position (0, 88)

  tft.setTextColor(ST7735_YELLOW, ST7735_BLACK);  // set text color to yellw and black background
  tft.setTextSize(1);                             // text size = 1
  tft.setCursor(16, 7);                           // move cursor to position (16, 7) pixel
  tft.print("SD Data Logger");

  tft.setTextColor(ST7735_GREEN, ST7735_BLACK);  // set text color to green and black background
  tft.setCursor(32, 53);                         // move cursor to position (32, 53) pixel
  tft.print("TEMPERATURE");

  tft.setTextColor(ST7735_GREEN, ST7735_BLACK);  // set text color to orange and black background
  tft.setCursor(42, 96);                         // move cursor to position (42, 96) pixel
  tft.print("HUMIDITY");
  tft.setTextSize(2);  // text size = 2

  tft.drawRoundRect(0, 132, 128, 28, 5, ST77XX_BLUE);  //Draw round rectangular (C,R,W,H,ROUND, COLOR )
  tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);        // set text color to green and black background
  tft.setCursor(4, 140);                               // move cursor to position (COLUMN 4, RAW 140) pixel
  tft.print("PKEEE, USM");
}

// main loop
void loop() {

  /*******Reading temperature or humidity takes about 250 milliseconds*****/
  // read humidity
  byte humi = dht11.readHumidity();
  // read temperature
  byte temp = dht11.readTemperature();
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht11.computeHeatIndex(temp, humi, false);

  /**************************Data Logging ************************************/
  unsigned long currentTime = millis();
  if (currentTime - previousTime >= eventInterval) {
    logData(humi, temp, hic);
    previousTime = currentTime;
  }

  // print temperature (in °C)
  tft.setTextColor(ST7735_RED, ST7735_BLACK);  // set text color to red and black background
  tft.setCursor(42, 68);
  tft.print(temp);
  tft.drawCircle(77, 70, 2, ST7735_RED);  // print degree symbol ( ° )
  tft.setCursor(83, 68);
  tft.print("C");

  // print humidity (in %)
  tft.setTextColor(ST7735_CYAN, ST7735_BLACK);  // set text color to cyan and black background
  tft.setCursor(44, 110);
  tft.print(humi);
  tft.setCursor(79, 110);
  tft.print("%");
  delay(1000);
}
/**************************Data Logging ************************************/
void logData(byte humi, byte temp, float hic) {

  DateTime now = rtc.now();
  int year = now.year();
  int month = now.month();
  int day = now.day();

  // update filename
  filename[0] = (year / 1000) + '0';
  filename[1] = ((year % 1000) / 100) + '0';
  filename[2] = ((year % 100) / 10) + '0';
  filename[3] = (year % 10) + '0';

  filename[4] = (month / 10) + '0';
  filename[5] = (month % 10) + '0';

  filename[6] = (day / 10) + '0';
  filename[7] = (day % 10) + '0';

  // 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(filename, FILE_WRITE);
  if (myFile) {  // if the file is available, write to it:
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(",");
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);

    myFile.print(",");  // delimiter between timestamp and data

    myFile.print("Humidity(%):");
    myFile.print(",");
    myFile.print(humi);
    myFile.print(",");
    myFile.print("Temperature(°C):");
    myFile.print(",");
    myFile.print(temp);
    myFile.print(",");
    myFile.print("Heat index(°C):");
    myFile.print(",");
    myFile.println(hic);
    myFile.close();
  }
}
// end of code.

there are many reports of other devices not working on the same SPI bus with common SD card readers, see Sharing the SPI bus among SD card and other SPI devices
consider using a microcontroller with multiple SPI interfaces, see connecting-sd-card-and-bmp280-via-spi-interface

It's puzzling because what we typically see is two SPI devices with conflicts on the MISO line. But you only have one SPI device that I can see - the SD card. Did I miss one?

And yet the logging module and the display both work when the other isn't present. How are you powering the Nano? From the video it looks like you get power through the Nano's USB port. But is that connected to a computer or power brick or what? I'm just wondering if it's a problem with having enough power for both devices. SD cards can draw a good bit of current when writing. Is there any voltage sag on the 5V line when this problem is happening?

Well, it's a mystery. I don't know of a reason why the Nano couldn't handle all this, but I don't know what your libraries might be doing that could interfere with each other.

Edit: Oh. I wasn't paying attention. In your connections list, you have the display connected to 11 and 13, but you show that as SDA and SCL. SDA and SCL are the I2C pins. So something is wrong. Can we back up and find out what devices you are using and whether they are I2C or SPI? Including the DHT device referred to in your code. It may be an SPI conflict after all.

Further Googling shows that the display is indeed an SPI device, and it appears to be connected correctly. So we need to look at the SD card module. In the picture below, the MISO line is pointed to by the red arrow. With no SD card in the holder, can you use your meter to see if that pin is directly connected to the "MISO" header of the module? If not, it may go to a pin on the 74HC125. If so, which one?
DOpin

You don't seem to be using Serial Monitor to help solve the problem.

I suggest you add

Serial.begin(115200); the 
Serial.println("Entered setup.");

At the start of setup(), and

Serial.println("Exiting setup.");

at the end of setup().

Similar for loop(), but don't repeat Serial.begin() there.

What do you see in serial monitor?

My reason for asking is that it appears to me from watching your video, that the Arduino is resetting at some point during loop(). If you see "Entering loop" and then "Entering setup" that would confirm that the Arduino is resetting.

A common reason for unexpected resetting is problems related to the power supply.

I found this

Despite @JohnRob 's concern, there should be no problem with this board sharing the SPI bus with other devices. The MISO pin does not connect to the 74hc125, it connects directly to the header pin.

You are 100% correct. My issue was my system included a M31855k board which is also SPI.
Because the Deek-Robot sends the MISO directly to the connector, it is not tri-stated. The MISO being not tri-stated conflicts with the M31855k MISO.

No. If MISO is connected directly to the SD card, the SD card will tristate the line unless its CS pin is asserted. So MISO will behave properly this way. The direct connection is the same on the Adafruit SD card module, and it plays well with others.

So I think the problem must be something else.

1 Like

Ah yes, your statement is correct. Your post jogged my memory.

A while ago I was trying to use this board:

image

For reasons that still escape me they passed the MISO through U1 grounding the CS for that channel. This is not a problem until there is a 2nd SPI in the system.

The board is powered with 5V and supposedly the design was to allow an SD card to work in a 5V system.

Yes, it seems such a simple slip, I wonder why a V2 is this board wasn't developed to fix it.


That's the problem. If that enable pin was connected to the CS pin instead of ground, I wonder if that would fix it. But it would not have translated the 3.3V MISO signal to 5V because the 74VHC125 is powered by 3.3V, so perhaps no better than simply connecting the MISO directly to the MISO pin, bypassing the 74VHC125.

But we are getting off topic now, this problem is not relevant to @kamranrauf87 's SD card/RTC module. There is some other problem we haven't discovered yet.

I have no verification, however I think these Chinese mfg make/get/copy a design and make a very large number of boards. Perhaps if they came out with V2 they would never be able to sell any more V1s. And for most people the boards work fine.

Well, just to finish this, below is the fix for the other SD module. I don't think you would make anything better by running MISO through the 125 and changing its /OE pin to /CS. You would just be translating 3.3V to 3.3V. Anyway, the direct connection is what Adafruit uses, and that's good enough for me.

But I don't know what the OP's problem is. He has two SPI devices driven by two different libraries. Also, isn't there an SD card holder on the back of the display? If so, I wonder why he doesn't use that. Maybe the display library would handle that SD too.

Possibly not, they don't all have them. Often those that do have an extra header for the SD card on the opposite end of the board. I don't know why this is done, when several of the pins (MOSI, MISO, CLK) could be commoned between the LCD, SD card and touch controller/sensor chip in some models

Amazing how a thread can go so far down a single path.

Back to the OP's connections. I'm not an expert at I2C but shouldn't ALL the SCL and ALL the SDA's be on the same pins

Yes, but the headers of the display module are wrongly named. They are actually SPI lines, so I think they are correctly connected. Plus, the OP said both the display and the SD card work fine when only one is connected.

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