Menu Close

LCD on ESP8266 using LUA

Robo India presents tutorial on how to run LCD on ESP8266 using LUA programming. 

1. Introduction:

This tutorial explains how to use LCD for displaying purpose on ESP8266 using LUA programming. We are using 16×2 character LCD in this tutorial. This tutorial has got LUA file you can include that file in your existing project to use LCD.

You need to have basic understanding of ESP8266, below mentioned tutorials of Robo India may help you out in this.

1. This tutorial explains configuration and setup of ESP8266

2. This tutorial explains basic of ESPlorer.

2. Circuit

The basic setup of ESP should be done along with basic setup make the following circuit.

We are using I2C LCD backpack here in this tutorial. Mount 16×2 LCD to the I2C LCD backpack.

3. Programming 

Before moving ahead to the programming you must know the i2c device no of the LCD Backpack, here is a tutorial of Robo India for I2C scanning.

I2C Scanner

once you know i2c address of LCD download the following file lcd.lua open the same and enter address of i2c on line no. 7th. In our case our i2c address is 0X3F.

You may download lcd.lua from here.

local M
do
local id = 0
local sda = 3      -- GPIO0
local scl = 4      -- GPIO2
local dev = 0x3F   -- I2C Address
local reg = 0x00   -- write
i2c.setup(id, sda, scl, i2c.SLOW)

local bl = 0x08      -- 0x08 = back light on

local function send(data)
   local value = {}
   for i = 1, #data do
      table.insert(value, data[i] + bl + 0x04 + rs)
      table.insert(value, data[i] + bl +  rs)      -- fall edge to write
   end
  
   i2c.start(id)
   i2c.address(id, dev ,i2c.TRANSMITTER)
   i2c.write(id, reg, value)
   i2c.stop(id)
end
 
if (rs == nil) then
-- init
 rs = 0
 send({0x30})
 tmr.delay(4100)
 send({0x30})
 tmr.delay(100)
 send({0x30})
 send({0x20, 0x20, 0x80})      -- 4 bit, 2 line
 send({0x00, 0x10})            -- display clear
 send({0x00, 0xc0})            -- display on
end

local function cursor(op)
 local oldrs=rs
 rs=0
 if (op == 1) then 
   send({0x00, 0xe0})            -- cursor on
  else 
   send({0x00, 0xc0})            -- cursor off
 end
 rs=oldrs
end

local function cls()
 local oldrs=rs
 rs=0
 send({0x00, 0x10})
 rs=oldrs
end

local function home()
 local oldrs=rs
 rs =0
 send({0x00, 0x20})
 rs=oldrs
end


local function lcdprint (str,line,col)
if (type(str) =="number") then
 str = tostring(str)
end
rs = 0
--move cursor
if (line == 2) then
 send({0xc0,bit.lshift(col,4)})
elseif (line==1) then 
 send({0x80,bit.lshift(col,4)})
end

rs = 1
for i = 1, #str do
 local char = string.byte(string.sub(str, i, i))
 send ({ bit.clear(char,0,1,2,3),bit.lshift(bit.clear(char,4,5,6,7),4)})
end

end

M={
lcdprint=lcdprint,
cls = cls,
home=home,
cursor=cursor,
}
end
return M

4. Calling the function:

The lcd.lua file has got following functions.

4.1 General Functions:

1. For clearing screen –
dofile(“lcd.lua”).cls();
2. Set cursor at Column 0 and Row 0 
dofile(“lcd.lua”).home();
3. To show cursor
dofile(“lcd.lua”).cursor(1);
4.  to hide cursor
dofile(“lcd.lua”).cursor(0);

4.2 Printing functions:

dofile(“lcd.lua”).lcdprint(“String”,Row,Column);

Rows can be 1 and 2. Columns can be 0 to 15.

example – dofile(“lcd.lua”).lcdprint(“Hello World!”,0,0);

4.3 How to run these functions:

Just copy and paste these functions to ESPlorer or any other interfacing software with ESP8266 and you will see the result on LCD screen.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
http://roboindia.com

Leave a Reply