Use LCD and sd with Uno

I set up the 16x2 LCD display and added a micro SD card reader/writer. The SD won't initialize. As the SD uses digital 10, 11, 12 and 13, I re-assigned the LCD from 7, 8, 9, 10, 11, 12 to 4, 5, 6, 7, 8, 9. This was changed in the connections and the sketch. What is wrong?

What sketch would that be ? Please post it here

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

I think it's :
Your wiring!
Your Code!
The SD is not formatted correctly!

Have you tried JUST the example code supplied with the SD library?

Please post your code.

Post a schematic of how the wiring should be. A schematic is less ambiguous than a written description.

Post photos of how the wiring actually is.

Thank you so much for all the help and suggestions. I was cleaning up my wiring to take a better photo when it started to work and I think I must have had a connection wrong. Just to see how to use the forum here is the working code

//www.elegoo.com
//2016.12.9

/*
  LiquidCrystal Library - Hello World

  Demonstrates the use of a 16x2 LCD display.  The LiquidCrystal
  library works with all LCD displays that are compatible with the
  Hitachi HD44780 driver. There are many of them out there, and you
  can usually tell them by the 16-pin interface.

  This sketch prints "Hello World!" to the LCD
  and shows the time.

  The circuit:
   LCD RS pin to digital pin 7
   LCD Enable pin to digital pin 8
   LCD D4 pin to digital pin 9
   LCD D5 pin to digital pin 10
   LCD D6 pin to digital pin 11
   LCD D7 pin to digital pin 12
   LCD R/W pin to ground
   LCD VSS pin to ground
   LCD VCC pin to 5V
   10K resistor:
   ends to +5V and ground
   wiper to LCD VO pin (pin 3)

  Library originally added 18 Apr 2008
  by David A. Mellis
  Library modified 5 Jul 2009
  by Limor Fried (http://www.ladyada.net)
  Example added 9 Jul 2009
  by Tom Igoe
  Modified 22 Nov 2010
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/

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

//www.elegoo.com
//2018.10.25


#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11

static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );

// initialize the library with the numbers of the interface pins
// LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// Had to re-assign the numbers to free up 10, 11, 12 & 13 for the SD card
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);

const int chipSelect = 10;
File root;
/*
   Initialize the serial port.
*/
void setup( )
{
  Serial.begin( 9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");

}



/*
   Poll for a measurement, keeping the state machine alive.  Returns
   true if a measurement is available.
*/
static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if ( millis( ) - measurement_timestamp > 3000ul )
  {
    if ( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return ( true );
    }
  }

  return ( false );
}



/*
   Main program loop.
*/
void loop( )
{
  float temperature;
  float humidity;
  char line0[21];
  char line1[21];

  /* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
  if ( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print( "T = " );
    Serial.print( temperature, 1 );
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" );
    lcd.begin(16, 2);
    lcd.print( "T = " );
    lcd.print( temperature, 1 );
    lcd.print( " deg. C" );
    lcd.setCursor(0, 1);
    lcd.print( "H = " );
    lcd.print( humidity, 1 );
    lcd.print( " %" );
    // 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 myFile = SD.open("test.txt", FILE_WRITE);

    // if the file opened okay, write to it:
    if (myFile) {
      myFile.print( "T = " );
      myFile.print( temperature, 1 );
      myFile.print( " deg. C, H = " );
      myFile.print( humidity, 1 );
      myFile.println( "%" );
      // close the file:
      myFile.close();
    } else {
      // if the file didn't open, print an error:
      Serial.println("error opening test.txt");
    }
    delay (30000);
  }

}

void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry = dir.openNextFile();
    if (! entry) {
      // no more files
      //Serial.println("**nomorefiles**");
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}

My wiring to use a temp and humidity sensor with 2x16 display and SD card to save the data.

1 Like

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