Introduction: Connect LCDs With NodeMCU and Arduino

About: Part software developer, part maker.

Most of our Arduino projects require a way to present us with information about their state, and very often an LED can be enough to indicate if a condition is true or false.

But, it is not uncommon that we need to display a lot more than just a Boolean state so we opt-in for using a more advanced solution in the form of a display.

In this Instructable, we will look into the Liquid Crystal Displays (or LCDs for short) and how we can connect them in our projects to display the information that we need.

This post is sponsored by PCBWay. Get your custom PCBs, SMD stencils, or assembly services professionally done for cheap in less than 24 hours.

PCBWay also offers sponsorships for students and hobbyists where you can get your projects built for free.

Supplies

Step 1: How Do Character LCDs Work?

LCDs are electronic display devices that consist of several layers with liquid crystals in between them. When current is applied, these liquid crystals can arrange themselves in patterns and will define a visible change on the screen.

They can have multiple colors, but very often they are monochromatic and rely on a backlight that shines from behind the layers to improve the visibility of the screen.

In Arduino projects, it is most common to use so-called character LCDs where the main purpose of the display is to show some textual data. These displays contain a matrix of characters spaced in columns and rows.

Two of the most common LCDs for use with Arduino are the 16x2 and 20x4 displays. The first number in these displays defines the number of characters they can show in a single row and the second number indicates the number of rows that the display has.

Step 2: How to Connect a Character LCD to NodeMCU or Arduino

Both of these displays are connected in the same way with a parallel connection and they have a total of 16 pins. To communicate with the display we need to connect at least 6 pins from the Arduino to show data to it and this is a problem on more complex projects when pin availability comes at a premium.

In situations where that data on the display needs to be updated fast, we need to use 8-bit communication instead of 4 bit. That requires 10 pins from the microcontroller and on devices like the Arduino pro mini, we can barely use this method.

To overcome this problem, certain adapter boards can accept an I2C connection over two wires and convert the signal to the parallel output that the display requires. These boards are usually based on the PCF8574 I/O expander chip and can either be soldered directly to the display or you can connect them through pin headers as in my case.

The board comes with a small trimpod that you can use to adjust the contrast of the screen and a small jumper that you can remove to turn off the backlight of the display.

The board is inexpensive and definitely recommended for any project requiring an LCD. You can check out the supplies section for links to all of the components that are used in this video.

To connect the display we connect the SDA and SCL pins of the adapter board, to the equivalent pins on the board that you are using. On the NodeMCU board that I'm using the pins are D2 and D1 appropriately.

Step 3: Using Multiple Displays at the Same Time

Since we can speak to multiple devices from the same I2C lines, each connected device has its own address. Depending on the specs and the device, the address is usually specified by the manufacturer but we can use a very simple sketch to actually find all of our I2C device addresses.

This sketch will initiate the communication and start speaking to each of the available addresses from 8 till 120. Whenever a device replies back, it reports the address and increases the count of the found devices.

In our case, the address is 39, or 0x27 in hex and it does match the address as specified on the adapter board for the display. If for some reason we want to have two such displays in our project, there are solder pads on the back of the adapter board that we can join with solder and that will update the address as shown on the table on the screen.

Step 4: Arduino Code for the Liquid Crystal Displays

To be able to speak to the display over I2C, we need a library called LiquidCrystal_I2C that can be downloaded and installed through the Library Manager in the Arduino IDE.

When using this library with NodeMCU, you might get a warning that the library is not compatible with the board but in my case, everything worked as expected.

To test out the display, I've created a small sketch that is heavily based on one of the examples from the library.

At the top of the sketch, we first include the Liquid Crystal library and initialize the communication with the HEX display address that we found and we set the columns and rows properties as on the display that we have.

Now in the setup function, we first need to correct the error that we did in connecting the SDA and SCL lines with the Wire.begin command. If we haven't changed the default I2C lines we would not have to specify the pins that we use but this is easy enough and a lot more intuitive to know exactly what happens.

After the Wire communication is initiated, we can initialize the display by calling its init function. We can use a trick to call the init twice as that will make sure to clear out any previous characters from the display.

Next, we need to turn on the screen's backlight and then start to set the content of the 4 character rows individually with the setCursor and print commands of the library.

The setCursor command accepts two parameters. The first one is the index of the position in the row where we want the first character to show up and the second one is the index of the row that we want to write to.

Once the content is set, the display will keep that state until a new command is received so in this example we don't need to do anything else in the loop part of the Arduino code and the screen will have the text shown until the power is cut from it.

Step 5: More Advanced Example

With the basic example out of the way, I've now created a more advanced sketch where I've implemented both the code for working with the DS1302 real-time clock module and the 20x4 LCD.

You can check my previous Instructable for more details on the real-time clock.

Here in this example, we retrieve the time from the real-time clock, and instead of just displaying that time in the serial monitor, I now print it to the first row of the LCD and it gets updated each second.

I won't go into details explaining the entire code but you can download all of the sketches and explore them yourself.

With that, I hope that you learned something from this Instructable and I will encourage you to like, share and subscribe to my YouTube channel so we can continue learning more stuff in the future.

I plan to upgrade this project where I will next add a settings menu that will be accessed with a rotary encoder so stay tuned for that.

Cheers and thanks for reading.

Attachments