LCD I2C NodeMCU beginner tutorial

This LCD I2C NodeMCU beginner tutorial, it is shown how to connect NodeMCU with I2C LCD and how to write scrolling text in various directions. Here we will use the OLED 12832 I2C LCD that has 128 columns and 32 rows. First, it is explained how to interface NodeMCU with the OLED I2C LCD and then it is explained what Arduino libraries for OLED I2C LCD are required and how to install them.

Interfacing NodeMCU with I2C LCD

 The following shows the circuit diagram of interfacing NodeMCU with OLED 12832 I2C LCD.

LCD I2C NodeMCU circuit diagram

The 128x32 OLED I2C LCD has four pins: SDA,SCL,VCC and GND

 The SDA, SCL, VCC and GND pin of OLED LCD is connected to NodeMCU SDA pin D2, SCL pin D1, 3.3V pin and GND pin respectively as shown in the above NodeMCU OLED I2C LCD interfacing circuit diagram above.

Programming NodeMCU with I2C LCD

Here we will be using Arduino IDE to write, compile and upload firmware to the NodeMCU. To write the program for OLED LCD we need to use two libraries which are:

1. Adafruit SSD1306 library(adafruit_SSD1306.h): provides driver support for SSD1206 OLED LCD driver

2. Adafruit GFX library(adafruit GFX.h): provides graphics primitives such as lines, circles, text, etc.

These two libraries can be installed using the Arduino library manager. In Arduino IDE, go to Tools > Manage Libraries.

 library manager

In the Library Manager that opens search for Adafruit SSD1306 library and install it.

Adafruit SSD1306 library

 Then search for Adafruit GFX library and install it.

Adafruit GFX library

Optionally if you use an older version of the above libraries and you get errors due to dependencies during the compilation of the LCD program you need to install the Adafruit BusIO library.

Adafruit BusIO library

Program code for NodeMCU OLED I2C LCD

In this tutorial, we will simply write text to the LCD and scroll it left, right and diagonally left, and right. The program code for this is below.

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32

//create Adafruit SSD1306 display object
Adafruit_SSD1306 OLEDLCD(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, 600000, 200000, OLED_RESET);

void setup() {
	// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
    Serial.println(F("SSD1306 allocation failed"));

  OLEDLCD.clearDisplay();	// Clear the buffer

  OLEDLCD.setCursor(22, 0);
  // Display initial static text

void loop() {
  // Scroll in various directions, pausing in-between:
  OLEDLCD.startscrollright(0x00, 0x0F);
  OLEDLCD.startscrollleft(0x00, 0x0F);
  OLEDLCD.startscrolldiagright(0x00, 0x07);
  OLEDLCD.startscrolldiagleft(0x00, 0x07);

In the above code, we have to use the libraries Wire.h, SPI.h, Adafruit SSD1306, and Adafruit GFX libraries for the I2C OLED LCD to work. The next lines of code define the width and height of the LCD and other constants required for the OLED LCD to work. The address of the OLED LCD used for 128x32 LCD is 0x3C. Then we need to create an object instance called OLEDLCD using Adafruit_SSD1306 constructor with screen dimension, protocol, and reset value.

In the setup() function we initialize the serial communication which is used only for debug purposes. The if statement check whether there is any error during the display object creation with the specified values, and checks any error in SSD1206 OLED LCD driver configuration. The OLEDLCD.begin() method allocates RAM for the image buffer, initialize peripherals and pins. We then clear the contents of the display buffer using the clearDisplay() method, set the text size, and color of the text, and then print text string such as used here for display. The OLEDLCD.display() is used to write push data currently in RAM to SSD1306 display.

In the loop() function we use the startscrollright(), startscrollleft(), startscrolldiagleft() and startscrolldiagright() to start left-handed, right-handed scroll, diagonal left scroll, and diagonal right scroll for the text part of the display.

Video demonstration

The following video demonstrates how the above I2C OLED LCD program works, and how the text is displayed and scrolled in various directions.

So this tutorial showed how to connect OLED LCD to NodeMCU using the I2C wires. We showed a simple program to show scrolling text in various directions. I2C devices are popular as they save pins. Here I2C capable LCD was used. For other I2C device tutorials see Write Read Sensor Data to External EEPROM with Arduino.

Post a Comment

Previous Post Next Post