[SOLVED]LCD 16x2 showing garbage

That did it, I was using pin 2 for the last bit of the LCD. Thank you very much!

Where can I look at all the pin usage? Maybe this will save me a lot of trouble in the future.

mrescude:
That did it, I was using pin 2 for the last bit of the LCD. Thank you very much!

Where can I look at all the pin usage? Maybe this will save me a lot of trouble in the future.

Look at the middle of the page "input and output". For shields, you should be able to find pin usage where you buy them.

What happened today is a good thing for me. As a shield designer I should have looked at Leonardo. Now that I have, I'll spend some time thinking what to do with my shield update, especially with pins 2 and 3 now.

Well, just in case anyone wants to see the working code... I just changed the pin usage in the LCD to LiquidCrystal lcd(13,12,11,9,8,7),

#include <LiquidCrystal.h>
#include <SdFat.h>
SdFat sd;
SdFile myFile;
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68  // This is the I2C address


// Global Variables
const int chipSelect = 4;
int command = 0;       // This is the command char, in ascii form, sent from the serial port     
int i;
int second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;
byte zero=0x00;
char nombrearchivo[13];
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13,12, 11, 9, 8, 7);

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{  return ( (val/10*16) + (val%10) );}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{  return ( (val/16*10) + (val%16) );}
 
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers, Probably need to put in checks for valid numbers.
// setDateDs1307() solo se necesita una vez, se mantiene el el codigo, por si se debe cambiar la bateria del RTC o modificar la fecha u hora. 
/*void setDateDs1307()                
{
   second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.  
   minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   dayOfWeek = (byte) (Serial.read() - 48);
   dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   Wire.write(zero);
   Wire.write(decToBcd(second) & 0x7f);    // 0 to bit 7 starts the clock
   Wire.write(decToBcd(minute));
   Wire.write(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   Wire.write(decToBcd(dayOfWeek));
   Wire.write(decToBcd(dayOfMonth));
   Wire.write(decToBcd(month));
   Wire.write(decToBcd(year));
   Wire.endTransmission();
}*/

// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(Wire.read() & 0x7f);
  minute     = bcdToDec(Wire.read());
  hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  dayOfWeek  = bcdToDec(Wire.read());
  dayOfMonth = bcdToDec(Wire.read());
  month      = bcdToDec(Wire.read());
  year       = bcdToDec(Wire.read());
  
  snprintf(nombrearchivo,13,"%02d%02d20%02d.txt", second, month, year); //merge together
}
 
 
void setup() {
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // set up the LCD's number of columns and rows:  
  lcd.begin(16, 2);
  
  lcd.print("Inicializando...");
  delay(1000);
  lcd.clear();
  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // change to SPI_FULL_SPEED for more performance.
  
    if (!sd.begin(chipSelect, SPI_HALF_SPEED)){ 
      sd.initErrorHalt();
      lcd.print("SD Error!!!     ");
  
  }
  else{lcd.print("SD Lista!!!     "); }
  delay(500);
  lcd.clear();
  Wire.begin();
  lcd.print("Sist Preparado!!");
  delay(500);
  
}
 
void loop() {
     /*if (Serial.available()) {      // Look for char in serial que and process if found
      command = Serial.read();
      if (command == 84 || command == 116) {      //If command = "Tt" Set Date
       //setDateDs1307();
       getDateDs1307();
       
       
       if (!myFile.open(nombrearchivo, O_RDWR | O_CREAT | O_AT_END)) {
        sd.errorHalt("opening test.txt for write failed");
      }
      // if the file is available, write to it:
      Serial.print("Writing to test.txt...");
      myFile.println("testing 1, 2, 3.");

      // close the file:
      myFile.close();
      Serial.println("done.");

      }
      else if (command == 82 || command == 114) {      //If command = "Rr" Read Date ... BBR
       getDateDs1307();
       Serial.println(" ");
      }
      
      Serial.print("Command: ");
      Serial.println(command);     // Echo command CHAR in ascii that was sent
      }
      command = 0;                 // reset command 
      delay(100);*/
      getDateDs1307();
      year+=2000;
      if (!myFile.open(nombrearchivo, O_RDWR | O_CREAT | O_AT_END)) {
        sd.errorHalt("opening test.txt for write failed");
      }
      // if the file is available, write to it:
      Serial.print("Writing");
      lcd.setCursor(0, 0);
      lcd.print("Escribiendo:    ");
      lcd.setCursor(0, 1);
      lcd.print(nombrearchivo);
      myFile.println("testing 1, 2, 3.");
      if(!myFile.timestamp(T_WRITE,year,month,dayOfMonth,hour,minute,second)){
      Serial.println("Mod date failed");
      }
      // close the file:
      myFile.close();
      Serial.println("done.");
      
      delay(500);
      
      
  }

