ESP8266 NodeMCU 16×2 LCD Interface

In this tutorial we interface 16×2 LCD display with ESP8266 NodeMCU without using I2C and with minimum pins i.e. using only 3 pins. To interface LCD with ESP8266 we need at least 6 IO lines but ESP already have very few IO lines, to expand IO lines we use serial to parallel shift register IC 74HC595.

A 16×2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5×7 pixel matrix. This LCD has two registers, namely, Command and Data.

The command register stores the command instructions given to the LCD. A command is an instruction given to LCD to do a predefined task like initializing it, clearing its screen, setting the cursor position, controlling display etc. The data register stores the data to be displayed on the LCD. The data is the ASCII value of the character to be displayed on the LCD.

Step 1: Circuit Diagram of ESP8266 LCD Interface

How to connect LCD with Node MCU?

Node MCU ESP8266 16x2 LCD Display

Step 2: ESP8266 NodeMCU Program for LCD Interface

How to program ESP8266 NodeMCU for LCD DIsplay?

We are not using any library for interfacing.

/*
  Three Wire LCD Interface using 74HC595 with ESP8266 NodeMCU
  www.circuits4you.com
*/
#include <ESP8266WiFi.h>

//Define 74HC595 Connections with arduio
const int Clock=13;
const int Data=12;
const int Latch=14;

void LCDinit(void);
void LCDSendByte(char d,char RS);
void LCDPrint(char Line,char string[16]);
//=========================================================
//              Setup
//=========================================================
void setup() {
  // initialize the digital pin as an output.
  pinMode(Data, OUTPUT);
  pinMode(Clock, OUTPUT);
  pinMode(Latch, OUTPUT);
  LCDinit(); //initalize LCD in 4-bit mode
}
//========================================================
//                Loop
//=========================================================
void loop() {
    LCDPrint(0,"Circuits4You");
    LCDPrint(1,"Hello There");
    delay(500);
}
//=============================================================
//      Send Data or Command to LCD
//=============================================================
void LCDSendByte(char d,char RS)
{
  char dH,dL,temp; //Keep Data on upper nybble
  dH = d & 0xF0; //Get MSB
  dL = d & 0x0F;
  dL = d << 4; //Get LSB
  
  //Send MSB with E=clock
  temp=0;
  temp=dH | RS | 0x02; //MSB With RS+E bit
  ShiftData(temp); //Send MSB with E=0
  temp=0;
  temp=dH | RS; //MSB With RS bit
  ShiftData(temp);  //Send LSB with E=clock
  temp=0;
  temp=dL | RS | 0x02; //MSB With RS+E bit
  ShiftData(temp); //Send LSB with E=0
  temp=0;
  temp=dL | RS; //MSB With RS bit
  ShiftData(temp);
}
//=============================================
void ShiftData(char temp)
{
  int i;
  for(i=0;i<8;i++)
  {
    if((temp & 0x80)==0x80)   //Send 1-bit to shift register
    {digitalWrite(Data,HIGH);}
    else
    {digitalWrite(Data,LOW);}
    
    digitalWrite(Clock,LOW);
    digitalWrite(Clock,HIGH);
    temp=temp<<1;
  }
  //Latch the data
  digitalWrite(Latch,LOW);
  delay(1);
  digitalWrite(Latch,HIGH);
}
//=============================================================
//        LCD Display Initialization Function
//=============================================================
void LCDinit()
{
  int count;
  char t[]={0x43,0x03,0x03,0x02,0x28,0x01,0x0C,0x06,0x02,0x02};
  for (count = 0; count <= 9; count++)
  {
      LCDSendByte(t[count],0);  //Command RS=0
  }
}
//=============================================================
//
Display Line on LCD at desired location Function
//=============================================================
void LCDPrint(char Line,char string[16])
{
  int len,count;
  if(Line==0) {
    LCDSendByte(0x80,0);  //Command RS=0 Move Cursor to Home
  }
  else {
    LCDSendByte(0xC0,0);  //Command RS=0 Move Cursor to Second Line
  }
  len = strlen(string);
  
  for (count=0;count<len;count++)
  {
    LCDSendByte(string[count],1); //Data RS=1
  }
}
//=============================================================

Step 3: Testing

Upload Program and see display will show Circuits4you.com. Give +5V to 74HC595 and Operate NodeMCU at 3.3V levels.

 

Leave a Reply