Go Down

Topic: Conflict between lcd 1602 and SD Card (Read 187 times) previous topic - next topic

EduardoBan

I am using an example that connects an LCD 1602, an SD memory, DS1307 clock and a DHT22 sensor to an Arduino one, in my case I use an Arduino nano.

Uno witch DHT, SD and LCD

I test the operation without enabling the storage in the SD memory and it works correctly, shows the values well, etc.

I just define the memory with the instruction:


SD.begin (chipSelect), strange characters appear and it does not work anymore,  the display prints strange characters, some are recognizable, but others are not.

The code is:

Code: [Select]
#include <SD.h>
#include <LiquidCrystal.h>
#include "DHT.h"

#include <Time.h> 

byte thermometer_icon[8] = //icon for termometer
{
    B00100,
    B01010,
    B01010,
    B01110,
    B01110,
    B11111,
    B11111,
    B01110
};

byte humidity_icon[8] = //icon for water droplet
{
    B00100,
    B00100,
    B01010,
    B01010,
    B10001,
    B10001,
    B10001,
    B01110,
};


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

#define DHTPIN A2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);


const int chipSelect = 10;
char message[]= "Logger";
int previous = 0;
int pos = 0;

void setup() {

 // make sure that the default chip select pin is set to
 // output, even if you don't use it:
    pinMode(chipSelect, OUTPUT);
   
  dht.begin();

  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print(" Data");
  lcd.setCursor(0, 1);
  lcd.print("Logger");
  delay(4000);
  Serial.begin(9600); 
 
  lcd.createChar(1,thermometer_icon);
  lcd.createChar(2,humidity_icon);

  lcd.clear();
  lcd.print("Initializing SD...");
    Serial.print("Initializing SD...\n");

//*******************************************************
  if (!SD.begin(chipSelect)) {
    // don't do anything more:
    Serial.print("SD Fail\n");
    return;
  }
  Serial.print("SD OK\n");
lcd.print("SD initialized.");

delay(4000);
}


//a counter to increase after each reading   
int counter = 0;
 
void loop() {
  char val_char[10] ;
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float dew = dewPoint(t, h);

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    lcd.setCursor(0, 0);
    lcd.print("Failed to read from DHT");
    Serial.println("Failed to read from DHT");
  } else {
  //  lcd.clear();
    lcd.clear();
    lcd.setCursor(0, 0);
   // lcd.write(2);
    lcd.print("H:");
    itoa (h, val_char, 10);
    lcd.print(val_char);
    lcd.print("% ");
    lcd.print("T:");
    itoa (t, val_char, 10);
    lcd.print(val_char);
    lcd.print((char)223); //degree sign
    lcd.print("C"); //degree sign
    lcd.setCursor(0, 1);
  //  lcd.print("*C");
    lcd.print("PRocio:");
    itoa (dew, val_char, 10);
    lcd.print(val_char);
    lcd.print((char)223); //degree sign
    lcd.print("C");
    Serial.print("Hum: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println((char)223); //degree sign


   
  // 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 dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.print(counter);
    dataFile.print(", ");
    dataFile.print("Humidity: ");
    dataFile.print(h);
    dataFile.print(", Temperature: ");
    dataFile.print(t);
    dataFile.print(", Dew Point: ");
    dataFile.print(dew);
    dataFile.print("\n");
    dataFile.close();
  } 
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
     
  }
 
 
   delay(3000); 
   counter++;
}

/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
        return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
        return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}



The captures of the connection diagram and the messages of the Lcd 1602 I add them

Can someone help me solve the problem of strange characters

PaulRB

#1
Dec 15, 2018, 12:15 pm Last Edit: Dec 15, 2018, 12:34 pm by PaulRB







I cannot see any error in your code or schematic to cause the wierd characters. Is it possible that you have made a wiring error, or that two wires are touching & shorting?

Paul__B

Is is unhelpful that the circuit schematic has been cut off and cannot be fully traced because we are not sure whether there is some subtle glitch hidden somewhere.

Suggest you try a 100 µF capacitor directly between pins 1 and 2 of the LCD, particularly if there is any distance between the modules.    By the way - and not related to your problem - a useful hint.  You have unusually specified a 1k potentiometer on "Vo" - pin 3 of the LCD.  That is rather fortunate.  Do not connect the contrast potentiometer to 5 V - just leave that end unconnected (or tie it to the wiper).

This is a silly mistake that has become ingrained in hobby (and no doubt, many professional) designs since the "early days".  Not connecting it makes contrast setting twice as easy with a 10k pot, but using a 1k pot makes it much easier again!  :smiley-lol:



Go Up