Thank you! I couldn't have done it without your help.

For shields, you should be able to find pin usage where you buy them.

How about here: --> http://shieldlist.org/

Don

floresta:

For shields, you should be able to find pin usage where you buy them.

How about here: --> http://shieldlist.org/

Don

A good starting point but not the most updated list I wish it would be. Most of my shields aren't listed :wink:

Most of my shields aren't listed...

Did you scroll down the page?
"If you know of a shield that needs to be added, please tell me about it!"

On the other hand you probably did.
"There are currently 285 shields submitted that I haven't got to processing yet. Sorry!"

That's not too good considering there are only 288 listed. Maybe he has a day job.

Don

floresta:

Most of my shields aren't listed...

Did you scroll down the page?
"If you know of a shield that needs to be added, please tell me about it!"

On the other hand you probably did.
"There are currently 285 shields submitted that I haven't got to processing yet. Sorry!"

That's not too good considering there are only 288 listed. Maybe he has a day job.

Don

I requested but he was probably too busy.

Folks,
I'm having the same problem. I have a shield with combined RTC and SD card. I've wired up the LCD (16 x 2) and it works perfectly when I use the simple sample code for the LCD. I've noticed that it also works fine when I integrate the code into my program which utilizes the RTC and SD card IF AND ONLY IF I write to the LCD before initializing the SD or RTC. Actually, I saw the comments about SPI conflicts etc, so I modified the pins for the LCD a few times and found that in the original configuration, I could write to the LCD successfully AFTER the RTC initialization, but not after the SD card. Then, after I switched the pins, and it ONLY works if I write to the LCD at the beginning of the Setup() function before both RTC and SD calls. I thought I understood the potential conflicts so I've wired the LCD to pins as follows:

LiquidCrystal lcd(7,8,9,6,5,0);

This doesn't seem to be using any pins that conflict, but maybe I'm missing something? I have an Uno and am also using pins 2-4 for an optical sensor so they are not avail. Thoughts on how to resolve the conflict? Oh, the problem I'm having is that any writes to the LCD are correct ahead of the RTC/SD initializations, but I get gibberish when I write after those calls.
Thanks,
Cameron

Are you using the SD slot in the ethernet shield? If so, the SD chip select is pin 4, that could be causing your problems.

LiquidCrystal lcd(7,8,9,6,5,0);

Are you going to keep us in the dark about your code? For example, are you using the Serial Monitor?

By the way, you tacked this on to an old thread that is marked [SOLVED], not really a good idea.

Don

Thanks guys. Yes. I'm using the SD card, the Serial Monitor and the RTC. Everything works fine if I leave out the code for the LCD, so I don' t see how pin 4 might be conflicting with my optical sensor. I also thought that the chipselect was pin 10. Here is the code.

 #include "SD.h"
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>

//These define the pin connections of the Arduino.  
//They can be changed but only use digital in 2 or 3 for the Freq pin
#define TSL_FREQ_PIN 2 // output use digital pin2 for interrupt

float fD = 0.1; // Dark frequency
float Re = 2.3; // Irradiance responsivity
float eff475 = 0.113; // Luminous efficiency at 475nm, for dinoflagellate bioluminescence

RTC_DS1307 RTC; // define the Real Time Clock object

#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

// the digital pins that connect to the LEDs
#define redLEDpin 3
#define greenLEDpin 4

// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;

// the logging file
File logfile;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
  
  // red LED indicates error
  digitalWrite(redLEDpin, HIGH);
  
  while(1);
}

int timing = 1000; // in milliseconds
volatile unsigned long pulse_cnt = 0;

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

