The ESP8266 NodeMCU Interface with LCD allows you to integrate an LCD with your NodeMCU board, enabling you to create projects with visual feedback.
Read more: ESP8266 NodeMCU Interface – LCDContents
LCD 16×2 HD44780 Display Interface
- We can easily add a 2×16 LCD to ESP8266.
- But ESP8266 has less GPIO pins.
- Two methods.
- Using i2c driver
- Using GPIO pins.
Code
This code provides basic functionality for interfacing with a 16×2 HD44780 LCD using an ESP8266 NodeMCU board. Adjustments may be needed based on your specific hardware setup and requirements.
-- Define GPIO pins for LCD connections local PIN_RS = 1 -- GPIO5 local PIN_EN = 2 -- GPIO4 local PIN_D4 = 3 -- GPIO0 local PIN_D5 = 4 -- GPIO2 local PIN_D6 = 5 -- GPIO14 local PIN_D7 = 6 -- GPIO12 -- Function to send a command to LCD local function lcd_command(cmd) gpio.write(PIN_RS, gpio.LOW) -- Set RS pin LOW for command mode -- Send high nibble gpio.write(PIN_D4, bit.isset(cmd, 4) and gpio.HIGH or gpio.LOW) gpio.write(PIN_D5, bit.isset(cmd, 5) and gpio.HIGH or gpio.LOW) gpio.write(PIN_D6, bit.isset(cmd, 6) and gpio.HIGH or gpio.LOW) gpio.write(PIN_D7, bit.isset(cmd, 7) and gpio.HIGH or gpio.LOW) gpio.write(PIN_EN, gpio.HIGH) gpio.write(PIN_EN, gpio.LOW) -- Send low nibble gpio.write(PIN_D4, bit.isset(cmd, 0) and gpio.HIGH or gpio.LOW) gpio.write(PIN_D5, bit.isset(cmd, 1) and gpio.HIGH or gpio.LOW) gpio.write(PIN_D6, bit.isset(cmd, 2) and gpio.HIGH or gpio.LOW) gpio.write(PIN_D7, bit.isset(cmd, 3) and gpio.HIGH or gpio.LOW) gpio.write(PIN_EN, gpio.HIGH) gpio.write(PIN_EN, gpio.LOW) -- Delay for command execution tmr.delay(2000) -- Adjust delay as needed end -- Function to send data to LCD local function lcd_data(data) gpio.write(PIN_RS, gpio.HIGH) -- Set RS pin HIGH for data mode -- Send high nibble gpio.write(PIN_D4, bit.isset(data, 4) and gpio.HIGH or gpio.LOW) gpio.write(PIN_D5, bit.isset(data, 5) and gpio.HIGH or gpio.LOW) gpio.write(PIN_D6, bit.isset(data, 6) and gpio.HIGH or gpio.LOW) gpio.write(PIN_D7, bit.isset(data, 7) and gpio.HIGH or gpio.LOW) gpio.write(PIN_EN, gpio.HIGH) gpio.write(PIN_EN, gpio.LOW) -- Send low nibble gpio.write(PIN_D4, bit.isset(data, 0) and gpio.HIGH or gpio.LOW) gpio.write(PIN_D5, bit.isset(data, 1) and gpio.HIGH or gpio.LOW) gpio.write(PIN_D6, bit.isset(data, 2) and gpio.HIGH or gpio.LOW) gpio.write(PIN_D7, bit.isset(data, 3) and gpio.HIGH or gpio.LOW) gpio.write(PIN_EN, gpio.HIGH) gpio.write(PIN_EN, gpio.LOW) -- Delay for data transfer tmr.delay(2000) -- Adjust delay as needed end -- Function to initialize the LCD local function lcd_init() -- Set GPIO pins as outputs gpio.mode(PIN_RS, gpio.OUTPUT) gpio.mode(PIN_EN, gpio.OUTPUT) gpio.mode(PIN_D4, gpio.OUTPUT) gpio.mode(PIN_D5, gpio.OUTPUT) gpio.mode(PIN_D6, gpio.OUTPUT) gpio.mode(PIN_D7, gpio.OUTPUT) -- Initialization sequence tmr.delay(15000) -- Wait for power-up lcd_command(0x33) -- Initialize lcd_command(0x32) -- Set to 4-bit mode lcd_command(0x28) -- 2 lines, 5x8 font lcd_command(0x0C) -- Display on, cursor off, blink off lcd_command(0x01) -- Clear display lcd_command(0x06) -- Increment cursor end -- Function to clear the LCD local function lcd_clear() lcd_command(0x01) -- Clear display end -- Function to set cursor position local function lcd_set_cursor(row, col) local offset = {0x00, 0x40} lcd_command(0x80 + offset[row] + col - 1) end -- Function to write a string to the LCD local function lcd_write_string(str) for i = 1, #str do lcd_data(string.byte(str, i)) end end -- Example usage lcd_init() -- Initialize the LCD lcd_clear() -- Clear the LCD lcd_set_cursor(1, 1) -- Set cursor to first row, first column lcd_write_string("Hello, World!") -- Write a string to the LCD
Code Explanation
Section | Explanation |
---|---|
Initialization | – The code initializes the LCD module by setting up the GPIO pins for communication and configuring the LCD settings. |
Function Definitions | – Defines helper functions for sending commands and data to the LCD, such as lcd_send_command and lcd_send_data . These functions handle the low-level communication with the LCD module. |
LCD Initialization | – Initializes the LCD by sending a sequence of commands to configure its display settings, such as the number of lines, cursor behavior, and display on/off settings. |
Clearing the Display | – Sends a command to clear the display and return the cursor to the home position. |
Setting Cursor | – Sets the cursor position on the LCD. The lcd_set_cursor function takes the row and column numbers as parameters and calculates the corresponding position based on the LCD’s row length. |
Writing Characters | – Writes characters to the LCD at the current cursor position. The lcd_write_char function sends data to the LCD to display the specified character. |
Writing Strings | – Writes strings to the LCD. The lcd_write_string function iterates over each character in the string and calls lcd_write_char to display them sequentially. |
Main Function | – Demonstrates how to use the defined functions to interact with the LCD. It initializes the LCD, clears the display, sets the cursor to the first position, and writes a string to the LCD. |
Nokia-5110 PCD8544 Display interface
- Required NodeMCU Modules (Firmware): GPIO Module
- Required hardware: ESP8266 with Programmer (or) NodeMCU Dev Kit, Nokia 5110 PCD8544 LCD Display,
- Required software tools: ESPlorer IDE Tool
Code
`lua
-- https://www.aruneworld.com/embedded/esp8266/esp8266-nodemcu/
-- Tested By : Arun(20170212)
-- Example Name : AEW_LCD_Nokia-5110_PCD8544.lua
-- Reference data sheet: https://www.sparkfun.com/datasheets/LCD/Monochrome/Nokia5110.pdf
--------------------------------------------------------------------------------
-- The following code referred by this post: http://playground.arduino.cc/Code/PCD8544
-- You need to configure the pins you are using here
PIN_RESET = 0
PIN_SCE = 1
PIN_DC = 2
PIN_SDIN = 3
PIN_SCLK = 4
local LCD_D = gpio.HIGH
local LCD_C = gpio.LOW
local LCD_X = 84
local LCD_Y = 48
local chars = "\0\0\0\0\0\0\0\95\0\0\0\7\0\7\0\20\127\20\127\20\36\42\127\42\18\35\19\8\100\98\54\73\85\34\80\0\5\3\0\0\0\28\34\65\0\0\65\34\28\0\20\8\62\8\20\8\8\62\8\8\0\80\48\0\0\8\8\8\8\8\0\96\96\0\0\32\16\8\4\2\62\81\73\69\62\0\66\127\64\0\66\97\81\73\70\33\65\69\75\49\24\20\18\127\16\39\69\69\69\57\60\74\73\73\48\1\113\9\5\3\54\73\73\73\54\6\73\73\41\30\0\54\54\0\0\0\86\54\0\0\8\20\34\65\0\20\20\20\20\20\0\65\34\20\8\2\1\81\9\6\50\73\121\65\62\126\17\17\17\126\127\73\73\73\54\62\65\65\65\34\127\65\65\34\28\127\73\73\73\65\127\9\9\9\1\62\65\73\73\122\127\8\8\8\127\0\65\127\65\0\32\64\65\63\1\127\8\20\34\65\127\64\64\64\64\127\2\12\2\127\127\4\8\16\127\62\65\65\65\62\127\9\9\9\6\62\65\81\33\94\127\9\25\41\70\70\73\73\73\49\1\1\127\1\1\63\64\64\64\63\31\32\64\32\31\63\64\56\64\63\99\20\8\20\99\7\8\112\8\7\97\81\73\69\67\0\127\65\65\0\2\4\8\16\32\0\65\65\127\0\4\2\1\2\4\64\64\64\64\64\0\1\2\4\0\32\84\84\84\120\127\72\68\68\56\56\68\68\68\32\56\68\68\72\127\56\84\84\84\24\8\126\9\1\2\12\82\82\82\62\127\8\4\4\120\0\68\125\64\0\32\64\68\61\0\127\16\40\68\0\0\65\127\64\0\124\4\24\4\120\124\8\4\4\120\56\68\68\68\56\124\20\20\20\8\8\20\20\24\124\124\8\4\4\8\72\84\84\84\32\4\63\68\64\32\60\64\64\32\124\28\32\64\32\28\60\64\48\64\60\68\40\16\40\68\12\80\80\80\60\68\100\84\76\68\0\8\54\65\0\0\0\127\0\0\0\65\54\8\0\16\8\8\16\8\120\70\65\70\120"
local function shiftOut(d, cl, data)
for index = 0, 7 do
if bit.isset(data, 7 - index) then
gpio.write(d, gpio.HIGH)
else
gpio.write(d, gpio.LOW)
end
gpio.write(cl, gpio.HIGH)
tmr.delay(5)
gpio.write(cl, gpio.LOW)
end
end
local function LCDWrite(dc, data)
gpio.write(PIN_DC, dc)
gpio.write(PIN_SCE, gpio.LOW)
shiftOut(PIN_SDIN, PIN_SCLK, data)
gpio.write(PIN_SCE, gpio.HIGH)
end
function LcdSetPins(sclk, sdin, dc, sce, reset)
PIN_SCLK = sclk
PIN_SDIN = sdin
PIN_DC = dc
PIN_SCE = sce
PIN_RESET = reset
end
function LcdLocate(x, y)
LCDWrite(LCD_C, 64 + x)
LCDWrite(LCD_C, 128 + y)
end
function LcdCharacter(character)
LCDWrite(LCD_D, 0x00)
for index = 0, 5 do
LCDWrite(LCD_D, character)
end
LCDWrite(LCD_D, 0x00)
end
function LcdClear()
local c = 84 * 6
for index = 0, c do
tmr.delay(5)
LCDWrite(LCD_D, 0)
end
end
function LcdPrintChar(c)
LCDWrite(LCD_D, 0)
local char_index = 1 + (string.byte(c) - 32) * 5
for index = 0, 4 do
LCDWrite(L
Code Explanation
This Lua code initializes and interacts with a Nokia 5110 LCD module (PCD8544 controller) using an ESP8266 NodeMCU board. The code provides a basic framework for interfacing with the Nokia 5110 LCD module using the ESP8266 NodeMCU board, allowing for the display of text and basic graphics. Here’s the explanation:
Section | Explanation |
---|---|
Initialization and Pins | – Pins for interfacing with the LCD module are configured (RESET, SCE, DC, SDIN, SCLK). – References and credits are provided in the comments. |
LCD Character Data | – Character data for the LCD is defined in the chars variable. Each character is represented by a series of bytes. |
Shift Out Function | – Sends data to the LCD module by shifting bits out through a specified pin. |
LCD Write Function | – Writes data to the LCD. Sets the appropriate data/command mode and then uses shiftOut to send the data. |
Setting Pin Configuration | – Allows setting the pins used for communication with the LCD dynamically. |
LCD Cursor Positioning | – Positions the cursor at the specified coordinates on the LCD. |
Writing a Character | – Writes a single character to the LCD at the current cursor position. |
Clearing the LCD | – Clears the entire LCD display. |
Printing a Character | – Prints a character to the LCD, advancing the cursor position accordingly. |
Initialization | – Initializes the LCD module with the appropriate settings. |
Test Function | – A test function that initializes the LCD and prints some sample text. |
Next
ESP8266 NodeMCU Interface – LCD