0

I'm trying to read temperature and humidity from a DHT11 sensor and write values to a 20x4 LCD. I hit a behaviour I don't understand. When I use the DHT tester program that come with the DHT library, everything works well. I get the DHT information on the Serial console. This DHT tester program use the pin 2 to read the data from the DHT.

I wrote the following code who works perfectly:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>  // F Malpartida's NewLiquidCrystal library
#include <DHT.h>

#define DHTTYPE DHT11
#define DHTPIN 13      // Digital

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C    lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {
  Serial.begin(9600); 
  while (!Serial) {
  }
  Serial.println("Serial OK!"); 

  dht.begin();
  lcd.begin(20,4);
  lcd.setBacklight(0);
  lcd.clear();
  lcd.print("Hello! ");

  Serial.println("Init finished!"); 
}

void loop() {
  delay(2000); 

  float DHT_hum = dht.readHumidity();
  float DHT_temp_c = dht.readTemperature(false);
  float DHT_temp_f = dht.readTemperature(true);

  if (isnan(DHT_hum) || isnan(DHT_temp_c) || isnan(DHT_temp_f)) {
    Serial.println("Failed to read from DHT sensor!!");
    lcd.clear();
    lcd.print("fail! "); 
    return;
  }

  float heat_index = dht.computeHeatIndex(DHT_temp_f, DHT_hum);

  Serial.print("Humidity: "); 
  Serial.print(DHT_hum);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(DHT_temp_c);
  Serial.print(" *C ");
  Serial.print(DHT_temp_f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(heat_index);
  Serial.println(" *F");

  lcd.clear();
  lcd.print("Humi: ");  
  lcd.print(DHT_hum);  
  lcd.print("%");  
  lcd.setCursor(0, 1);
  lcd.print("Temp: ");  
  lcd.print(DHT_temp_c); 
  lcd.print("C"); 
  lcd.setCursor(14, 0);
  lcd.print("HI: ");
  lcd.setCursor(14, 1);
  lcd.print(dht.convertFtoC(heat_index)); 
  lcd.print("C");

}

As you can see, I use pin 13 for the DHT11 (DHTPIN). If I set to it to "2" then my program is no longer working (having the pin connected or not doesn't change the result). The serial display

Serial OK!
Init finished!
Failed to read from DHT sensor!!

and that's all, it even doesn't loop to display "Failed to read from DHT sensor!!" again. LCD text doesn't change too. Like if the program is totally exiting after "Serial.println("Failed to read from DHT sensor!!");"

I'm lost... The LCD lib I use is the very common one from fm https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home

0
LiquidCrystal_I2C    lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

Are you not using pin 2 in the LiquidCrystal_I2C constructor? So you can't also use it for the DHT sensor.


I withdraw that suggestion.


Can you please try saving some RAM?

Use the F macro with your prints, eg.

  Serial.print(F("Humidity: ")); 
  Serial.print(DHT_hum);
  Serial.print(F(" %\t"));
  Serial.print(F("Temperature: ")); 

Ditto with the lcd.print() calls.

  • From my understanding, the pin used in the constructor, are the pin used by the logic chip on the LCD panel, not on the arduino. I don't have anything from the LCD connected on pin 2 and LCD works. If I changed '2' to another (not used) pin, the LCD no longer work. – radius Jul 7 '15 at 9:09
  • I don't see how that could be right. Why would the code care what pin is on the LCD logic chip? It needs to know what pin on the Arduino it uses for certain functions. – Nick Gammon Jul 7 '15 at 21:17
  • Because all LCD panel are not wired the same way. see arduino-info.wikispaces.com/LCD-Blue-I2C "Set the pins on the I2C chip used for LCD connections" – radius Jul 7 '15 at 23:28
  • Hmm, that's odd. I see what you mean. Please see revised answer. – Nick Gammon Jul 7 '15 at 23:59
0

I'm self answering.. On my Arduino Leonardo, the SDA and SCL pin used for the I2C connexion to the LCD panel, are the same pin than the Digital pin 2 and 3. So using SDA/SCL remove ability to use pin 2 & 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.