void setup() {
   // IF I PUT THE LCD CODE HERE, IT WORKS FINE (see bottom)

  Serial.begin(9600);
  Serial.println();
  Wire.begin();
  RTC.begin();

#if WAIT_TO_START
  Serial.println("Type any character to start");
  while (!Serial.available());
#endif //WAIT_TO_START

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(chipSelect, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
// PROBLEM CODE IS HERE:
    // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
 lcd.setCursor(0, 0);
  // Print a message to the LCD.
 lcd.print("LCD not working!");

Thanks guys. Yes. I'm using the SD card, the Serial Monitor and the RTC.

It is probably the Serial Monitor and the LCD competing for the use of pin 0.

Don

No. I switched pin 0 around before. Same thing unfortunately. Doesn't make any sense to me. I've looked at all the pins to find which one might conflict with the SPI etc and I'm not using any of those pins. Perhaps pin 4 which is now hardwired unfortunately, is suddenly becoming a problem, tho is was fine before the LCD. Dunno. Thx

We can only troubleshoot based on the information that you provide, when you withhold information it just wastes time.

You posted code that clearly shows that the LCD is using pin 0 and when asked you told us that you are also using the Serial Monitor. Since the Serial Monitor always uses pins 0 and 1 this is a clear conflict that could very well result in garbage being displayed on the LCD.

Now you tell us that you "switched pin 0 around" and you got the "Same thing". What we need to know is
(1) what was the configuration after you switched things
(2) what code was used when you switched things
(3) was the 'garbage' the same garbage as before or was it different
(4) in either case did the garbage resemble what was supposed to be there (same number of characters as expected, same location as expected, etc.)

I have a shield with combined RTC and SD card.
...
This doesn't seem to be using any pins that conflict, but maybe I'm missing something?

You haven't identified your shield so how can we tell if you are missing something?

Don

Got it. The shield I'm using is this one: Adafruit Data logging shield for Arduino [v1.0] : ID 243 : $24.80 : Adafruit Industries, Unique & fun DIY electronics and kits. I set up the LCD using this tutorial Wiring a Character LCD | Character LCDs | Adafruit Learning System. Of course I had suspected a conflict between the LCD and either the SD card or the Serial comm since I could place the LCD print code before initialization and it worked fine. I searched around the net to find potential conflicting pins. The last pin I changed was the DB7 from pin 12 to pin 0. I also tried pin 1. The results are the same. Text displays correctly at the beginning of the Setup() function but displays random characters that generally look alike if the code is placed below the initialization of either the SD or the serial. I get that there are potential conflicts. This is a community project and the others had earlier hardwired pins 2-4 so these are not an option. I guess the question is, if the SD card uses pins 10, 11, 12, and 13; and the serial uses 0 and 1, and I'm already using 2-4 for the sensor, and 5,6,7, 8, 9 for the LCD, then the DB7 pin which was on 12 then I placed on 1, then 0 and I had the same results, then I'm stuck without any pins - unless I can use an analog pin, which I haven't tried yet. BTW, the garbage was nothing near what it should have been, randomly place on the screen and odd ascii chars.
thx

bluenomad:
I guess the question is, if the SD card uses pins 10, 11, 12, and 13; and the serial uses 0 and 1, and I'm already using 2-4 for the sensor, and 5,6,7, 8, 9 for the LCD, then the DB7 pin which was on 12 then I placed on 1, then 0 and I had the same results, then I'm stuck without any pins - unless I can use an analog pin, which I haven't tried yet.

The LCD is just a module and you should be able to wire it where you like, including the analogue pins, rather than suffer the constraints of a shield. I had a similar clash between an LCD shield and the SD. I was obliged to run a jumper across a proto shield and thereby use A2 (16) for the LCD. The SD will surely use 11,1,2,13 but the select is usually on D4, so you need to be certain of that.

It can pay to use a spreadsheet with lots of colour in order to sort the pins out

The shield schematic clearly says D10-13 are used for the SD card and A4-A5 are used for real time clock. Then Leonardo uses D0 and D1 for hardware serial. Use any other pins, including A0-A3 analog pins. D4 is NOT used by the shield. Nick said with a "usually" which means he did NOT read the schematic, AGAIN!!! :stuck_out_tongue: :stuck_out_tongue: :stuck_out_tongue:

Here it is:


You can also buy a backpack from my store to save all your pins:

http://www.inmojo.com/store/liudr-arduino-and-physics-gadgets/item/serial-lcd-back-pack---phi-panel/

Will do you a lot good compared with its price.

BTW, is your RTC working? I suppose not but let me know. I have the same shield but don't have Leonardo.

Guys, with your help, I resolved it. Here is the final config that worked: LiquidCrystal lcd(7,8,9,A2,A1,A0)
Karma, I love your backpacks - will pick a couple up.
Thanks.

Karma, I love your backpacks - will pick a couple up.

That 'Karma' guy must have carpal tunnel syndrome by now.

Don