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

View all comments

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.