Go Down

Topic: SD and 16x2 LCD do not work together. (Read 2 times) previous topic - next topic


Nov 07, 2012, 08:10 pm Last Edit: Nov 07, 2012, 08:16 pm by ablarry91 Reason: 1
ElCaron, I think I'm experiencing the same problem as you, though I'm not sure what you mean by two different SPI interfaces.  Could you elaborate a bit, perhaps posting your end-result pinout?

I'm using an ethernet shield, 20x4 LCD, and Arduino Mega.  The LCD utilizes pins (7,8,9,10,11,12) and the SDCS at pin 4.  I can run each component separately, though when combined together (specifically when I add the line 'lcd.setCursor(0,0)'), I fail to write anything to the SD.


Nov 08, 2012, 01:55 am Last Edit: Nov 08, 2012, 03:03 pm by NickPyner Reason: 1
ElCaron, I think this is just a matter of clashing pins. The lines of code that point to this are

// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

Where you don't assign pin 13, despite the comment.

I have had to do much the same thing. My SD is built in and selects with pin 4. I thus had no choice but re-wire the lcd pin 4 to A2 via an intermediate shield. You may have the same problem.  Similarly, there could be problems with pin 10, which I understand the SD requires to be high, or it cannot write. Fortunately, my LCD is not interested in that.

Here is some code, the relevant bits of which may be of interest. BTW note that Arduino does not datestamp the CSVs, and you may need to go on to incorporate a clock!

Code: [Select]

Reading two DS18B20s pins 14&15
display to monitor
display to LCD with pin D04 moved to pin 16 (A2)
Code uses Arduino LCD, sheepdog temp reader, SD stuff from Mellis & Igoe

#define tture1 14//no ; here
#define tture2 15//no ; here

// include the library codes:
#include <LiquidCrystal.h>
#include <SD.h>

File myFile;

// initialize the library with the numbers of MY interface pins
LiquidCrystal lcd(8,9,16,5,6,7);

int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;

void setup() {
   //For each tture sensor: Do a pinMode and a digitalWrite
   pinMode(tture1, INPUT);
   pinMode(tture2, INPUT);

   digitalWrite(tture1, LOW);//Disable internal pull-up.
   digitalWrite(tture2, LOW);

   delay(300);//Wait for newly restarted system to stabilize
   Serial.print("Temperature measurement, two sensors:\n\n");

 // set up the LCD's number of columns and rows:
 lcd.begin(16, 2);

 Serial.print("Initializing SD card...");

  pinMode(10, OUTPUT);
 if (!SD.begin(4)) {
   Serial.println("initialization failed!");
 Serial.println("initialization done.");
 // Print a message to the LCD.
 lcd.print("SD card OK");    
 lcd.print("temp   in   out");      
 // 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);//+++++++++++++++++++ OPEN
 // if the file opened okay, write to it:
 if (myFile) {
   Serial.print("Writing to test.txt...");
   myFile.println("testing! Did you forget the date?");
   myFile.close();//++++++++++++++++++++++++++++++++++++++++++++ CLOSE

 } else {
   // if the file didn't open, print an error:
   Serial.println("error opening test.txt");

void loop(){

// set the LCD cursor to column 4, line 1
 // (note: line 1 is the second row, since counting begins with 0):
 lcd.setCursor(4, 1);
 readTture(tture1);//N.B.: Values passed back in globals
 printTture();//N.B.: Takes values from globals. Also...
    //no newline part of printTture;

 delay(120);// Delay... must not be too short.
 // set LCD cursor for second temp
 lcd.setCursor(11, 1);
 readTture(tture2);//Now read and report 2nd tture.
 delay(200);// Delay... must not be too short.
 Serial.print("\n");//Start new line
      myFile = SD.open("test.txt", FILE_WRITE);//+++++++++++++++++++++++++OPEN

//Everything below here... just copy it into your program "as is".
//You are only likely to need to use readTture(pin) and printTture()
//   directly. Others are subordinate to those.
//These routine access the following global variables...
//   int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;

void OneWireReset(int Pin) // reset.  Should improve to act as a presence pulse
    digitalWrite(Pin, LOW);
    pinMode(Pin, OUTPUT); // bring low for 500 us
    pinMode(Pin, INPUT);
//end OneWireReset

void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
  byte n;

  for(n=8; n!=0; n--)
     if ((d & 0x01) == 1)  // test least sig bit
        digitalWrite(Pin, LOW);
        pinMode(Pin, OUTPUT);
        pinMode(Pin, INPUT);
        digitalWrite(Pin, LOW);
        pinMode(Pin, OUTPUT);
        pinMode(Pin, INPUT);
     d=d>>1; // now the next bit is in the least sig bit position.
}//end OneWireOutByte

byte OneWireInByte(int Pin) // read byte, least sig byte first
   byte d, n, b;

   for (n=0; n<8; n++)
       digitalWrite(Pin, LOW);
       pinMode(Pin, OUTPUT);
       pinMode(Pin, INPUT);
       b = digitalRead(Pin);
       d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
}//end OneWireInByte

void readTture(byte Pin){
//Pass WHICH pin you want to read in "Pin"
//Returns values in... (See global declarations)
 OneWireOutByte(Pin, 0xcc);
 OneWireOutByte(Pin, 0x44); // perform temperature conversion, strong pullup for one sec

 OneWireOutByte(Pin, 0xcc);
 OneWireOutByte(Pin, 0xbe);

 LowByte = OneWireInByte(Pin);
 HighByte = OneWireInByte(Pin);
 TReading = (HighByte << 8) + LowByte;
 SignBit = TReading & 0x8000;  // test most sig bit
 if (SignBit) // negative
   TReading = (TReading ^ 0xffff) + 1; // 2's comp
 Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

 Whole = Tc_100 / 100;  // separate off the whole and fractional portions
 Fract = Tc_100 % 100;
};//end readTture

void printTture(){//Uses values from global variables.
//See global declarations.
//N.B.: No new line inside printTture
     myFile = SD.open("test.txt", FILE_WRITE); //+++++++++++++OPEN
if (SignBit) // If it's negative

 if (Fract < 10)
   Serial.print("       ");
   myFile.print("  ,    ");
 myFile.close();//++++++++++++++++++++++++ CLOSE
};//end printTture

Go Up