DHT22 sensors displaying weird characters

Hello everyone! I just recently got into arduino and microcontrollers. My latest project is working with the DHT22 sensors and getting their readout on a LCD screen. There seems to be something wrong with the way I'm using the humidity call because I get these two weird characters every time I output humidity to the LCD screen. Also, as I've been debugging, I'm not sure why "!test!" starts on line 3 and then continues to line 2 or why that "*" comes between the humidity output and the weird characters the first time. Any help would be much appreciated! Cheers!

Here's what those "weird characters" look like:

and here's my sketch code:

#include <LiquidCrystal.h>
#include <DHT.h>

int f=8;
int v=7;
float Ftemp=-1;
float Fhum=-1;
float Vtemp=-1;
float Vhum=-1;

DHT Fdht(f, DHT22);
DHT Vdht(v, DHT22);

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
  Serial.println("Debug mode");
  // Print a message to the LCD.
  lcd.print("Testing sensors");
  delay(3000);
}

void loop() {
  Ftemp = Fdht.readTemperature(true);
  Fhum = Fdht.readHumidity();
  Vtemp = Vdht.readTemperature(true);
  Vhum = Vdht.readHumidity();
  Serial.print("FTemp ");
  Serial.println(Ftemp);
  Serial.print("FHum ");
  Serial.println(Fhum);
  Serial.print("VTemp ");
  Serial.println(Vtemp);
  Serial.print("VHum ");
  Serial.println(Vhum);
  lcd.clear();
  lcd.print("F T:");
  lcd.print(Ftemp);
  lcd.print(" H:");
  lcd.print(Fhum);
  lcd.println("*");
  lcd.print("V T:");
  lcd.print(Vtemp);
  lcd.print(" H:");
  lcd.println(Vhum);
  lcd.print("!test!");
  delay(2000);
}

I'm using dht_sensor_library not dht_sensor_library_master if that makes any difference.

This line:

lcd.begin(16, 2);

Tells the library you have an LCD display connected that is 16 characters wide and has 2 lines.

Your display is 20 characters wide and 4 lines so try:

lcd.begin(20, 4);

Hi again,

I just had a look at the LiquidCrystal library (BTW I have never used it!) and it does not properly support the print stream.

This means that when you use "println" two extra characters are added to the end of the line these are "carraige return" and "line feed". These are interpreted by the LCD as two weird characters.

Instead use just plain print and then when you want to move to a different line:

lcd.setCursor(thisCol, thisRow);

as in the SetCursor example.

1 Like

Hi rowboteer, thanks so much for the speedy reply. I can't believe I missed the initialization... I was using a 16X2 and upgraded but forgot to change those numbers. I'm not sure I ever would have figured out the println problem though. Thanks again, everything is working smoothly now!

@rowboteer
You got about 99.9% of that with this statement:

"This means that when you use "println" two extra characters are added to the end of the line these are "carraige return" and "line feed". These are interpreted by the LCD as two weird characters."

Here's the rest:

The two characters are 'weird' because they are coming from the CGRAM (Character Generator Random Access Memory) in the LCD controller. Since this is RAM, and since it has not been initialized (see below), you get some strange character. I suspect you will get the same result each time on any particular display and possibly a different but repeatable result on another display.

The 'carriage return' has an ASCII code of 0x0D or 0b00001101 and the 'line feed' has an ASCII code of 0x0A or 0b00001010. The LCD controller treats these ASCII codes as memory addresses and these particular codes point to the CGRAM and specifically to two of the the eight 'foldback' (duplicate) addresses that correspond to the addresses of the eight 'custom characters' available on the LCD.

So, if you were to program something into the 'custom characters' at locations 5 and 2 those characters would show up in place of the 'weird' ones.

Don

I'm not sure why "!test!" starts on line 3 and then continues to line 2. . .

If you do not do any cursor positioning then the LCD controller inherently places characters through rows 1, 3, 2, 4 in that order.

If you specify a 16x2 and use a 20x4 then even if you do cursor positioning it will (most likely, I haven't looked at the code lately) be messed up on rows 3 and 4, but rows 1 and 2 should be OK.

Don

shab154:
Thanks again, everything is working smoothly now!

No problem, good luck with your project. :slight_smile: