Help with code, cannot get read out on display

Cant get display to show read out.

#include <DallasTemperature.h>.\

#include <OneWire.h>

/*

const int SENSOR_PIN = 13; // Arduino pin connected to DS18B20 sensor's DQ pin

OneWire oneWire(SENSOR_PIN); // setup a oneWire instance
DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library
LiquidCrystal_I2C lcd(0x3F, 16, 2); // I2C address 0x27, 16 column and 2 rows

float tempCelsius; // temperature in Celsius
float tempFahrenheit; // temperature in Fahrenheit

void setup()
{
sensors.begin(); // initialize the sensor
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
}

void loop()
{
sensors.requestTemperatures(); // send the command to get temperatures
tempCelsius = sensors.getTempCByIndex(0); // read temperature in Celsius
tempFahrenheit = tempCelsius * 9 / 5 + 32; // convert Celsius to Fahrenheit

lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print(tempCelsius); // print the temperature in Celsius
lcd.print((char)223); // print ° character
lcd.print("C");
lcd.setCursor(0, 1); // start to print at the second row
lcd.print(tempFahrenheit); // print the temperature in Fahrenheit
lcd.print((char)223); // print ° character
lcd.print("F");

delay(500);
}

Please read the instructions for posting at the top of each forum and edit your post to correctly display the code. The forum editor doesn't keep the sketch formatting without code tags which makes it unreadable. If you post your code in code tags, more forum members will read it.

Hi

Read: How to get the best out of this forum
Use </> tags to post sketcks or printouts;

Can you please edit your opening post, select all code and click the </> button
to apply code tags and next save your post. It makes it easier to read,
easier to copy and prevents the forum software from incorrect interpretation of the
code.

BTW,
you didn't include any library to control the LCD. (added)
if you include the library "LiquidCrystal_I2C.h" maybe your display will show something.

You would be better off switching to the hs44780 library if you have a 1602 or 2004 LCD that uses the hd44780 display driver chips. For an I2C LCD display to work, the I2C address and the I2C backpack to LCD pin mapping must be correct. If the library default settings for either or both are not correct the LCD will not work. You can try to figure out the right pin mapping and use an I2C scanner to find the address, but if you install and use the hd44780 library that is done automatically by the library.

To install the hd44780 library. The hd44780 library is the best available for I2C LCDs. The library is available in the Library Manager. Go to Library Manager (in the IDE menus, Sketch, Include Libraries, Manage Libraries) and in the Topics dropdown choose Display and in the Filter your search box enter hd44780. Select and install the hd44780 library by Bill Perry.

The class that you want to use is the hd44780_I2Cexp class. There are examples to show how to use the library. The nice thing about the hd44780 library is that it will autodetect the I2C address and the I2C backpack to LCD pin mapping.

In the examples, there is a diagnostic sketch that will help us to help you if you still have trouble with the display. Run the diagnostic sketch and post the results.

Here is your code with the hd44780 library and also the dallas temperature and onewire libraries included. Code verifies but is untested.

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
#include <OneWire.h>
#include <DallasTemperature.h>

const int SENSOR_PIN = 13; // Arduino pin connected to DS18B20 sensor's DQ pin

OneWire oneWire(SENSOR_PIN); // setup a oneWire instance
DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library
//LiquidCrystal_I2C lcd(0x3F, 16, 2); // I2C address 0x27, 16 column and 2 rows
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

float tempCelsius; // temperature in Celsius
float tempFahrenheit; // temperature in Fahrenheit

void setup()
{
   sensors.begin(); // initialize the sensor
   //lcd.init(); // initialize the lcd
   lcd.begin(16,2);
   lcd.backlight(); // open the backlight
}

void loop()
{
   sensors.requestTemperatures(); // send the command to get temperatures
   tempCelsius = sensors.getTempCByIndex(0); // read temperature in Celsius
   tempFahrenheit = tempCelsius * 9 / 5 + 32; // convert Celsius to Fahrenheit

   lcd.clear();
   lcd.setCursor(0, 0); // start to print at the first row
   lcd.print(tempCelsius); // print the temperature in Celsius
   lcd.print((char)223); // print ° character
   lcd.print("C");
   lcd.setCursor(0, 1); // start to print at the second row
   lcd.print(tempFahrenheit); // print the temperature in Fahrenheit
   lcd.print((char)223); // print ° character
   lcd.print("F");

   delay(500);
}

You may need to adjust the contrast pot on the I2C backpack to get the display to be visible.

I would use a different pin than pin 13 for the sensor input. That is the built in LED on many Arduino boards and can effect the input.

Does the display show anything, or is the error with the actual text being displayed?
Have you run the example code supplied with the LiquidCrystal_I2C library?
If you are not getting anything on the display, the I2C address (0x3F) may be incorrect, or the contrast control on the display may need to be adjusted. The above-mentioned hd44780 library has a nice sketch at File > Examples > hd44780 > ioClass > hd44780_I2Cexp > I2CexpDiag that will automatically find the correct I2C address and test the display.

display works with a test sketch.
The back light works,the contrast is turned up.
Wired correct.

Hi @jeff2121
In the code of your post #1 there is no library for LCD included.

Thanks it works great.

                            Jeff

I have zero intention of derailing the thread, so feel free to ignore. Or I can open a new thread. But what makes this library ("the best") better than others available? I'm currently using this, what are the potential reasons for switching?

A few advantages of the hd44780 library:

The name is unique, unlike the multiple versions of LiquidCrystal_I2C that makes locating the correct library difficult.

The library detects the wiring connections between the I2C adapter and LCD display. Some libraries simply assume the most common wiring, others require that you specify the wiring.

The library automatically detects I2C addresses for all connected LCD displays.

The library is currently maintained.

There is a terrific example sketch for testing the display, that prints the display information to the serial monitor, so you can find the I2C address and wiring information needed for other libraries.

If I may add to what @david_2018 said.

The hd44780 library is faster.

The auto detect also means that if something happens to the current display one can replace it with another, equivalent, display easily even if the address and/or pin mapping are different. Just replace the display, no recompile and upload necessary.

Bill Perry (bperrybap), the author of the hd44780 library, is often on the forum and will answer questions.

You certainly may! :smiley:

Great info to know! I might see if I can get it swapped over then. The line wrapping feature is also tempting. If one needed to find info on which subclass to include, where could we find that? I'm using this model, with the I2C adapter board.

At this point, it probably constitutes a thread hijack. Apologies to the OP and admins.

There is an example in the library ioClass example code showing how to use the line wrap feature.

I have not seen that particular display before. If the chip on the backpack is PCF8574 or MCP23008, the hd44780_I2Cexp class should work.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.