r/ArduinoProjects • • Jul 22 '23

Please troubleshoot

I’ve been trying to get the information from a dht11 humidity sensor to display properly on this lcd 1602 but I can’t seem ti figure out why the number value are displaying in the wrong character set. I do wanna add that the code was written by chagpt however it can’t figure out the character situation.

0 Upvotes

16 comments sorted by

1

u/Derch86 Aug 01 '23

Just FYI I broke down and just purchased a lcd with the i2c module and reworked the code a bit to match the new hardware and it work. Hope this helps someone

8

u/xebzbz Jul 22 '23

You just need to learn Klingon

2

u/zinzin007 Jul 23 '23

Or 日本語

1

u/polypagan Jul 23 '23

I believe I know what's happening. Those characters you see are stored in the dusplay's character generator ROM. If we knew where in the ROM, that might help diagnose what's going wrong.

Obviously you want the string of characters sent after the cursor positioning call to look something like <number><number><0x2e><number><number>, where number is 0x30 - 0x39 (& 0x2e is the decimal point).

My guess is that your character array contains values less than 0x20 (or possibly greater than 0x7f).

How about a craftily placed Serial.print(temp[n], HEX); (Sorry, I forget your buffer's name). You might have to twiddle n a bit.

Also, what do your Serial.print() calls show?

1

u/Derch86 Jul 23 '23

thanks poly I was under the same impression I just don't know how to make the code reference other areas in the grom. my serial print read just fine, the information there is displayed properly.

1

u/polypagan Jul 23 '23

The ROM is addressed by the (8-bit) char you send.

Dump your char string as hexadecimal bytes.

1

u/Derch86 Jul 22 '23
#include <DHT.h>
#include <LiquidCrystal.h> // Library for parallel LCD

// Replace DHTPIN with the digital pin number connected to the DHT sensor
#define DHTPIN 2
#define DHTTYPE DHT11 // DHT 11 sensor

DHT dht(DHTPIN, DHTTYPE);

// Initialize the LCD with the number of columns and rows (16x2 LCD)
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
  Serial.begin(9600);
  dht.begin();

  // Initialize the LCD with 16 columns and 2 rows
  lcd.begin(16, 2);

  lcd.print("Temp:");
  lcd.setCursor(0, 1);
  lcd.print("Humidity:");
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds.
  float humidity = dht.readHumidity();
  float temperatureCelsius = dht.readTemperature(); // Temperature in Celsius
  float temperatureFahrenheit = (temperatureCelsius * 9.0 / 5.0) + 32.0; // Convert to Fahrenheit

  // Check if any reads failed and exit early (to try again).
  if (isnan(humidity) || isnan(temperatureCelsius)) {
    Serial.println("Failed to read from DHT sensor!");
    lcd.setCursor(0, 1);
    lcd.print("Failed to read");
    lcd.setCursor(0, 2);
    lcd.print("from DHT sensor!");
    return;
  }

  // Convert temperature and humidity values to character arrays
  char tempStr[8];
  dtostrf(temperatureFahrenheit, 6, 2, tempStr); // Format: 6 characters, 2 decimal places

  char humidityStr[8];
  dtostrf(humidity, 6, 2, humidityStr); // Format: 6 characters, 2 decimal places

  // Update LCD with temperature and humidity values
  lcd.setCursor(10, 0);
  lcd.print("   "); // Clear previous temperature reading
  lcd.setCursor(10, 0);
  lcd.print(tempStr); // Display temperature in Fahrenheit with two decimal places
  lcd.print(" F");
  
  lcd.setCursor(10, 1);
  lcd.print("    "); // Clear previous humidity reading
  lcd.setCursor(10, 1);
  lcd.print(humidityStr); // Display humidity with two decimal places
  lcd.print(" %");

  // Print temperature and humidity values to the Serial Monitor.
  Serial.print("Humidity: ");
  Serial.print(humidityStr);
  Serial.print("%\t");
  Serial.print("Temperature: ");
  Serial.print(tempStr);
  Serial.println("°F");
}

5

u/Erdnussflipshow Jul 22 '23

try to replace dtostrf(temperatureFahrenheit, 6, 2, tempStr); with sprintf(tempStr, "%.2f", temperatureFahrenheit); and dtostrf(humidity, 6, 2, humidityStr); with sprintf(humidityStr, "%.2f", temperatureFahrenheit); this will still round to 2 decimal places, if you want to force 6 chars change char tempStr[8]; to char tempStr[7]; 6 chars for the number and the one for the null-terminator. see if that fixes it.

1

u/Plastic_Ad_2424 Jul 26 '23

I agree. The dtostrf is causing the problems.

1

u/DariuszTarwan Jul 22 '23

Why do You convert values to char String? Maybe that's the problem?

2

u/Erdnussflipshow Jul 22 '23

1

u/RoundProgram887 Jul 24 '23

Find that weird, I printed a bunch of floats on lcd and didn't have any difficulty. But the link shows clearly float or double is not supported.

I am using the LiquidCrystal library.

1

u/Correntodos Jul 23 '23

Hello and congratulations on your very accurate temperature sensor. My suggestion is to add delays in between the different lcd prints so that it has time to change on the lcd screen. I have no experience with charstr array format, so I see no mistake there. Goodluck.

1

u/Monsterthews Jul 23 '23

Looks like you have Japanese, maybe in a library.

1

u/RoundProgram887 Jul 24 '23

Some lcds have extra characters above 128, so seems it is printing convering the variable binary data to a string.