Categories

Tutorial

LCD on ESP8266 using LUA

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

Detailed Tutorial

1. Introduction:

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

LCD-16x2-

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 16x2 LCD to the I2C LCD backpack.

esp8266-LCD-lua-connections

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.

LUA-16x2lcd

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

our whatsapp helpline: whatsapp-logo-robo-india +91 9694011188

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

Related Tutorials
ESP8266 Wiif Module Setup

Robo India presents tutorial how to setup ESP8266 wifi module. Hardware configuration to run ESP 8266 wifi module is explained in this tutorial.

ESP8266 Flash with NodeMCU firmware

Robo India presents tutorial how to flash ESP8266 wifi module with LUA firmware. NodeMCU firmware is used here.

Starting with LUA on ESP8266 Wifi module

Robo India presents tutorial on starting with LUA on ESP8266 wifi module and ESPLORER.

NodeMCU Amica Installation - ESP8266 Based Development Board

Robo India presents tutorial on how to install NodeMCU Amica, ESP8266 wifi module based development board.

Home Automation using ESP8266 on LUA

Robo India presents tutorial on how make HTTP server of ESP8266 wifi module in both station and access point mode. This server is being used to automatic control of LEDs. This tutorial can be implemented to automate home or industry.

I2C LCD on Arduino

This tutorial explains how to use I2C LCD on Arduino.

I2C Scanner - Arduino

This tutorial explains how to find attached i2c devices and their addresses on Arduino.

Robo India Tutorial portal

Illustrated tutorials and guides.

Feel free to contact us

© Robo India | http://roboindia.com